gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:44:05 24 4
gpt4 key购买 nike

我在玩 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com