Tensorflow 连接局部变量时出错

Tensorflow 连接局部变量时出错,tensorflow,Tensorflow,我尝试制作一个自定义图层。在急切模式下,一切正常,并已成功编译。但当我尝试拟合我的模型时,我遇到了下一个错误:当我尝试将self.total变量与计算张量连接起来时,我认识到原因是self.total变量。如果我删除循环,我就不能处理大于1的输入值。也许有另一种方法可以在没有错误的情况下得到正确的结果 def call(self, inputs, **kwargs): for i in tf.range(0, tf.shape(inputs)[0]): self.tot

我尝试制作一个自定义图层。在急切模式下,一切正常,并已成功编译。但当我尝试拟合我的模型时,我遇到了下一个错误:当我尝试将self.total变量与计算张量连接起来时,我认识到原因是self.total变量。如果我删除循环,我就不能处理大于1的输入值。也许有另一种方法可以在没有错误的情况下得到正确的结果

def call(self, inputs, **kwargs):

    for i in tf.range(0, tf.shape(inputs)[0]):
        self.total = tf.concat([self.total, tf.reshape(self.concat_result(inputs[i]), [1, 3])], 0)

    return self.total

def __init__(self):
    super(ConcatLayer, self).__init__(dtype=tf.float64)
    self.total = tf.Variable((np.empty((0, 3), dtype=np.float64)), shape=[None, 3]) 
错误:

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: while:3

concat_result()中发生了什么?请发布更多代码/这没关系。如果我删除了循环,并且没有将结果收集到self,那么工作正常。全部的