Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

raises the following error:

AttributeError: module 'tensorflow' has no attribute 'name_scope'.

I am using the following packages.

Keras                     2.2.2,                     
Keras-Applications        1.0.4,                   
Keras-Preprocessing       1.0.2,                   
tensorflow                1.9.0,                     
tensorflow-gpu            1.9.0                

I was unable to reproduce with the same versions of the keras and tensorflow, reinstalling keras and tensorflow, may solve the issue, please use commands below:

pip install --upgrade pip setuptools wheel
pip install -I tensorflow
pip install -I keras

NOTE: The -I parameter stands for ignore installed package.

Just to add to this answer, name_scope should indeed be included with tf 1.9.0 so it seems that there is something fishy with the installation. – VegardKT Aug 7, 2018 at 10:29 But Why? I was using this environment for a week now to build models in keras. Maybe because I installed tensorflow serving today? – Henrik Aug 7, 2018 at 10:42 I get the error "ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow" when following these instructions. – user3367130 Aug 27, 2020 at 2:10 @user3367130 I think you have installed older tensorflow version at some point, you can check versions with pip freeze – Andriy Ivaneyko Aug 27, 2020 at 9:02

As Andriy Ivaneyko mentioned above, reinstalling tensorflow helps. I'm not sure why, but installing tensorflow-serving-api breaks something somewhere along the way. We solved this by running:

pip install --force-reinstall tensorflow

Note that this applies to both tensorflow and tensorflow-gpu installations. Specifically, the above command will fix this problem in situations where you're specifically using tensorlfow-gpu. tensorflow-serving-api installs regular tensorflow if it's not already installed.

I encountered this same bug and reinstalling tensorflow made no difference, and this caused me some headscratching.

Eventually I noticed that my IDE autocomplete had added the following line to my code:

from tensorflow_core.python.keras.callbacks import EarlyStopping

It seems that directly referencing tensorflow_core.python will break tensorflow.

Replacing this with the normal tensorflow import solved the problem!

from tensorflow.keras.callbacks import EarlyStopping

In my example I could either import like this:

from keras.layers import Dense, Dropout, LSTM, Input, Activation
from keras import optimizers, Model

or like that:

from tensorflow_core.python.keras import Input, Model, optimizers
from tensorflow_core.python.keras.layers import LSTM, Dropout, Dense

Mixing up tensorflow_core.python.keras and plain keras led to the problem in my case. After I imported everything directly from keras and keras.layers, it worked for me.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.