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

105,817

Solution 1

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 2

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 3

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

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

Change:

import keras.<something>.<something>

to:

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

Where 'something' is the module you want to import

Share:
105,817
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Admin
    Admin over 7 years
    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.
  • Admin
    Admin over 7 years
    my file name is cartpole.py I've imported: import tensorflow as tf and use tf.reset_default_graph()
  • martianwars
    martianwars over 7 years
    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() ?
  • Admin
    Admin over 7 years
    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
  • Simon Forsberg
    Simon Forsberg over 5 years
    @magnp I would suggest that you write your own answer for that. Using Python 3 helped me too.
  • Loganathan
    Loganathan almost 5 years
    Thanks! This solution of upgrading tensorflow removed the error for me.. But I have 'uninstall'ed and re-'install'ed instead of --upgrade option.
  • NeStack
    NeStack almost 5 years
    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
  • Marcello Miorelli
    Marcello Miorelli over 4 years
    that is exactly what the accepted answer says, so your answer is redundant
  • Shoval Sadde
    Shoval Sadde over 4 years
    On this thread? The accepted answer suggests that there might be another file named tensorflow.py. You probably mean that my answer is in line with the accepted answer in the thread that I link to. In such a case it is not redundant (here, on this page), because it contains a link to a more wholesome solution.
  • Zain Ul Abidin
    Zain Ul Abidin about 4 years
    Yeah you are right the accepted answer is not actually addressing the problem this is the actual answer
  • Debvrat Varshney
    Debvrat Varshney almost 4 years
    How does this answer the question?