当前位置:  开发笔记 > 编程语言 > 正文

使用交互式会话将Tensorflow MNIST代码更改为会话

如何解决《使用交互式会话将TensorflowMNIST代码更改为会话》经验,为你挑选了1个好方法。

所以,我一直在学习tensorflow,我试图将文档上的代码从在交互式会话上运行更改为在常规会话中运行,这样我就可以运行包含命令行代码的python文件.相关的张量流代码在这里: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 
    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 
    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> mrry..:

您只需要更改两件事就可以完成这项工作:

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

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

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

    内联的机构train()eval()with tf.Session() as sess:块.您的eval()函数使用局部变量,train()因此代码不是有效的Python编写.(注意sess.run()周围train()eval()不正确,太那些函数没有返回值,所以这相当于调用sess.run(None),这将产生一个错误.)

推荐阅读
家具销售_903
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有