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

PythonTensorflowPycharm

Python Problem Overview


I have installed tensorflow version r0.11.

In my file name cartpole.py I have imported tensorflow:

 import tensorflow as tf  

and use it:

 tf.reset_default_graph()

Trying to run my project in PyCharm I get this error:

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

How can I fix this error?

Python Solutions


Solution 1 - Python

This function is deprecated. Use tf.compat.v1.reset_default_graph() instead.

Update This is not the only function to be out of date. Check out this answer for release notes and a conversion script.

Solution 2 - Python

You normally import tensorflow by writing,

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.

Alternatively, you can try this,

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

Solution 3 - Python

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()

Solution 4 - Python

Actually, this answer will resolve all TF 1.x related issues.

Get TF 1.x like behaviour in TF 2.0 by using this:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

Solution 5 - Python

Change your import to tensorflow.keras For example From keras import Sequential to From tensorflow.keras import Sequential

Solution 6 - Python

Change:

import keras.<something>.<something>

to:

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

Where 'something' is the module you want to import

Solution 7 - Python

I am adding this text, so that, people like me - who might have old code from 2018, failing with tensorflow latest version.

My situation was that, in 2018, the versions being used were 1.x The latest, as of writing this post , is 2.x

So, when I ran the code stored in google colab, it actually failed with the error that tensorflow.contrib module not found

For this, you can do the following magic mentioned in :

https://colab.research.google.com/notebooks/tensorflow_version.ipynb#scrollTo=NeWVBhf1VxlH

Basically in your jupyter notebook cell, just run in a separate cell at the top

%tensorflow_version 1.x

This will switch your tensorflow version to 1.15.2 I guess

And then your old code will still work like a charm :)

Solution 8 - Python

This also may caused you run your code in the wrong environment.

I install tensorflow-gpu in my ~/tensorflow virtualenv.

I can run the python3 code.py in the env with source ./tensorflow/bin/activate

But whenI ran python3 code.py in the env ~ without virtualenv, I sometimes may came to issues like

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

or > AttributeError: module 'tensorflow' has no attribute 'Session'

and some others

Solution 9 - Python

Instead of importing directly from keras

from keras.layers import Input

Import from tensorflow

from tensorflow.keras.layers import Input

I got this issue twice and the above one solved my issue

Solution 10 - Python

Downloading binary version of TensorFlow solved my problem.

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

Select right binary URL according to your system from below.
https://github.com/lakshayg/tensorflow-build

Solution 11 - Python

If you are using tf 2.0 beta make sure that all your keras imports are tensorflow.keras... any keras imports will pickup the standard keras package that assumes tensorflow 1.4.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, InputLayer 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmagnpView Question on Stackoverflow
Solution 1 - PythonShoval SaddeView Answer on Stackoverflow
Solution 2 - PythonmartianwarsView Answer on Stackoverflow
Solution 3 - PythonBhadru BhukyaView Answer on Stackoverflow
Solution 4 - PythonHimanshuView Answer on Stackoverflow
Solution 5 - PythonChinmayView Answer on Stackoverflow
Solution 6 - PythonantonioView Answer on Stackoverflow
Solution 7 - Pythona3.14_InfinityView Answer on Stackoverflow
Solution 8 - PythonLF00View Answer on Stackoverflow
Solution 9 - PythonSumanth MeenanView Answer on Stackoverflow
Solution 10 - Python0x01hView Answer on Stackoverflow
Solution 11 - Pythonroshandeep singhView Answer on Stackoverflow