【问题标题】:Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'
【发布时间】:2019-08-04 04:37:38
【问题描述】:

当我在 Tensorflow 2.0 环境中执行命令 sess = tf.Session() 时,我收到如下错误消息:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'

系统信息:

  • 操作系统平台和分发:Windows 10
  • Python 版本:3.7.1
  • Tensorflow 版本:2.0.0-alpha0(使用 pip 安装)

重现步骤:

安装:

  1. pip install --upgrade pip
  2. pip install tensorflow==2.0.0-alpha0
  3. 点安装 keras
  4. pip install numpy==1.16.2

执行:

  1. 执行命令:import tensorflow as tf
  2. 执行命令:sess = tf.Session()

【问题讨论】:

标签: python tensorflow keras tensorflow2.0


【解决方案1】:

根据TF 1:1 Symbols Map,在TF 2.0 中你应该使用tf.compat.v1.Session() 而不是tf.Session()

https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJIteQ/edit#gid=0

要在 TF 2.0 中获得类似 TF 1.x 的行为,可以运行

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

但随后无法从 TF 2.0 中的许多改进中受益。有关详细信息,请参阅迁移指南 https://www.tensorflow.org/guide/migrate

【讨论】:

  • 使用import tensorflow.compat.v1 as tf tf.disable_v2_behavior() 给我一个错误AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'
  • 在 TF 2.0 迁移文档It is still possible to run 1.X code, unmodified (except for contrib), in TensorFlow 2.0中找到了这个
  • 收到tensorflow_core has no attribute 错误时,您使用的是哪个TF版本?
  • 我已经下载了一些笔记本,但我遇到了这些问题,在答案中提到的顶部导入语句帮助我摆脱了恼人的错误。
  • 我如何评估 TF2 中的静态 .pb 图形呢?只有通过使用像tf.compat.v1.Session() 这样的tf1-feature。在 TF2 中,您应该始终使用渴望模式,而不是 .pb
【解决方案2】:

TF2 默认运行 Eager Execution,因此不再需要 Session。如果要运行静态图,更合适的方法是在 TF2 中使用tf.function()。虽然在 TF2 中仍然可以通过tf.compat.v1.Session() 访问 Session,但我不鼓励使用它。通过比较 hello worlds 中的差异来证明这种差异可能会有所帮助:

TF1.x 你好世界:

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(msg))

TF2.x 你好世界:

import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
tf.print(msg)

欲了解更多信息,请参阅Effective TensorFlow 2

【讨论】:

  • TF2有非eager模式吗?还是急切模式只是建议的执行模式?如果我想在 TF2 中有静态 .pb 文件怎么办?可能吗?那么如何在 TF2 中评估它?
【解决方案3】:

我在安装windows10 + python3.7(64bit) + anacconda3 + jupyter notebook.后第一次尝试python时遇到了这个问题

我通过引用“https://vispud.blogspot.com/2019/05/tensorflow200a0-attributeerror-module.html”解决了这个问题

我同意

我相信“Session()”已在 TF 2.0 中删除。

我插入了两行。一个是tf.compat.v1.disable_eager_execution(),另一个是sess = tf.compat.v1.Session()

我的Hello.py如下:

import tensorflow as tf

tf.compat.v1.disable_eager_execution()

hello = tf.constant('Hello, TensorFlow!')

sess = tf.compat.v1.Session()

print(sess.run(hello))

【讨论】:

  • 我宁愿说在 TF 2.0 中 Session() 已被移动而不是被删除。 需要使用 Session() 已被删除。
【解决方案4】:

对于TF2.x,你可以这样做。

import tensorflow as tf
with tf.compat.v1.Session() as sess:
    hello = tf.constant('hello world')
    print(sess.run(hello))

&gt;&gt;&gt; b'hello world

