我正在追踪cifar10示例代码here 。我在下面的代码片段中得到了一些问题
def _variable_on_cpu(name, shape, initializer):
"""Helper to create a Variable stored on CPU memory.
Args:
name: name of the variable
shape: list of ints
initializer: initializer for Variable
Returns:
Variable Tensor
"""
with tf.device('/cpu:0'):
dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
var = tf.get_variable(name, shape, initializer=initializer, dtype=dtype)
return var
似乎没有初始化var(无论是global_variables_initializer()还是var.initializer.run()) 它运行得很好。
但是,以下测试代码会引发错误:
" FailedPreconditionError:尝试使用未初始化的值test / x"
import tensorflow as tf
with tf.variable_scope('test') as scope:
x = tf.get_variable('x', shape = [3, 4], initializer = tf.constant_initializer([0,1,2]))
sess = tf.InteractiveSession()
print(sess.run(x))
有没有人有任何想法?
答案 0 :(得分:0)
您在底部添加的代码无效,因为您尚未初始化变量。所以只需添加初始化:
import tensorflow as tf
with tf.variable_scope('test') as scope:
x = tf.get_variable('x', shape = [3, 4], initializer = tf.constant_initializer([0,1,2]))
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
print(sess.run(x))