AttributeError: module 'tensorflow' has no attribute 'name_scope' with Keras - python

I am trying to run a script, but I struggle already at the imports.
This import
from keras.preprocessing.image import save_img
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.

For everyone who use Tensorflow 2.0 and stumble upon this question with the same error, like I did: I solved it by changing the imports from keras.xxx to tensorflow.keras.xxx

I also encountered this same problem when I stopped my IDE while executing. Restarting my IDE works for me. Just save your program and restart IDE. Hope it will work for you as well.

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

My IDE offered me two different import paths
keras
or
tensorflow_core.python.keras
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.

My tensorflow version is 2.1, and I found my tensorflow-estimator version is 2.2
My fix is to downgrade the estimator to the same version

Related

DLL load failed while importing _pywrap_quantize_training

Hi developers and programmers, I'm try to import keras module and it show error, please help, Thank you.
code
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, TensorBoard
error
DLL load failed while importing _pywrap_quantize_training
I have try to reinstall keras and it show error below, im not is this causing the problem
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.10.0 requires keras<2.11,>=2.10.0, but you have keras 2.11.0 which is incompatible.tensorflow 2.10.0 requires tensorflow-estimator<2.11,>=2.10.0, but you have tensorflow-estimator 2.11.0 which is incompatible
im using vscode, xampp
I found the solution by myself, its the version compatible, i uninstall keras and tensorflow, then reinstall tensorflow by the version of 2.10 and it work

cannot import name 'normalization' from 'tensorflow.python.keras.layers'

I am trying to use tensorflow_rankings, but cannot import it due to the above error. I am using tensorflow 2.8.0 and tensorflow_rankings 0.5.0, which seem to be the latest stable builds. They are what get automatically installed from
pip install tensorflow
pip install tensorflow_ranking
I am on Python 3.8.10, Windows 11.
The TF 2.8.0 docs show there is a Normalization layer in tf.keras.layers. The error seems to come from:
from tensorflow.python.keras.layers import normalization as keras_norm
in
from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
Any advice?
Seems my installation of TF was corrupted. A full uninstall and reinstall fixed it.

torchvision and tensorflow-gpu import error

Running this:
import torchvision
import tensorflow
Produces the error:
SystemError: google/protobuf/pyext/descriptor.cc:354: bad argument to internal function
However, swapping the order of the imports does not cause the error:
import tensorflow
import torchvision
Why is this happening?
Version info:
tensorflow-gpu=1.15.0
torchvision==0.5.0
Try reinstalling torchvision.
I have opened a ticket on tensorflow. My guess is that the order in which you install torchvision matters:
The fix is either to reinstall torchvision or to always import tensorflow first, as shown above. The issue is difficult to reproduce since fresh install fixes things. Perhaps it is related to the order in which packages are installed -- perhaps torchvision is somehow targeting older tensorflow-related libraries... and updating it again fixes this.
This fix:
import tensorflow # BEFORE all other imports
import torchvision
It works for me.
Source: https://github.com/tensorflow/tensorflow/issues/48797

keras module not found (No module named 'keras')

I tried to import keras to my ANN model but I found this Module not found the type of error. I tried tp import from tensorflow.keras but still it's the same problem.
No module named 'keras'
Try importing keras first. Try:
from numpy import loadtxt
import keras
from keras.models import Sequential
from keras.layers import Dense
If this doesn't work, try using TensorFlow: pip install tensorflow and:
from tensorflow.keras import Sequential
from tensorflow.keras import Sequential
!pip install keras on jupyter notebook
I think this problem is related to the environment. Your TensorFlow module may be installed in a different env. and Keras is in a different env. So, try to uninstall the Keras module and reinstall it.
$ pip uninstall keras
$ pip install keras
If this doesn't work, try using TensorFlow: pip install tensorflow and:
from tensorflow.keras import Sequential

TensorFlow, "'module' object has no attribute 'placeholder'"

