TensorLow: FailedPreconditionError:Attempting to use uninitialized value Variable

问题:

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

出处:

constant_e = tf.Variable(tf.random_normal(shape=[2, 3],stddev=2, dtype=tf.float32))
sess.run(constant_e)

原因:
Tensorflow中,所有变量都必须初始化才能使用。

方法:

init = tf.global_variables_initializer()
sess.run(init)

结果:

constant_e = tf.Variable(tf.random_normal(shape=[2, 3], stddev=2, dtype=tf.float32))
init = tf.global_variables_initializer()
sess.run(init)
print(sess.run(constant_e ))

猜你喜欢

转载自blog.csdn.net/shaozhulei555/article/details/78469883
今日推荐