执行以下代码,报错tensorflow.python.framework.errors_impl.FailedPreconditionError: Could not find variable Variable.
原因:使用Variable需要初始化
#变量Operation
a = tf.Variable(initial_value=30.0)
b = tf.Variable(initial_value=40.0)
sum = tf.add(a, b)
with tf.Session() as sess:
print(sess.run(sum))
修改为:
#变量Operation
a = tf.Variable(initial_value=30.0)
b = tf.Variable(initial_value=40.0)
sum = tf.add(a, b)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
#手动运行init_op
sess.run(init_op)
print(sess.run(sum))
改后可正常运行