Python 在教程中发现TensorFlow错误
我敢问吗?在这一点上,这是一项如此新的技术,以至于我无法找到解决这个看似简单的错误的方法。我要看的教程可以在这里找到- 我把所有的代码复制粘贴到IPython笔记本上,在最后一段代码中我得到了一个错误Python 在教程中发现TensorFlow错误,python,tensorflow,Python,Tensorflow,我敢问吗?在这一点上,这是一项如此新的技术,以至于我无法找到解决这个看似简单的错误的方法。我要看的教程可以在这里找到- 我把所有的代码复制粘贴到IPython笔记本上,在最后一段代码中我得到了一个错误 # 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 th
# 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})
运行此代码后,我收到此错误
---------------------------------------------------------------------------
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)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
15
16打印“测试精度%g”%accurity.eval(进给指令={
--->17 x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
/eval中的root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc(self,feed_dict,session)
403
404 """
-->405使用默认会话返回评估会话(self、feed、dict、self.graph、session)
406
407
/root/anaconda/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in使用默认会话(张量、提要、图表、会话)
2712会话=获取默认会话()
2713如果会话为无:
->2714 raise VALUERROR(“无法使用eval()计算张量:无默认值”
2715“会话已注册。请使用“with”
2716“DefaultSession(sess)”或将显式会话传递给
ValueError:无法使用eval()计算tensor:未注册默认会话。请使用“与DefaultSession(sess)”或将显式会话传递给eval(session=sess)
我想我可能需要通过conda install安装或重新安装TensorFlow,但conda甚至不知道如何安装它
有人知道如何解决这个错误吗?我找到了答案。正如您在值错误中看到的,它说没有注册默认会话。使用“with DefaultSession(sess)”或将显式会话传递给eval(session=sess)所以我想到的答案是向eval传递一个显式会话,正如它所说的。这里是我进行更改的地方
if i%100 == 0:
train_accuracy = accuracy.eval(session=sess, feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
及
现在代码运行正常。我在尝试一个简单的tensorflow示例时遇到了类似的错误
import tensorflow as tf
v = tf.Variable(10, name="v")
sess = tf.Session()
sess.run(v.initializer)
print(v.eval())
我的解决方案是使用sess.as_default()。例如,我将我的代码更改为以下内容,并且成功了:
import tensorflow as tf
v = tf.Variable(10, name="v")
with tf.Session().as_default() as sess:
sess.run(v.initializer)
print(v.eval())
另一种解决方案是使用InteractiveSession。InteractiveSession和Session之间的区别在于,InteractiveSession将自己设置为默认会话,因此您可以运行()或eval(),而无需显式调用会话
v = tf.Variable(10, name="v")
sess = tf.InteractiveSession()
sess.run(v.initializer)
print(v.eval())
或者您可以只创建session,sess=tf.InteractiveSession,然后删除“session=sess”args,它将使用您默认创建的会话
v = tf.Variable(10, name="v")
sess = tf.InteractiveSession()
sess.run(v.initializer)
print(v.eval())