Attributeerror: module ‘keras.engine’ has no attribute ‘layer’ issue is occurring because of some package structure change internally in keras. Actually, the code we are using is compatible with keras==2.4.0 and tensorflow==2.3.0 and above version but in some cases, we have installed version Lower versions of TensorFlow like 1.x series etc . In this situation, the Interpreter will through this attributeError. In this article, we will explore the best ways to fix this error.
Attributeerror: module ‘keras.engine’ has no attribute ‘layer’ ( Solution ) –
As we discussed the package upgrade the best and first solution. There are different ways to upgrade the package internally. However the third solution is a bit different, In the third solution we check the import statement.
Solution 1: Keras and TensorFlow Upgrade using pip –
In a single command, we can upgrade the version.
pip install keras==2.4.0
pip install tensorflow==2.3.0
Here we can choose the other above version or we can directly install the latest version. The command to install the latest version for keras and tensorflow will be –
pip install keras
pip install tensorflow

Solution 2: Keras and TensorFlow Upgrade using conda –
If are using the anaconda or conda package manager then you should upgrade the tensorflow or keras via the below command.
conda install -c conda-forge tensorflow
conda install -c conda-forge keras
Solution 3: Changing the Import Statement –
You should also check the import statement if the above two are not working.
The below one is correct –
import keras.engine.topology as KE
I know you are thinking then what is incorrect? So here it is –
import keras.engine as KE
Actually, In the above one we missed the topology module. Hence we are getting this error. This is a kind of AttributeError but the rules and basic concept to fix any attributError will always be the same. Just to give some hand to understand the generic attributeError we have created the below article. Please go through the same.
What is AttributeError in Python ? Complete Overview
Thanks
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.