magnp
magnp

Reputation: 693

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

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?

Upvotes: 69

Views: 151986

Answers (11)

Himanshu
Himanshu

Reputation: 106

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

Upvotes: 9

roshandeep singh
roshandeep singh

Reputation: 565

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 

Upvotes: 0

a3.14_Infinity
a3.14_Infinity

Reputation: 5861

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

Upvotes: 2

Shoval Sadde
Shoval Sadde

Reputation: 1202

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.

Upvotes: 93

Bhadru Bhukya
Bhadru Bhukya

Reputation: 369

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

Upvotes: 9

0x01h
0x01h

Reputation: 925

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

Upvotes: 0

Sumanth Meenan
Sumanth Meenan

Reputation: 1

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

Upvotes: 0

antonio
antonio

Reputation: 487

Change:

import keras.<something>.<something>

to:

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

Where 'something' is the module you want to import

Upvotes: 5

Chinmay
Chinmay

Reputation: 145

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

Upvotes: 5

LF-DevJourney
LF-DevJourney

Reputation: 28554

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

Upvotes: 0

martianwars
martianwars

Reputation: 6500

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

Upvotes: 36

Related Questions