【讨论】:

    【解决方案5】:

    如果这是您的代码,正确的解决方案是将其重写为不使用 Session(),因为在 TensorFlow 2 中不再需要这样做

    如果这只是你正在运行的代码,你可以通过运行降级到 TensorFlow 1

    pip3 install --upgrade --force-reinstall tensorflow-gpu==1.15.0 
    

    (或latest version of TensorFlow 1 是什么)

    【讨论】:

    • 1.15.x之后应该没有其他的1.x版本的TF,除非会出现一些补丁,但没有改进。
    【解决方案6】:

    Tensorflow 2.x 默认支持 Eager Execution,因此不支持 Session。

    【讨论】:

      【解决方案7】:
      import tensorflow as tf
      sess = tf.Session()
      

      此代码将在版本 2.x 上显示属性错误

      在版本 2.x 中使用版本 1.x 代码

      试试这个

      import tensorflow.compat.v1 as tf
      sess = tf.Session()
      

      【讨论】:

        【解决方案8】:

        我在更新 Windows 10 后第一次尝试 Google Colab 时也遇到了同样的问题。然后我改了,插入了两行,

        • tf.compat.v1.disable_eager_execution()
        • sess = tf.compat.v1.Session()

        结果一切顺利

        【讨论】:

          【解决方案9】:
          import tensorflow._api.v2.compat.v1 as tf
          tf.disable_v2_behavior()
          

          【讨论】:

            【解决方案10】:

            使用 Anaconda + Spyder (Python 3.7)

            [代码]

            import tensorflow as tf
            valor1 = tf.constant(2)
            valor2 = tf.constant(3)
            type(valor1)
            print(valor1)
            soma=valor1+valor2
            type(soma)
            print(soma)
            sess = tf.compat.v1.Session()
            with sess:
                print(sess.run(soma))
            

            [控制台]

            import tensorflow as tf
            valor1 = tf.constant(2)
            valor2 = tf.constant(3)
            type(valor1)
            print(valor1)
            soma=valor1+valor2
            type(soma)
            Tensor("Const_8:0", shape=(), dtype=int32)
            Out[18]: tensorflow.python.framework.ops.Tensor
            
            print(soma)
            Tensor("add_4:0", shape=(), dtype=int32)
            
            sess = tf.compat.v1.Session()
            
            with sess:
                print(sess.run(soma))
            5
            

            【讨论】:

              【解决方案11】:

              TF v2.0 支持 Eager 模式相对于 v1.0 的 Graph 模式。因此,v2.0 不支持 tf.session()。因此,建议您重写代码以在 Eager 模式下工作。

              【讨论】:

              • TF2 是否支持非急切模式?还是非急切只是 tf1 功能?那么如何评估 tf2 中的.pb 图表呢?
              【解决方案12】:

              我也遇到了同样的问题

              import tensorflow as tf
              hello = tf.constant('Hello World ') 
              sess = tf.compat.v1.Session()    *//I got the error on this step when I used 
                                                 tf.Session()*
              sess.run(hello)
              

              尝试用tf.compact.v1.Session()替换它

              【讨论】:

                【解决方案13】:

                使用这个:

                sess = tf.compat.v1.Session()
                

                如果有错误,使用下面的

                tf.compat.v1.disable_eager_execution()
                sess = tf.compat.v1.Session()
                

                【讨论】:

                  【解决方案14】:

                  如果您在某些导入时这样做,

                  from keras.applications.vgg16 import VGG16
                  from keras.preprocessing import image
                  from keras.applications.vgg16 import preprocess_input
                  import numpy as np
                  

                  那么我建议您按照以下步骤操作,
                  注意:仅适用于 TensorFlow2 和 CPU 进程
                  第 1 步:告诉您的代码就像编译器是 TF1 一样并禁用 TF2 行为,请使用以下代码:

                  import tensorflow as tf
                  import tensorflow.compat.v1 as tf
                  tf.disable_v2_behavior()
                  

                  第 2 步:在导入库时,提醒您的代码必须像 TF1 一样运行,每次都是这样。

                  tf.disable_v2_behavior()
                  from keras.applications.vgg16 import VGG16
                  from keras.preprocessing import image
                  from keras.applications.vgg16 import preprocess_input
                  import numpy as np
                  

                  结论:这应该可行,如果出现问题,请告诉我,如果是 GPU,请提及为 keras 添加后端代码。另外,TF2 不支持 session 有一个单独的理解,在 TensorFlow 上已经提到,链接是:

                  TensorFlow Page for using Sessions in TF2

                  此链接中提到了其他主要的 TF2 更改,它很长,但请仔细阅读,使用 Ctrl+F 寻求帮助。链接,

                  Effective TensorFlow 2 Page Link

                  【讨论】:

                    【解决方案15】:

                    对于 TensorFlow 2.0 及更高版本,试试这个。

                    import tensorflow as tf
                    
                    tf.compat.v1.disable_eager_execution()
                    
                    a = tf.constant(5)
                    b = tf.constant(6)
                    c = tf.constant(7)
                    d = tf.multiply(a,b)
                    e = tf.add(c,d)
                    f = tf.subtract(a,c)
                    
                    with tf.compat.v1.Session() as sess:
                      outs = sess.run(f)
                      print(outs)
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2020-08-17
                      • 2019-10-05
                      • 2020-12-06
                      • 2017-04-08
                      • 2020-02-04
                      • 2020-02-12
                      • 2019-01-10
                      • 2019-10-01
                      相关资源
                      最近更新 更多