Tensorflow 2.0 - AttributeError: 模块“tensorflow"没有属性“Session" [英] Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'

查看:79
本文介绍了Tensorflow 2.0 - AttributeError: 模块“tensorflow"没有属性“Session"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.AttributeError: 模块tensorflow"没有属性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. pip 安装 keras
  4. pip install numpy==1.16.2

<块引用>

执行:

  1. 执行命令:import tensorflow as tf
  2. 执行命令:sess = 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

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

 将 tensorflow.compat.v1 导入为 tftf.disable_v2_behavior()

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

When I am executing the command sess = tf.Session() in Tensorflow 2.0 environment, I am getting an error message as below:

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)

Steps to reproduce:

Installation:

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

Execution:

  1. Execute command: import tensorflow as tf
  2. Execute command: sess = tf.Session()

解决方案

According to TF 1:1 Symbols Map, in TF 2.0 you should use tf.compat.v1.Session() instead of tf.Session()

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

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

这篇关于Tensorflow 2.0 - AttributeError: 模块“tensorflow"没有属性“Session"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