Python Tensorflow 2.0 - AttributeError: 模块“tensorflow”没有属性“Session”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55142951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'
提问by Atul Kamble
When I am executing the command sess = tf.Session()
in Tensorflow 2.0 environment, I am getting an error message as below:
当我sess = tf.Session()
在 Tensorflow 2.0 环境中执行命令时,我收到如下错误消息:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'tensorflow' has no attribute 'Session'
System Information:
系统信息:
- OS Platform and Distribution: Windows 10
- Python Version: 3.7.1
- Tensorflow Version: 2.0.0-alpha0 (installed with pip)
- 操作系统平台和发行版:Windows 10
- Python 版本:3.7.1
- Tensorflow 版本:2.0.0-alpha0(使用 pip 安装)
Steps to reproduce:
Installation:
重现步骤:
安装:
- pip install --upgrade pip
- pip install tensorflow==2.0.0-alpha0
- pip install keras
- pip install numpy==1.16.2
- pip 安装 --upgrade pip
- 点安装张量流==2.0.0-alpha0
- 点安装keras
- pip 安装 numpy==1.16.2
Execution:
执行:
- Execute command: import tensorflow as tf
- Execute command: sess = tf.Session()
- 执行命令:import tensorflow as tf
- 执行命令:sess = tf.Session()
回答by MP?kalski
According to TF 1:1 Symbols Map
, in TF 2.0 you should use tf.compat.v1.Session()
instead of tf.Session()
根据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
https://docs.google.com/spreadsheets/d/1FLFJLzg7WNP6JHODX5q8BDgptKafq_slHpnHVbJiteQ/edit#gid=0
To get TF 1.x like behaviour in TF 2.0 one can run
要在 TF 2.0 中获得类似 TF 1.x 的行为,可以运行
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
but then one cannot benefit of many improvements made in TF 2.0. For more details please refer to the migration guide https://www.tensorflow.org/guide/migrate
但是,人们无法从 TF 2.0 中的许多改进中受益。有关更多详细信息,请参阅迁移指南 https://www.tensorflow.org/guide/migrate
回答by Wes
TF2 runs Eager Execution by default, thus removing the need for Sessions. If you want to run static graphs, the more proper way is to use tf.function()
in TF2. While Session can still be accessed via tf.compat.v1.Session()
in TF2, I would discourage using it. It may be helpful to demonstrate this difference by comparing the difference in hello worlds:
TF2 默认运行 Eager Execution,因此不需要会话。如果要运行静态图,更合适的方法是tf.function()
在TF2中使用。虽然 Session 仍然可以通过tf.compat.v1.Session()
TF2访问,但我不鼓励使用它。通过比较 hello world 中的差异来证明这种差异可能会有所帮助:
TF1.x hello world:
TF1.x 你好世界:
import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(msg))
TF2.x hello world:
TF2.x 你好世界:
import tensorflow as tf
msg = tf.constant('Hello, TensorFlow!')
tf.print(msg)
For more info, see Effective TensorFlow 2
有关详细信息,请参阅Effective TensorFlow 2
回答by user12217934
I faced this problem when I first tried python after installing windows10 + python3.7(64bit) + anacconda3 + jupyter notebook.
我在安装后第一次尝试 python 时遇到了这个问题 windows10 + python3.7(64bit) + anacconda3 + jupyter notebook.
I solved this problem by refering to "https://vispud.blogspot.com/2019/05/tensorflow200a0-attributeerror-module.html"
我参考“ https://vispud.blogspot.com/2019/05/tensorflow200a0-attributeerror-module.html”解决了这个问题
I agree with
我同意
I believe "Session()" has been removed with TF 2.0.
我相信“Session()”已随 TF 2.0 删除。
I inserted two lines. One is tf.compat.v1.disable_eager_execution()
and the other is sess = tf.compat.v1.Session()
我插入了两行。一个是tf.compat.v1.disable_eager_execution()
,另一个是sess = tf.compat.v1.Session()
My Hello.py is as follows:
我的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))
回答by BlueRaja - Danny Pflughoeft
If this is your code, the correct solution is to rewrite it to not use Session()
, since that's no longer necessary in TensorFlow 2
如果这是您的代码,正确的解决方案是将其重写为不使用Session()
,因为在 TensorFlow 2 中不再需要
If this is just code you're running, you can downgrade to TensorFlow 1 by running
如果这只是您正在运行的代码,您可以通过运行降级到 TensorFlow 1
pip3 install --upgrade --force-reinstall tensorflow-gpu==1.15.0
(or whatever the latest version of TensorFlow 1is)
(或任何最新版本的 TensorFlow 1)
回答by Bandham Manikanta
回答by sergio
Using Anaconda + Spyder (Python 3.7)
使用 Anaconda + Spyder (Python 3.7)
[code]
[代码]
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))
[console]
[安慰]
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