在使用Tensorflow和MNIST数据时获取AttributeError:__exit__

时间:2019-07-11 11:43:45

标签: python tensorflow mnist

我是Tensorflow的初学者,正在使用MNIST数据。尝试按以下方式运行Tensorflow会话时,出现属性错误。

有人可以调查吗?

出现错误的代码段如下。


    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__

1 个答案:

答案 0 :(得分:0)

您缺少()来创建新的Session对象/实例:

with tf.Session() as sess:

tf.Session仅指Session类。