tensorflow - 无效参数错误 : The node has inputs from different frames

标签 tensorflow

我在玩 Tensorflow 时遇到了这个代码的问题:

def process_tree_tf(matrix, weights, idxs, name=None):

    with tf.name_scope(name, "process_tree", [tree, weights, idxs]).as scope():
         loop_index = tf.sub(tf.shape(matrix)[0], 1)
         loop_vars = loop_index, matrix, idxs, weights

         def loop_condition(loop_idx, *_):
             return tf.greater(loop_idx, 0)

         def loop_body(loop_idx, mat, idxs, weights):
             x = mat[loop_idx]
             w = weights
             bias = tf.Variable(tf.constant(0.1, [2], dtype=tf.float64)) # Here?

             ...
             return loop_idx-1, mat, idxs, weights

         return tf.while_loop(loop_condition, loop_body, loop_vars, name=scope)[1]

我正在以这种方式评估该功能:
height = 2
width = 2
nodes = 4
matrix = np.ones((nodes, width+height))
weights = np.ones((width+height, width))/100
idxs = [0,0,1,2]
with tf.Session as sess():
    sess.run(tf.global_variables_initializer()) # Error Here!
    r = process_tree_tf(matrix, weights, idxs)
    print(r.eval())

我收到此错误:

InvalidArgumentError: The node 'process_tree_tf/Variable/Assign' has inputs from different frames. The input 'process_tree_tf/Const_1' is in frame 'process_tree_tf/process_tree_tf/'. The input 'process_tree_tf/Variable' is in frame ''.



奇怪的是,如果我在 jupyter notebook 中重新启动内核并再次运行,我会收到此错误:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value bias [[Node: bias/read = IdentityT=DT_FLOAT, _class=["loc:@bias"], _device="/job:localhost/replica:0/task:0/cpu:0"]]



我尝试改用这个:bias = tf.get_variable("bias", shape=[2], initializer=tf.constant_initializer(0.1))但这也不起作用。

如果我在这里忽略了一些明显的东西,我很抱歉,但如果有人能告诉我我哪里出错了,我真的很感激。

非常感谢!

最佳答案

这实际上是 tf.Variable 的一个微妙问题。 TensorFlow 中的对象 tf.while_loop() . TensorFlow 变得困惑,因为似乎 tf.constant() 用于初始化变量的值是在循环内创建的值(即使它显然是循环不变的),但所有变量都被提升到循环外。最简单的解决方法是将变量的创建移到循环之外:

def process_tree_tf(matrix, weights, idxs, name=None):

    with tf.name_scope(name, "process_tree", [tree, weights, idxs]).as scope():
         loop_index = tf.sub(tf.shape(matrix)[0], 1)
         loop_vars = loop_index, matrix, idxs, weights

         # Define the bias variable outside the loop to avoid problems.
         bias = tf.Variable(tf.constant(0.1, [2], dtype=tf.float64)) 

         def loop_condition(loop_idx, *_):
             return tf.greater(loop_idx, 0)

         def loop_body(loop_idx, mat, idxs, weights):
             x = mat[loop_idx]
             w = weights

             # You can still refer to `bias` in here, and the loop body
             # will capture it appropriately.
             ...
             return loop_idx-1, mat, idxs, weights

         return tf.while_loop(loop_condition, loop_body, loop_vars, name=scope)[1]

(另一种可能的解决方法是在创建变量时使用 tf.constant_initializer() 而不是 tf.constant() 。)

关于tensorflow - 无效参数错误 : The node has inputs from different frames,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42564698/

相关文章:

c++ - 如何使用tensorflow 2.0 C API?

python - 强化学习如何通过高斯策略进行连续控制?

python - Tensorflow:关闭 session 后访问经过训练的变量

python - 第 3 部分 : Switching between multiple contexts - no error and a bad exit code

machine-learning - 从 Keras 中的 3 维张量收集 2 维张量列表

python-3.x - 导入tensorflow时出现以下错误: No module named 'numpy.core._multiarray_umath'

python - Tensorflow:训练并不能提高准确性

python - 如何训练用于无人机姿态估计的自定义关键点检测器。检测器2

text-classification - TensorFlow - 使用神经网络进行文本分类

r - Keras R 图像分类模型中的形状错误