错误是“试图使用未初始化的值state_variable”。但是我认为这个代码应该初始化这个值。
这是我的代码,谢谢:
import tensorflow as tf
def index():
state = tf.Variable(0, name="state_variable")
new_value = tf.add(state, tf.constant(1))
update = tf.assign(state, new_value)
return update
if __name__ == "__main__":
with tf.Session() as sess:
init_op = tf.group(tf.local_variables_initializer(),
tf.global_variables_initializer())
op = index()
sess.run(init_op)
for _ in range(4):
print(sess.run(op))发布于 2017-10-10 16:02:37
您需要将op = index()行放在使用tf.group定义init_op的行前面。目前,当您调用tf.group时,还没有调用tf.Variable(0, name="state_variable"),因此tf.group不知道如何将其初始化放入init_op中。以下版本在我的机器上运行良好:
import tensorflow as tf
def index():
state = tf.Variable(0, name="state_variable")
new_value = tf.add(state, tf.constant(1))
update = tf.assign(state, new_value)
return update
if __name__ == "__main__":
with tf.Session() as sess:
op = index()
init_op = tf.group(tf.local_variables_initializer(),
tf.global_variables_initializer())
sess.run(init_op)
for _ in range(4):
print(sess.run(op))https://stackoverflow.com/questions/46671009
复制相似问题