Python TensorFlow:变量初始化中的“尝试使用未初始化的值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36007883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TensorFlow: "Attempting to use uninitialized value" in variable initialization
提问by NEW USER
I am trying to implement multivariate linear regression in Python using TensorFlow, but have run into some logical and implementation issues. My code throws the following error:
我正在尝试使用 TensorFlow 在 Python 中实现多元线性回归,但遇到了一些逻辑和实现问题。我的代码抛出以下错误:
Attempting to use uninitialized value Variable
Caused by op u'Variable/read'
Ideally the weights
output should be [2, 3]
理想情况下,weights
输出应该是[2, 3]
def hypothesis_function(input_2d_matrix_trainingexamples,
output_matrix_of_trainingexamples,
initial_parameters_of_hypothesis_function,
learning_rate, num_steps):
# calculate num attributes and num examples
number_of_attributes = len(input_2d_matrix_trainingexamples[0])
number_of_trainingexamples = len(input_2d_matrix_trainingexamples)
#Graph inputs
x = []
for i in range(0, number_of_attributes, 1):
x.append(tf.placeholder("float"))
y_input = tf.placeholder("float")
# Create Model and Set Model weights
parameters = []
for i in range(0, number_of_attributes, 1):
parameters.append(
tf.Variable(initial_parameters_of_hypothesis_function[i]))
#Contruct linear model
y = tf.Variable(parameters[0], "float")
for i in range(1, number_of_attributes, 1):
y = tf.add(y, tf.multiply(x[i], parameters[i]))
# Minimize the mean squared errors
loss = tf.reduce_mean(tf.square(y - y_input))
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss)
#Initialize the variables
init = tf.initialize_all_variables()
# launch the graph
session = tf.Session()
session.run(init)
for step in range(1, num_steps + 1, 1):
for i in range(0, number_of_trainingexamples, 1):
feed = {}
for j in range(0, number_of_attributes, 1):
array = [input_2d_matrix_trainingexamples[i][j]]
feed[j] = array
array1 = [output_matrix_of_trainingexamples[i]]
feed[number_of_attributes] = array1
session.run(train, feed_dict=feed)
for i in range(0, number_of_attributes - 1, 1):
print (session.run(parameters[i]))
array = [[0.0, 1.0, 2.0], [0.0, 2.0, 3.0], [0.0, 4.0, 5.0]]
hypothesis_function(array, [8.0, 13.0, 23.0], [1.0, 1.0, 1.0], 0.01, 200)
采纳答案by mrry
It's not 100% clear from the code example, but if the list initial_parameters_of_hypothesis_function
is a list of tf.Variable
objects, then the line session.run(init)
will fail because TensorFlow isn't (yet) smart enough to figure out the dependencies in variable initialization. To work around this, you should change the loop that creates parameters
to use initial_parameters_of_hypothesis_function[i].initialized_value()
, which adds the necessary dependency:
从代码示例中并不是 100% 清楚,但是如果列表initial_parameters_of_hypothesis_function
是tf.Variable
对象列表,那么该行将session.run(init)
失败,因为 TensorFlow(还)不够聪明,无法确定变量初始化中的依赖关系。要解决此问题,您应该将创建的循环更改parameters
为 use initial_parameters_of_hypothesis_function[i].initialized_value()
,这会添加必要的依赖项:
parameters = []
for i in range(0, number_of_attributes, 1):
parameters.append(tf.Variable(
initial_parameters_of_hypothesis_function[i].initialized_value()))
回答by Philippe Remy
Run this:
运行这个:
init = tf.global_variables_initializer()
sess.run(init)
Or (depending on the version of TF that you have):
或者(取决于您拥有的 TF 版本):
init = tf.initialize_all_variables()
sess.run(init)
回答by o0omycomputero0o
There is another the error happening which related to the order when calling initializing global variables. I've had the sample of code has similar error FailedPreconditionError (see above for traceback): Attempting to use uninitialized value W
调用初始化全局变量时发生的另一个错误与顺序有关。我的代码示例有类似的错误FailedPreconditionError(见上文的回溯):尝试使用未初始化的值 W
def linear(X, n_input, n_output, activation = None):
W = tf.Variable(tf.random_normal([n_input, n_output], stddev=0.1), name='W')
b = tf.Variable(tf.constant(0, dtype=tf.float32, shape=[n_output]), name='b')
if activation != None:
h = tf.nn.tanh(tf.add(tf.matmul(X, W),b), name='h')
else:
h = tf.add(tf.matmul(X, W),b, name='h')
return h
from tensorflow.python.framework import ops
ops.reset_default_graph()
g = tf.get_default_graph()
print([op.name for op in g.get_operations()])
with tf.Session() as sess:
# RUN INIT
sess.run(tf.global_variables_initializer())
# But W hasn't in the graph yet so not know to initialize
# EVAL then error
print(linear(np.array([[1.0,2.0,3.0]]).astype(np.float32), 3, 3).eval())
You should change to following
您应该更改为以下
from tensorflow.python.framework import ops
ops.reset_default_graph()
g = tf.get_default_graph()
print([op.name for op in g.get_operations()])
with tf.Session() as
# NOT RUNNING BUT ASSIGN
l = linear(np.array([[1.0,2.0,3.0]]).astype(np.float32), 3, 3)
# RUN INIT
sess.run(tf.global_variables_initializer())
print([op.name for op in g.get_operations()])
# ONLY EVAL AFTER INIT
print(l.eval(session=sess))
回答by Lerner Zhang
Normally there are two ways of initializing variables, 1) using the sess.run(tf.global_variables_initializer())
as the previous answers noted; 2) the load the graph from checkpoint.
通常有两种初始化变量的方法,1)使用sess.run(tf.global_variables_initializer())
前面提到的答案;2)从检查点加载图形。
You can do like this:
你可以这样做:
sess = tf.Session(config=config)
saver = tf.train.Saver(max_to_keep=3)
try:
saver.restore(sess, tf.train.latest_checkpoint(FLAGS.model_dir))
# start from the latest checkpoint, the sess will be initialized
# by the variables in the latest checkpoint
except ValueError:
# train from scratch
init = tf.global_variables_initializer()
sess.run(init)
And the third method is to use the tf.train.Supervisor. The session will be
第三种方法是使用tf.train.Supervisor。会议将于
Create a session on 'master', recovering or initializing the model as needed, or wait for a session to be ready.
在“master”上创建会话,根据需要恢复或初始化模型,或等待会话准备就绪。
sv = tf.train.Supervisor([parameters])
sess = sv.prepare_or_wait_for_session()
回答by Shu Zhang
run both:
运行两个:
sess.run(tf.global_variables_initializer())
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
sess.run(tf.local_variables_initializer())
回答by Gao Yin
I want to give my resolution, it work when i replace the line [session = tf.Session()]
with [sess = tf.InteractiveSession()]
. Hope this will be useful to others.
我想给出我的解决方案,当我[session = tf.Session()]
用[sess = tf.InteractiveSession()]
. 希望这对其他人有用。