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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:59:36  来源:igfitidea点击:

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

pythontensorflowpycharm

提问by magnp

I have installed tensorflow version r0.11.

我已经安装了 tensorflow 版本 r0.11。

In my file name cartpole.pyI 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 tensorflowby writing,

您通常tensorflow通过写作导入,

import tensorflow as tf

It's possible that you have named a file in your project tensorflow.pyand the importstatement 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 ~/tensorflowvirtualenv.

我在我的~/tensorflowvirtualenv 中安装了 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

和其他一些