69

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?

2
  • Could you provide some more code? I've got a feeling you might not have imported things correctly Commented Nov 24, 2016 at 9:27
  • Or perhaps you may have named a file tensorflow.py in the project Commented Nov 24, 2016 at 9:32

11 Answers 11

95

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.

Sign up to request clarification or add additional context in comments.

2 Comments

that is exactly what the accepted answer says, so your answer is redundant
Yeah you are right the accepted answer is not actually addressing the problem this is the actual answer
36

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

7 Comments

I tried this your tip. But this occured: Command "python setup.py egg_info" failed with error code 1 I've used: pip install --upgrade setuptools or easy_install -U setuptools but nothing helped.
my file name is cartpole.py I've imported: import tensorflow as tf and use tf.reset_default_graph()
You should add these details to your question. I think it's a PyCharm specific issue. Have you tried opening a python terminal in a different folder and typing import tensorflow as tf; tf.reset_default_graph() ?
i've solved an issue, thanks! the problem was in python version. it was running 2.7 by default, when my project was running on version 3.5.2 it works well when you run project like python3 and then use tensoreflow, also i made python version 3.5.2 by default
@magnp I would suggest that you write your own answer for that. Using Python 3 helped me too.
|
9

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

Comments

9

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

Comments

5

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

Comments

5

Change:

import keras.<something>.<something>

to:

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

Where 'something' is the module you want to import

Comments

2

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

Comments

0

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

Comments

0

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

1 Comment

How does this answer the question?
0

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

1 Comment

Thanks! This solution of upgrading tensorflow removed the error for me.. But I have 'uninstall'ed and re-'install'ed instead of --upgrade option.
0

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 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.