报错类型:
AttributeError module 'tensorflow' has no attribute 'get_default_graph'
复现代码的时候,每次运行到创建序列容器 model = Sequential()
,总是会报这个错误。触发这个错误的代码是: tf.get_default_graph()
。这个问题的原因就是:版本升级导致该方法被弃用。
一、尝试过的解决方法
- 将
tf.get_default_graph()
改为tf.compat.v1.get_default_graph()
。 - 将所有
import keras
改为import tensorflow.keras
。 - 降低tensorflow的版本,从
2.x
降回1.x
版本。问题又双叒叕来了…python3.8
只能装2.x本版…
二、解决方法
- 创建一个
python <3.8
的虚拟环境。 - 安装
1.x
版本的tensorflow。 - 重装其他需要的包。
具体操作命令是:
- 创建一个
python <3.8
的虚拟环境。
conda create -n your_env_name python=3.6
- 激活并进入该环境。
activate your_env_name
- 安装
1.x
版本的tensorflow。
pip install tensorflow==1.15.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
- 查看tensorflow安装情况。
conda list
okk,经过以上步骤,你将获得一个python3.6,tensorflow1.15版本的新环境,可畅通无阻地运行~