所以,我一直在学习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 <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()
我不知道造成这个问题的原因是什么;最有可能的是会话的使用不正确。有人可以给我一些帮助吗?感谢。
答案 0 :(得分:5)
为了实现这一目标,您只需要更改两件事:
在运行第一个训练步骤之前初始化变量。
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)
,会引发错误。)
答案 1 :(得分:1)
以下代码有效:
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}))