Python is used for creating web applications and website pages by programmers and developers frequently. It is a general-purpose and high-level programming language well-accepted all over the world. Python is used for creating various programs with ease as it is easy to code, which makes it perfect for beginners to learn it. Python has many libraries that make coding simpler, and Tensorflow is one of those. It is an open-source Python-friendly library used for numerical computation to make developing neural networks and machine learning simpler and faster. Keras is a popular Tensorflow API used for handling machine learning issues. When you are working with all of these, you may experience “ImportError: cannot import name ‘LayerNormalization’ from ‘tensorflow.python.keras.layers.normalization’”.
Don’t worry when you get this error as we are here to help you fix it. Let’s start with how the error arises
How the error appears
When you try to create a model using Keras 2.3.1 and Tensorflow 2.5.0 in Python 3.8, you get the following error
ImportError: cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization' (C:\Users\ssc\AppData\Local\Programs\Python\Python38\lib\site-packages\tensorflow\python\keras\layers\normalization\__init__.py)
Ways To Handle the Error “ImportError: cannot import name ‘LayerNormalization’ from ‘tensorflow.python.keras.layers.normalization’”
The cause of the error is the outdated version. The library you are using to create a program is not matching with the requirements. We have a few options to help you successfully solve the error.
Option 1 – Version you should have
When there is a mismatch between the version of Keras, Tensorflow, and Python, you get the error warning. So, you are required to have the following version to avoid the error
python 3.8.6
tensorflow==2.5.0
keras==2.4.3
Option 2 – Import LayerNormalization
This is the simplest solution you can try to fix the error. You just need to import LayerNormalization. To do that, have a look at the below code:
from keras.layers import LayerNormalization
Option 3 – Join bits and pieces from various places
Another way to handle the error is to join bits and pieces from various places. To do that, use the below command that works with macOS:
pip uninstall -y tensorflow keras tf-nightly keras-nightly
python -m pip install tensorflow-macos
Option 4 – Import modules through the Tensorflow repository
To get rid of the error warning, you need to import the module through the Tensorflow repository instead of keras. You can do it using the following way:
from tensorflow.keras.models import Sequential
Option 5 – Update Tensorflow to version == 2.7.0
Updating Tensoflow to version == 2.7.0 is another way to make the error go away. You may have the following version:
tensorflow == 2.5.0
python == 3.9
To update to the 2.7.0 version, use the below command:
pip install --upgrade tensorflow
Conclusion
We shed light on the options to tackle the error “ImportError: cannot import name ‘LayerNormalization’ from ‘tensorflow.python.keras.layers.normalization’”. It is up to you to choose the one you want according to the current situation of your project.
I hope you find the options helpful! I wish you all the luck!