66

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?

jwpfox
  • 5,124
  • 11
  • 45
  • 42
magnp
  • 663
  • 1
  • 5
  • 6

12 Answers12

89

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.

Shoval Sadde
  • 1,152
  • 1
  • 11
  • 11
35

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()
martianwars
  • 6,380
  • 5
  • 35
  • 44
  • 1
    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. – magnp Nov 24 '16 at 11:25
  • my file name is *cartpole.py* I've imported: *import tensorflow as tf* and use *tf.reset_default_graph()* – magnp Nov 24 '16 at 11:26
  • 1
    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()` ? – martianwars Nov 24 '16 at 11:52
  • 2
    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 Nov 27 '16 at 21:00
  • @magnp I would suggest that you write your own answer for that. Using Python 3 helped me too. – Simon Forsberg Mar 16 '19 at 14:57
  • This suggestion didn't work for me on my colab session, since I already was using python3, but the suggestions below with `from tensorflow.keras import ...` worked – NeStack Aug 12 '19 at 14:16
  • This is a more sensible way of doing it rather than reverting the compatibility with version 1 – Ehsan Shaghaei Sep 21 '22 at 11:14
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()
Zoe
  • 27,060
  • 21
  • 118
  • 148
Bhadru Bhukya
  • 369
  • 3
  • 3
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()
Himanshu
  • 106
  • 1
  • 3
5

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

Chinmay
  • 147
  • 2
  • 8
5

Change:

import keras.<something>.<something>

to:

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

Where 'something' is the module you want to import

antonio
  • 477
  • 7
  • 18
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 :)

a3.14_Infinity
  • 5,653
  • 7
  • 42
  • 66
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

LF00
  • 27,015
  • 29
  • 156
  • 295
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

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

0x01h
  • 843
  • 7
  • 13
  • Thanks! This solution of upgrading tensorflow removed the error for me.. But I have 'uninstall'ed and re-'install'ed instead of --upgrade option. – Loganathan Aug 05 '19 at 05:10
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 
0

Quick answer:

Replace

import tensorflow as tf

By

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
myworldbox
  • 361
  • 6
  • 11