我是 Tensorflow 的初学者,正在使用 MNIST 数据。当尝试运行如下所示的 Tensorflow session 时,我收到属性错误。
有人可以研究一下吗?
我收到错误的代码片段如下。
with tf.Session as sess:
sess.run(init)
for step in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train, feed_dict={x:batch_x, y:batch_y})
# Evaluate the model
matches = tf.equal(tf.argmax(y,1),tf.argmax(y_true,1))
# Output will be like [True, False, True.....] --> Cast to [1.0, 0.0, 1.0.....]
acc = tf.reduce_mean(tf.cast(matches,tf.float32))
print(sess.run(acc, feed_dict={x:mnist.test.images, y_true:mnist.test.labels}))
我收到以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-59-c78b8b9359b3> in <module>()
----> 1 with tf.Session as sess:
2 sess.run(init)
3 for step in range(1000):
4 batch_x, batch_y = mnist.train.next_batch(100)
5 sess.run(train, feed_dict={x:batch_x, y:batch_y})
AttributeError: __exit__
最佳答案
为了创建新的 Session
对象/实例,您缺少 ()
:
将 tf.Session() 作为 sess:
tf.Session
仅引用 Session
类。
关于python - 获取属性错误: __exit__ when working with Tensorflow and MNIST data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56988419/