我正在尝试使用GPU在tensorflow上运行我的卷积模型,我试过这个example并且它工作正常。但是,当我运行我的代码时,我收到以下错误。我不是什么问题。知道怎么解决这个问题吗?
File "Network.py", line 379, in <module>
print('%d\t%f\t%.1f%%\t%.1f%%' % (step, l, accuracy(predictions, batch_labels), accuracy(valid_prediction.eval(), valid_labels)))
File "/home/mido/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 567, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/home/mido/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3729, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/home/mido/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/home/mido/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "/home/mido/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "/home/mido/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value weight_4
[[Node: weight_4/read = Identity[T=DT_FLOAT, _class=["loc:@weight_4"], _device="/job:localhost/replica:0/task:0/gpu:0"](weight_4)]]
[[Node: Softmax_1/_1 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_55_Softmax_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
部分代码:
with graph.as_default() :
w_b = {
'weight_4': tf.Variable(tf.random_normal([num_hidden, num_labels], stddev=0.1), name = 'weight_4'),
'bias_4' : tf.Variable(tf.constant(1.0, shape=[num_labels]), name = 'bias_4'),}
w_b = {
'wc1_0': tf.Variable(tf.random_normal([patch_size_1, patch_size_1, num_channels, depth],stddev=0.1), name = 'wc1_0'),
'bc1_0' : tf.Variable(tf.zeros([depth]), name = 'bc1_0'), }
.......
init = tf.global_variables_initializer()
with tf.Session(graph=graph) as sess:
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(init)
for step in range(num_steps):
offset = (step * batch_size) % (train_labels.shape[0] - batch_size)
batch_data = train_dataset[offset:(offset + batch_size), :, :, :]
batch_labels = train_labels[offset:(offset + batch_size), :]
feed_dict = {tf_train_dataset : batch_data, tf_train_labels : batch_labels}
_, l, predictions = sess.run([optimizer, loss, train_prediction ], feed_dict=feed_dict)
if (step % 50 == 0):
print('%d\t%f\t%.1f%%\t%.1f%%' % (step, l, accuracy(predictions, batch_labels), accuracy(valid_prediction.eval(), valid_labels)))
答案 0 :(得分:1)
您正在初始化会话范围之外的变量,并且您在会话初始化时做了一些奇怪的事情(您要多次重新分配它)。我通常以下列方式定义我的模型参数,以便获得正确的结果:
with tf.Session() as sess:
# define your variables and tensors
# ... initialization code ...
sess.run(tf.global_variables_initializer())
# ... training code ...
通过这种方式,您可以正确初始化模型图。