Tensorflow:FailedPreconditionError (see above for traceback): Attempting to use uninitialized value

错误:

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

代码:

n = tf.sqrt(tf.reduce_sum(tf.square(tf.Variable([[1,2,3],[2,3,4]],dtype=tf.float32)),keep_dims=True))
 with tf.Session as sess:
      n1 = sess.run(n)

问题原因:
tf.Variable( ) 定义的变量没有初始化。
只有tensor变量需要初始化,tensor常量不需要初始化。

应该添加以下代码:

sess.run(tf.global_variables_initializer())

正确代码:

n = tf.sqrt(tf.reduce_sum(tf.square(tf.Variable([[1,2,3],[2,3,4]],dtype=tf.float32)),keep_dims=True))
 with tf.Session as sess:
      sess.run(tf.global_variables_initializer())
      n1 = sess.run(n)

你可能感兴趣的:(Tensorflow)