I've been trying to use tensorflow for two days now installing and reinstalling it over and over again in python2.7 and 3.4. No matter what I do, I get this error message when trying to use tensorflow.placeholder()
It's very boilerplate code:
tf_in = tf.placeholder("float", [None, A]) # Features
No matter what I do I always get the trace back:
Traceback (most recent call last):
File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 2, in <module>
import tensorflow as tf
File "/home/willim/PycharmProjects/tensorflow/tensorflow.py", line 53, in <module>
tf_in = tf.placeholder("float", [None, A]) # Features
AttributeError: 'module' object has no attribute 'placeholder'
Anyone know how I can fix this?
If you have this error after an upgrade to TensorFlow 2.0, you can still use 1.X API by replacing:
import tensorflow as tf
by
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Solution: Do not use "tensorflow" as your filename.
Notice that you use tensorflow.py as your filename. And I guess you write code like:
import tensorflow as tf
Then you are actually importing the script file "tensorflow.py" that is under your current working directory, rather than the "real" tensorflow module from Google.
Here is the order in which a module will be searched when importing:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names,
with the same syntax as the shell variable PATH).
The installation-dependent default.
It happened to me too. I had tensorflow and it was working pretty well, but when I install tensorflow-gpu along side the previous tensorflow this error arose then I did these 3 steps and it started working with no problem:
I removed tensorflow-gpu, tensorflow, tensorflow-base packages from Anaconda. Using.
conda remove tensorflow-gpu tensorflow tensorflow-base
re-installed tensorflow. Using
conda install tensorflow
Instead of tf.placeholder(shape=[None, 2], dtype=tf.float32) use something like
tf.compat.v1.placeholder(shape=[None, 2], dtype=tf.float32) if you don't want to disable v2 completely.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
works.
I am using Python 3.7 and tensorflow 2.0.
It appears that .placeholder() , .reset_default_graph() , and others were removed with version 2. I ran into this issue using Docker image: tensorflow/tensorflow:latest-gpu-py3 which automatically pulls the latest version. I was working in 1.13.1 and was 'upgraded to 2' automatically and started getting the error messages. I fixed this by being more specific with my image: tensorflow/tensorflow:1.13.1-gpu-py3.
More info can be found here: https://www.tensorflow.org/alpha/guide/effective_tf2
Avoid using the below striked out statement in tensorflow=2.0
i̶m̶p̶o̶r̶t̶ ̶t̶e̶n̶s̶o̶r̶f̶l̶o̶w̶ ̶a̶s̶ ̶t̶f̶ ̶x̶ ̶=̶ ̶t̶f̶.̶p̶l̶a̶c̶e̶h̶o̶l̶d̶e̶r̶(̶s̶h̶a̶p̶e̶=̶[̶N̶o̶n̶e̶,̶ ̶2̶]̶,̶ ̶d̶t̶y̶p̶e̶=̶t̶f̶.̶f̶l̶o̶a̶t̶3̶2̶)̶
You can disable the v2 behavior by using the following code
This one is perfectly working for me.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
x = tf.placeholder(shape=[None, 2], dtype=tf.float32)
I also got the same error. May be because of the version of tensorflow.
After installing tensorflow 1.4.0, I got relief from the error.
pip install tensorflow==1.4.0
If you are using TensorFlow 2.0, then some code developed for tf 1.x may not code work. Either you can follow the link : https://www.tensorflow.org/guide/migrate
or you can install a previous version of tf by
pip3 install tensorflow==version
Import the old version of tensorflow instead of the new version
[https://inneka.com/ml/tf/tensorflow-module-object-has-no-attribute-placeholder/][1]
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
You need to use the keras model with tensorflow 2, as here
import tensorflow as tf
from tensorflow.python.keras.layers import Input, Embedding, Dot, Reshape, Dense
from tensorflow.python.keras.models import Model
Recent version 2.0 does not support placeholder.
I uninstalled 2.0 using command: conda remove tensorflow.
then I installed 1.15.0 using command: conda install -c conda-forge tensorflow=1.15.0.
1.15 is latest in version 1 series. You can change as per you wish and requirement.
For seeing all version, use command: conda search tensorflow.
It worked for Anaconda3 in Windows.
Try this:
pip install tensorflow==1.14
or this (if you have GPU):
pip install tensorflow-gpu==1.14
Please take a look at the Migrate your TensorFlow 1 code to TensorFlow 2.
These codes:
import tensorflow as tf
tf_in = tf.placeholder("float", [None, A]) # Features
need to be migrated in TensorFlow 2 as below:
import tensorflow as tf
import tensorflow.compat.v1 as v1
tf_in = vi.placeholder("float", [None, A]) # Features
If you get this on tensorflow 2.0.0+, it's very likely because the code isn't compatible with the newer version of tensorflow.
To fix this, run the tf_upgrade_v2 script.
tf_upgrade_v2 --infile=YOUR_SCRIPT.py --outfile=YOUR_SCRIPT.py
Faced same issue on Ubuntu 16LTS when tensor flow was installed over existing python installation.
Workaround:
1.)Uninstall tensorflow from pip and pip3
sudo pip uninstall tensorflow
sudo pip3 uninstall tensorflow
2.)Uninstall python & python3
sudo apt-get remove python-dev python3-dev python-pip python3-pip
3.)Install only a single version of python(I used python 3)
sudo apt-get install python3-dev python3-pip
4.)Install tensorflow to python3
sudo pip3 install --upgrade pip
for non GPU tensorflow, run this command
sudo pip3 install --upgrade tensorflow
for GPU tensorflow, run below command
sudo pip3 install --upgrade tensorflow-gpu
Suggest not to install GPU and vanilla version of tensorflow
The error shows up because we are using tensorflow version 2 and the command is from version 1. So if we use:
tf.compat.v1.summary.(method_name)
It'll work
Because you cant use placeholder in tensflow2.0version, so you need to use tensflow1*, or you need to change your code to fix tensflow2.0
I had the same problem before after tried to upgrade tensorflow, I solved it by reinstalling Tensorflow and Keras.
pip uninstall tensorflow
pip uninstall keras
Then:
pip install tensorflow
pip install keras
The problem is with TensorFlow version; the one you are running is 2.0 or something above 1.5, while placeholder can only work with 1.4.
So simply uninstall TensorFlow, then install it again with version 1.4 and everything will work.
It may be the typo if you incorrectly wrote the placeholder word.
In my case I misspelled it as placehoder and got the error like this:
AttributeError: 'module' object has no attribute 'placehoder'

Categories

Resources