Changing Tensorflow MNIST code with interactive session into session
Question: So, I have been learning tensorflow, and I have tried to change the code on
the documentation from being run on an interactive session to being run in a
regular session, so that I can run the python file containing the code from
command line. The relevant tensorflow code is here:
<https://www.tensorflow.org/versions/master/tutorials/mnist/pros/index.html>

Here is my code:

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

However, I am getting error message when I try to run the code:

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

I have no idea what is causing this problem; most likely it is an incorrect
usage of the session. Could someone give me some help? Thanks.


Answer: There are just two things you need to change to make this work:

  1. Initialise the variables before running the first training steps.
    
        init_op = tf.initialize_all_variables()
    init_op.run()
    for i in range(1000):
        # …
    

This will fix the first error you are seeing, and is an important first step
in any TensorFlow program that uses variables.

  2. Inline the bodies of `train()` and `eval()` in the `with tf.Session() as sess:` block. Your `eval()` function uses local variables from `train()` so the code is not valid Python as written. (Note that the `sess.run()` around `train()` and `eval()` is incorrect too—those functions don't have a return value, so this is equivalent to calling `sess.run(None)`, which will raise an error.)

