我正在玩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]
Run Code Online (Sandbox Code Playgroud)
我正在用这种方式评估函数:
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())
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
InvalidArgumentError:节点'process_tree_tf/Variable/Assign'具有来自不同帧的输入.输入'process_tree_tf/Const_1'在框架'process_tree_tf/process_tree_tf /'中.输入'process_tree_tf/Variable'在框架''中.
奇怪的是,如果我在jupyter笔记本中重新启动内核并再次运行,我会收到此错误:
FailedPreconditionError(参见上面的回溯):尝试使用未初始化的值偏差[[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.VariableTensorFlow中对象的一个微妙问题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]
Run Code Online (Sandbox Code Playgroud)
(另一种可能的解决方案是在创建变量时使用tf.constant_initializer()而不是a tf.constant().)
| 归档时间: |
|
| 查看次数: |
4212 次 |
| 最近记录: |