AttributeError: module 'tensorflow' has no attribute 'get_default_graph'

今天我也是郁闷了,好不容易把python3.7里需要的包安装好了,结果运行代码就报上面的错误。

WARNING: Logging before flag parsing goes to stderr.
W0701 11:00:32.062613 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0701 11:00:32.131539 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0701 11:00:32.141761 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

W0701 11:00:32.217406 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

W0701 11:00:32.255942 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:133: The name tf.placeholder_with_default is deprecated. Please use tf.compat.v1.placeholder_with_default instead.

W0701 11:00:32.260227 139666427000576 deprecation.py:506] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
W0701 11:00:32.316102 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

W0701 11:00:32.329323 139666427000576 deprecation_wrapper.py:119] From /home/clh/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3295: The name tf.log is deprecated. Please use tf.math.log instead.

W0701 11:00:32.493103 139666427000576 deprecation.py:323] From /home/clh/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_grad.py:1250: add_dispatch_support..wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.

看来代码有些老,安装的python3.7与最新包不能用,好多需要修改。

为了简单方便,正好这台电脑也有一个python3.5,于是就修改了解释器,用python3.5来运行

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'_第1张图片

但是我用python3.5运行的时候,没安装keras,所以只好再次安装keras,安装好再次运行,结果还是报错:

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'_第2张图片

这是安装的tensorflow版本太高了??

检查一下版本

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'_第3张图片

确实版本挺高的,而写这代码时用keras用的2.2.2,tensorflow为1.9.0

好吧,我只好卸载了该安装包,重新安装特定的包:

卸载的时候,当我用指定路径进行卸载时

sudo pip uninstall --target=/usr/local/lib/python3.5/dist-packages tensorflow

说这种用法不对

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'_第4张图片

被逼无赖,我只好在pycharm中进行卸载

pycharm中file里的setting,如下图所示,选择某个包后,点击右边的减号,输入密码,即可卸载

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'_第5张图片

卸载后,还是按指定路径进行安装,并指定安装版本号

sudo pip install --target=/usr/local/lib/python3.5/dist-packages tensorflow==1.9.0

sudo pip install --target=/usr/local/lib/python3.5/dist-packages keras==2.2.2

安装keras时,说是两个包不兼容

于是我在pycharm中再次卸载这两个包

AttributeError: module 'tensorflow' has no attribute 'get_default_graph'_第6张图片

然后再重新安装keras。终于代码可以跑了!!!

你可能感兴趣的:(linux)