Python AttributeError: 模块“tensorflow”没有属性“reset_default_graph”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40782271/
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
AttributeError: module 'tensorflow' has no attribute 'reset_default_graph'
提问by magnp
I have installed tensorflow version r0.11.
我已经安装了 tensorflow 版本 r0.11。
In my file name cartpole.py
I have imported tensorflow
:
在我的文件名中,cartpole.py
我已导入tensorflow
:
import tensorflow as tf
and use it:
并使用它:
tf.reset_default_graph()
Trying to run my project in PyCharm I get this error:
尝试在 PyCharm 中运行我的项目时出现此错误:
in <module>
tf.reset_default_graph()
AttributeError: module 'tensorflow' has no attribute 'reset_default_graph'
How can I fix this error?
我该如何解决这个错误?
采纳答案by martianwars
You normally import tensorflow
by writing,
您通常tensorflow
通过写作导入,
import tensorflow as tf
It's possible that you have named a file in your project tensorflow.py
and the import
statement is importing from this file.
您可能在项目中命名了一个文件,tensorflow.py
并且import
语句是从该文件导入的。
Alternatively, you can try this,
或者,你可以试试这个,
from tensorflow.python.framework import ops
ops.reset_default_graph()
回答by Shoval Sadde
This function is deprecated.
Use tf.compat.v1.reset_default_graph()
instead.
此功能已弃用。使用tf.compat.v1.reset_default_graph()
来代替。
UpdateThis is not the only function to be out of date. Check out this answerfor release notes and a conversion script.
更新这不是唯一过时的功能。查看此答案以获取发行说明和转换脚本。
回答by Bhadru Bhukya
I have tried and successfully removed the attribute error
我已经尝试并成功删除了属性错误
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
classifier = Sequential()
回答by Chinmay
Change your import to tensorflow.keras For example From keras import Sequential to From tensorflow.keras import Sequential
将您的导入更改为 tensorflow.keras 例如 From keras import Sequential to From tensorflow.keras import Sequential
回答by antonio
Change:
改变:
import keras.<something>.<something>
to:
到:
import tensorflow.keras.<something>.<something>
Where 'something' is the module you want to import
'something' 是你想要导入的模块
回答by 0x01h
Downloading binary version of TensorFlow solved my problem.
下载二进制版本的 TensorFlow 解决了我的问题。
$ pip install --ignore-installed --upgrade "<URL>"
Select right binary URL according to your system from below.
https://github.com/lakshayg/tensorflow-build
根据您的系统从下面选择正确的二进制 URL。
https://github.com/lakshayg/tensorflow-build
回答by Sumanth Meenan
Instead of importing directly from keras
而不是直接从 keras 导入
from keras.layers import Input
从 keras.layers 导入输入
Import from tensorflow
从张量流导入
from tensorflow.keras.layers import Input
从 tensorflow.keras.layers 导入输入
I got this issue twice and the above one solved my issue
我两次遇到这个问题,上面的一个解决了我的问题
回答by Kris Roofe
This also may caused you run your code in the wrong environment.
这也可能导致您在错误的环境中运行代码。
I install tensorflow-gpu in my ~/tensorflow
virtualenv.
我在我的~/tensorflow
virtualenv 中安装了 tensorflow-gpu 。
I can run the python3 code.py in the env with source ./tensorflow/bin/activate
我可以在 env 中运行 python3 code.py source ./tensorflow/bin/activate
But whenI ran python3 code.py in the env ~
without virtualenv, I sometimesmay came to issues like
但是当我在~
没有 virtualenv 的环境中运行 python3 code.py 时,我有时可能会遇到类似的问题
AttributeError: module 'tensorflow' has no attribute 'reset_default_graph'
AttributeError: 模块“tensorflow”没有属性“reset_default_graph”
or
或者
AttributeError: module 'tensorflow' has no attribute 'Session'
AttributeError: 模块“tensorflow”没有属性“Session”
and some others
和其他一些