gpt4 book ai didi

python - 将带有交互式 session 的 Tensorflow MNIST 代码更改为 session

转载 作者:行者123 更新时间:2023-11-28 21:12:08 26 4
gpt4 key购买 nike

所以,我一直在学习tensorflow,我试图将文档上的代码从在交互式 session 中运行改为在常规 session 中运行,这样我就可以运行包含命令代码的python文件线。相关的tensorflow代码在这里:https://www.tensorflow.org/versions/master/tutorials/mnist/pros/index.html

这是我的代码:

import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
def train():
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
for i in range(1000):
batch = mnist.train.next_batch(50)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})

def test():
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

with tf.Session() as sess:
sess.run(train())
sess.run(test())

但是,当我尝试运行代码时收到错误消息:

Traceback (most recent call last):
File "tensorflow_mnist.py", line 15, in <module>
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1267, in run
_run_using_default_session(self, feed_dict, self.graph, session)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2763, in _run_using_default_session
session.run(operation, feed_dict)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 345, in run
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 419, in _do_run
e.code)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value Variable
[[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_0, Variable)]]
Caused by op u'MatMul', defined at:
File "tensorflow_mnist.py", line 10, in <module>
y = tf.nn.softmax(tf.matmul(x,W) + b)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 781, in matmul
name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 600, in _mat_mul
transpose_b=transpose_b, name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1710, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 988, in __init__
self._traceback = _extract_stack()

我不知道是什么导致了这个问题;很可能是 session 的不正确使用。有人可以帮我吗?谢谢。

最佳答案

要使这项工作正常进行,您只需更改两件事:

  1. 在运行第一个训练步骤之前初始化变量。

    init_op = tf.initialize_all_variables()
    init_op.run()
    for i in range(1000):
    # …

    这将修复您看到的第一个错误,并且是任何使用变量的 TensorFlow 程序中重要的第一步。

  2. with tf.Session() as sess: block 中内联 train()eval() 的主体.您的 eval() 函数使用来自 train() 的局部变量,因此代码不是有效的 Python 编写。 (请注意,train()eval() 周围的 sess.run() 也不正确——这些函数没有返回值值,因此这相当于调用 sess.run(None),这将引发错误。)

关于python - 将带有交互式 session 的 Tensorflow MNIST 代码更改为 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34442420/

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