理解:TypeError:函数构建代码之外的op被传递一个“图形”张量

2024-04-26 03:39:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图创建一个FIFO队列,其中包含的表示不会反向传播到图形中,这要归功于以下代码行:

z2 = tf.stop_gradient(self.momentum_projector(h2, training=True))
self.queue = self._update_queue(z2, self.queue, self.queue_size)

def _update_queue(self, new_entry, queue, max_queue_size):
        if self.queue is None:
            new_queue = new_entry
        else:
            new_queue  = tf.concat((new_entry, queue), axis = 0)
        new_queue      = new_queue[:max_queue_size]
        return new_queue

但是,当我执行代码时,出现以下错误:

TypeError: An op outside of the function building code is being passed
a "Graph" tensor. It is possible to have Graph tensors
leak out of the function building context by including a
tf.init_scope in your function building code.
For example, the following function will fail:
  @tf.function
  def has_init_scope():
    my_constant = tf.constant(1.)
    with tf.init_scope():
      added = my_constant * 2
The graph tensor has name: PartitionedCall:0

我试图在函数中添加一个tf.function,但没有成功。有人有解决这个错误的建议吗