在教程中发现 TensorFlow 错误 [英] TensorFlow Error found in Tutorial
问题描述
我什至敢问?这是一项如此新的技术,我无法找到解决这个看似简单的错误的方法.我要学习的教程可以在这里找到 - http://www.tensorflow.org/tutorials/mnist/pros/index.html#deep-mnist-for-experts
我将所有代码复制并粘贴到 IPython Notebook 中,在最后一段代码中出现错误.
# 为了训练和评估它,我们将使用与上面简单的一层 SoftMax 网络几乎相同的代码.# 不同之处在于:我们将用更复杂的 ADAM 优化器替换最陡峭的梯度下降优化器.cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)正确预测 = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))精度 = tf.reduce_mean(tf.cast(correct_prediction, "float"))sess.run(tf.initialize_all_variables())对于我在范围内(20000):批次 = mnist.train.next_batch(50)如果 i%100 == 0:train_accuracy =accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})打印步骤 %d,训练精度 %g"%(i, train_accuracy)train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})打印测试准确率 %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
运行此代码后,我收到此错误.
---------------------------------------------------------------------------ValueError 回溯(最近一次调用)<ipython-input-46-a5d1ab5c0ca8>在 <module>()1516 打印测试准确率 %g"%accuracy.eval(feed_dict={--->17 x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc 在 eval(self, feed_dict, session)403第404话-->第405回406407/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc 在 _eval_using_default_session(tensors, feed_dict, graph, session)2712 会话 = get_default_session()2713 如果会话是无:->2714 raise ValueError(无法使用 eval() 评估张量:无默认值"2715 "会话已注册.使用 'with "2716DefaultSession(sess)"或将显式会话传递给ValueError:无法使用 eval() 评估张量:未注册默认会话.使用 'with DefaultSession(sess)' 或将显式会话传递给 eval(session=sess)
我认为我可能需要通过 conda install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl 但 conda 甚至不知道如何安装它.>
有人知道如何解决这个错误吗?
我想通了.正如您在值错误中看到的那样,它表示 No default session is registered.使用 'with DefaultSession(sess)' 或将显式会话传递给 eval(session=sess)
所以我想出的答案是将显式会话传递给 eval,就像它说的那样.这是我进行更改的地方.
如果 i%100 == 0:train_accuracy =accuracy.eval(session=sess,feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
和
train_step.run(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
现在代码运行良好.
Dare I even ask? This is such a new technology at this point that I can't find a way to solve this seemingly simple error. The tutorial I'm going over can be found here- http://www.tensorflow.org/tutorials/mnist/pros/index.html#deep-mnist-for-experts
I literally copied and pasted all of the code into IPython Notebook and at the very last chunk of code I get an error.
# To train and evaluate it we will use code that is nearly identical to that for the simple one layer SoftMax network above.
# The differences are that: we will replace the steepest gradient descent optimizer with the more sophisticated ADAM optimizer.
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess.run(tf.initialize_all_variables())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print "step %d, training accuracy %g"%(i, train_accuracy)
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print "test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
After running this code, I receive this error.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-46-a5d1ab5c0ca8> in <module>()
15
16 print "test accuracy %g"%accuracy.eval(feed_dict={
---> 17 x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in eval(self, feed_dict, session)
403
404 """
--> 405 return _eval_using_default_session(self, feed_dict, self.graph, session)
406
407
/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in _eval_using_default_session(tensors, feed_dict, graph, session)
2712 session = get_default_session()
2713 if session is None:
-> 2714 raise ValueError("Cannot evaluate tensor using eval(): No default "
2715 "session is registered. Use 'with "
2716 "DefaultSession(sess)' or pass an explicit session to "
ValueError: Cannot evaluate tensor using eval(): No default session is registered. Use 'with DefaultSession(sess)' or pass an explicit session to eval(session=sess)
I thought that I may need to install or reinstall TensorFlow via conda install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl but conda doesn't even know how to install it.
Does anyone have any idea of how to work around this error?
I figured it out. As you see in the value error, it says No default session is registered. Use 'with DefaultSession(sess)' or pass an explicit session to eval(session=sess)
so the answer I came up with is to pass an explicit session to eval, just like it says. Here is where I made the changes.
if i%100 == 0:
train_accuracy = accuracy.eval(session=sess, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
And
train_step.run(session=sess, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
Now the code is working fine.
这篇关于在教程中发现 TensorFlow 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!