AttributeError:模块'tensorflow'没有属性'reset_default_graph'

问题描述 投票:7回答:6

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

在我的文件名cartpole.py我导入了tensorflow

 import tensorflow as tf  

并使用它:

 tf.reset_default_graph()

试图在PyCharm中运行我的项目我收到此错误:

in <module>
tf.reset_default_graph()
AttributeError: module 'tensorflow' has no attribute 'reset_default_graph'

我该如何解决这个错误?

python tensorflow pycharm
6个回答
5
投票

你通常通过写作导入tensorflow

import tensorflow as tf

您可能已在项目tensorflow.py中命名了一个文件,而import语句正在从此文件导入。

或者,你可以尝试这个,

from tensorflow.python.framework import ops
ops.reset_default_graph()

5
投票

将导入更改为tensorflow.keras例如From keras import Sequential to From tensorflow.keras import Sequential


1
投票

更改:

import keras.<something>.<something>

至:

import tensorflow.keras.<something>.<something>

哪里'某事'是您要导入的模块


0
投票

这也可能导致您在错误的环境中运行代码。

我在~/tensorflow virtualenv中安装了tensorflow-gpu。

我可以使用source ./tensorflow/bin/activate在env中运行python3 code.py.

但是当我在没有virtualenv的情况下在env ~中运行python3 code.py时,我有时会遇到像

AttributeError:模块'tensorflow'没有属性'reset_default_graph'

要么

AttributeError:模块'tensorflow'没有属性'Session'

和其他一些人


0
投票

而不是直接从keras导入

来自keras.layers导入输入

从tensorflow导入

来自tensorflow.keras.layers导入输入

我两次得到这个问题,上面的问题解决了我的问题


0
投票

下载TensorFlow的二进制版本解决了我的问题。

$ pip install --ignore-installed --upgrade "<URL>"

从下面根据您的系统选择正确的二进制URL。 https://github.com/lakshayg/tensorflow-build

© www.soinside.com 2019 - 2024. All rights reserved.