Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow 2.0-属性错误:模块';tensorflow';没有属性';会议';_Python_Tensorflow_Keras_Tensorflow2.0 - Fatal编程技术网

Python Tensorflow 2.0-属性错误:模块';tensorflow';没有属性';会议';

Python Tensorflow 2.0-属性错误:模块';tensorflow';没有属性';会议';,python,tensorflow,keras,tensorflow2.0,Python,Tensorflow,Keras,Tensorflow2.0,在Tensorflow 2.0环境中执行命令sess=tf.Session() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'tensorflow' has no attribute 'Session' 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 AttributeError:模块“tensorfl

在Tensorflow 2.0环境中执行命令
sess=tf.Session()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:模块“tensorflow”没有属性“Session”
系统信息:

  • 操作系统平台和发行版:Windows 10
  • Python版本:3.7.1
  • Tensorflow版本:2.0.0-alpha0(与pip一起安装)
复制步骤:

安装:

  • pip安装--升级pip
  • pip安装tensorflow==2.0.0-alpha0
  • pip安装keras
  • pip安装numpy==1.16.2
  • 执行:

  • 执行命令:将tensorflow作为tf导入
  • 执行命令:sess=tf.Session()

  • 根据TF 1:1符号映射图,在TF 2.0中,您应该使用
    TF.compat.v1.Session()
    而不是
    TF.Session()

    要在TF2.0中获得类似TF1.x的行为,可以运行

    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    
    但是,我们不能从TF2.0的许多改进中获益。有关更多详细信息,请参阅迁移指南

    在安装了
    windows10+python3.7(64位)+anacconda3+jupyter笔记本之后,我第一次尝试python时遇到了这个问题。

    我通过引用“”解决了此问题

    我同意

    我相信TF2.0已经删除了“Session()”

    我插入了两行。一个是
    tf.compat.v1.disable\u eager\u execution()
    ,另一个是
    sess=tf.compat.v1.Session()

    我的Hello.py如下:

    将tensorflow导入为tf
    tf.compat.v1.disable_eager_execution()
    hello=tf.constant('hello,TensorFlow!')
    sess=tf.compat.v1.Session()
    打印(sess.run(hello))
    
    如果这是您的代码,正确的解决方案是重写它,使其不使用
    会话()
    ,因为在TensorFlow 2中不再需要它

    如果这只是您正在运行的代码,您可以通过运行

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

    (或不管是什么)

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

    TF1.x hello world:

    import tensorflow as tf
    msg = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(msg))
    
    import tensorflow as tf
    msg = tf.constant('Hello, TensorFlow!')
    tf.print(msg)
    
    TF2.x hello world:

    import tensorflow as tf
    msg = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(msg))
    
    import tensorflow as tf
    msg = tf.constant('Hello, TensorFlow!')
    tf.print(msg)
    
    有关更多信息,请参见使用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
    

    对于
    TF2.x
    ,您可以这样做

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

    >>b'hello world

    tfv2.0支持v1.0的即时模式vis-a-vis图形模式。因此,v2.0不支持tf.session()。因此,我建议您重写代码,使其在急切模式下工作。

    试试这个

    import tensorflow as tf
    
    tf.compat.v1.disable_eager_execution()
    
    hello = tf.constant('Hello, TensorFlow!')
    
    sess = tf.compat.v1.Session()
    
    print(sess.run(hello))
    

    默认情况下,Tensorflow 2.x支持即时执行,因此不支持会话。

    将Tensorflow作为tf导入
    sess=tf.Session()
    
    此代码将在版本2.x上显示属性错误

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

    试试这个

    import tensorflow as tf
    
    tf.compat.v1.disable_eager_execution()
    
    hello = tf.constant('Hello, TensorFlow!')
    
    sess = tf.compat.v1.Session()
    
    print(sess.run(hello))
    
    将tensorflow.compat.v1导入为tf
    sess=tf.Session()
    
    同样的问题也发生在我身上

    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()

    替换它。在更新Windows10后,我第一次尝试Google Colab时也遇到了同样的问题。然后我换了两行

    • tf.compat.v1.禁用执行()
    • sess=tf.compat.v1.Session()
    因此,一切正常

    使用以下方法:

    sess = tf.compat.v1.Session()
    
    如果出现错误,请使用以下命令

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

    如果你在做这件事的时候

    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不支持会话。对此有单独的理解,在TensorFlow中已经提到,链接为:



    此链接中还提到了其他主要的TF2更改,虽然很长,但请仔细查看,并使用Ctrl+F获得帮助。链接,


    奇怪。我认为这不是由于TF版本,但完整的TF安装已被破坏。请参见TensorFlow 2.0的工作原理。我认为最初的想法是保持
    tf.Session
    ,至少在开始时是这样,但现在看来它似乎已经被完全删除了。哦,看起来你仍然可以通过它访问它。@Dmytropylipko我在创建这个问题之前试过了。我宁愿说在TF2.0中,
    Session()
    已被移动,而不是删除。使用
    Session()
    的需要已被删除。使用
    import tensorflow.compat.v1作为tf tf.disable\u v2\u behavior()
    会给我一个错误
    AttributeError:模块“tensorflow\u core.compat.v1”没有属性“contrib”
    在tf 2.0迁移文档中发现了这一点
    仍然可以运行1.X代码,未经修改(供款除外),在TensorFlow 2.0中
    当你得到
    TensorFlow_核心
    没有属性错误时,你使用哪个TF版本?我下载了一些笔记本,我面临着这些问题,正如回答中提到的,在顶部导入语句帮助我摆脱了恼人的错误。我如何在T中评估静态
    .pb
    图形F2然后?只有通过使用tf1功能,如
    tf.compat.v1.Session()
    。在TF2中,您应该始终使用渴望模式,并且没有
    .pb
    ?在
    1.15.x
    之后,不应该有其他