将带有交互式会话的 Tensorflow MNIST 代码更改为会话

Changing Tensorflow MNIST code with interactive session into session

所以,我一直在学习tensorflow,我试图将文档上的代码从交互式会话中的运行更改为常规会话中的运行,这样我可以 运行 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()

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

您只需更改两点即可完成这项工作:

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

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

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

  2. with tf.Session() as sess: 块中内联 train()eval() 的主体。您的 eval() 函数使用 train() 中的局部变量,因此代码在编写时无效 Python。 (请注意 train()eval() 周围的 sess.run() 也是不正确的——这些函数没有 return 值,所以这等同于调用 sess.run(None) ,这将引发错误。)

以下代码有效:

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

import tensorflow as tf

with tf.Session() as sess:

    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)
    init_op = tf.initialize_all_variables()
    init_op.run()
    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]})

    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}))