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?
11 Answers
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.
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()
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()
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()
Change your import to tensorflow.keras For example From keras import Sequential to From tensorflow.keras import Sequential
Change:
import keras.<something>.<something>
to:
import tensorflow.keras.<something>.<something>
Where 'something' is the module you want to import
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 :
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 :)
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
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
Downloading binary version of TensorFlow solved my problem.
$ pip install --ignore-installed --upgrade "<URL>"
Select right binary URL according to your system from below.
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