gpt4 book ai didi

python - 尝试使用未初始化的值时出错

转载 作者:太空宇宙 更新时间:2023-11-03 15:32:48 26 4
gpt4 key购买 nike

我正在尝试使用 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)))

最佳答案

您正在初始化 session 范围之外的变量,并且您在 session 初始化中做了奇怪的事情(您正在多次重新分配它)。我通常通过以下方式定义模型参数以获得正确的结果:

with tf.Session() as sess:
# define your variables and tensors
# ... initialization code ...
sess.run(tf.global_variables_initializer())

# ... training code ...

通过这种方式,您将能够正确初始化模型图。

关于python - 尝试使用未初始化的值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com