python - Tensorflow 2.0 - AttributeError:模块'tensorflow'没有属性'Session'
问题描述
当我sess = tf.Session()
在 Tensorflow 2.0 环境中执行命令时,我收到如下错误消息:
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 安装)
重现步骤:
安装:
- 点安装——升级点
- pip install tensorflow==2.0.0-alpha0
- 点安装 keras
- pip install numpy==1.16.2
执行:
- 执行命令:import tensorflow as tf
- 执行命令: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 的行为,可以运行
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
但随后无法从 TF 2.0 中的许多改进中受益。有关更多详细信息,请参阅迁移指南 https://www.tensorflow.org/guide/migrate
推荐阅读
- sql - ORACLE 获取第一条记录
- python - 密码学python包中的Fernet解密错误
- reactjs - 将数据从 Firebase 实时数据库加载到 React 钩子组件
- java - 将类打包在 jar 中时 Spring-boot @ComponentScan 不起作用,未打包类时工作正常
- swift - 在没有 FileManager 的情况下将 UIDocument 保存到新 URL 时如何防止文件名冲突
- reactjs - 在 useEffect 中反应 setInterval
- algorithm - 根据内核扩展 CPU 小时数
- solidity - 当您将合约存储在映射中时,实际存储了什么?
- python - pygame 中有多个动画?
- javascript - 如何删除 JS var 中的空格?