python - 获取属性错误: __exit__ when working with Tensorflow and MNIST data

标签 python tensorflow mnist

我是 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/

相关文章:

python - 使用 Pygame 让子弹以一定速度跟随移动物体?

Python 多类

python - 用scrapy下载整页

python - 无效类型,必须是字符串或 Tensor [TensorFlow]

python - 导入 MNIST 数据集时出现 IOError

python - 在 Python Tkinter 中创建模态对话框是否需要 wait_window()?

tensorflow - 与 TensorFlow/cuDNN 中的 NHWC 相比,NCHW 快多少?

python - Tensorflow 只能看到 XLA_GPU 而不能使用它们

tensorflow - MNIST 样本中所有权重均为零(不变)

dataset - 如何在caffe中将任意数据集转换为siamese网络数据集格式?