【问题标题】:'tensorflow' has no attribute 'Session' - TensorFlow 2.0“tensorflow”没有属性“会话”-TensorFlow 2.0
【发布时间】:2020-04-07 17:36:26
【问题描述】:

我遇到了 Session 模块的问题。

import tensorflow as tf
n1 = tf.constant(3.0)
n2 = tf.constant(4.0)
print(n1)
sess = tf.Session()
print(sess.run([n1,n2]))

下面是我收到的控制台输出。

runfile('C:/Users/Rushabh Shah/.spyder-py3/temp.py', wdir='C:/Users/Rushabh Shah/.spyder-py3')
tf.Tensor(3.0, shape=(), dtype=float32)
Traceback (most recent call last):

  File "C:\Users\Rushabh Shah\.spyder-py3\temp.py", line 11, in <module>
    sess = tf.Session()

AttributeError: module 'tensorflow' has no attribute 'Session'

【问题讨论】:

  • 看看stackoverflow.com/questions/55142951/…。不确定您的版本是什么,但也许这会有所帮助。
  • 是的,这很有用。但在此之后我收到如下新错误
  • tf.Tensor(3.0, shape=(), dtype=float32)Traceback(最近一次调用最后):文件“C:\Users\Rushabh Shah\.spyder-py3\temp.py” ,第 12 行,在 print(sess.run('Hello')) 文件“G:\Rushabh_Shah\envs\tf_env\lib\site-packages\tensorflow_core\python\client\session.py”中,第 956 行,在运行 run_metadata_ptr) 文件 "G:\Rushabh_Shah\envs\tf_env\lib\site-packages\tensorflow_core\python\client\session.py",第 1105 行,在 _run raise RuntimeError('会话图为空。添加操作到' RuntimeError: 会话图为空。在调用 run() 之前向图中添加操作。

标签: python tensorflow


【解决方案1】:

如果你使用的是 Tensorflow 2.0,你应该使用 sess = tf.compat.v1.Session() 而不是 sess = tf.Session()

【讨论】:

    【解决方案2】:

    sess = tf.Session()改成sess = tf.compat.v1.Session(),就可以了。

    来源:https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多