Importerror: cannot import name adam from keras.optimizers

If you’re encountering the “ImportError: cannot import name adam from keras.optimizers” error, it means that there is an issue with the Keras library when trying to use the Adam optimizer.

This error is commonly faced by developers who are trying to run deep-learning models using Keras.

So this article will explore what this error message means and how you can troubleshoot it.

But before we dive into the details of the “ImportError: cannot import name adam from keras.optimizers” error, let’s understand first what the Adam optimizer is.

What is the Adam Optimizer?

The Adam optimizer is an adaptive learning rate optimization algorithm that was introduced in 2014 by Diederik Kingma and Jimmy Ba.

Moreover, it is an extension of the stochastic gradient descent (SGD) optimizer that is commonly used in deep learning.

In addition to that, it works by keeping track of the first and second moments of the gradients and using them to adaptively adjust the learning rate.

This allows the algorithm to converge faster and more reliably than traditional SGD.

What is Importerror: cannot import name adam from keras.optimizers?

This error Importerror: cannot import name adam from keras.optimizers occur when attempting to import the adam optimizer from the keras.optimizers module in Python’s Keras library.

The error message indicates that the adam optimizer cannot be imported, which could be due to various reasons such as incorrect installation, outdated version, or a typo in the code.

Problem Scenario

Here’s how to reproduce this error, indicating the discussion above.

from keras.optimizers import Adam 

Output:

Importerror: cannot import name 'adam' from 'keras.optimizers'

Another example that can trigger this error…

When we use Adam but in lower case, such as “Adam” case folds are “adam” and “ADAM”.

from tensorflow.keras.optimizers import adam 

How to fix cannot import name ‘adam’ from ‘keras.optimizers’?

Now that we’ve looked at the common causes of the Importerror: cannot import name adam from keras.optimizers, let’s explore some solutions to fix it.

Update Keras and Tensorflow

If you’re using an outdated version of Keras or Tensorflow, updating to the latest version can fix the “ImportError: cannot import name adam from keras.optimizers” error.

You can update Keras and Tensorflow using pip, a package manager for Python.

To update Keras and Tensorflow, run the following command in your terminal or command prompt:

pip install keras --upgrade
pip install tensorflow --upgrade

This will install the latest version of Keras and Tensorflow on your system.

Check your Code typos

If you’re confident that your Keras and Tensorflow versions are up to date, the next step is to check your code for typos.

Make sure that you spelled “Adam” correctly and that the capitalization is correct in your code.

Remove Conflicting Versions of Keras or Tensorflow

If you have multiple versions of Keras or Tensorflow installed on your system, removing conflicting versions can fix the “ImportError: cannot import name adam from keras.optimizers” error.

You can use pip to uninstall Keras and Tensorflow:

pip uninstall keras
pip uninstall tensorflow

This will remove all versions of Keras and Tensorflow from your system.

You can then install the latest version of Keras and Tensorflow using pip:

pip install keras
pip install tensorflow

Reinstall Keras or Tensorflow

If none of the above solutions work, you can try reinstalling Keras or Tensorflow. Make sure that you uninstall all versions of Keras and Tensorflow from your system before reinstalling.

To reinstall Keras and Tensorflow, run the following command in your terminal or command prompt:

pip install keras
pip install tensorflow

Fixed Importerror

Now, here is the example code snippet that imports the adam optimizer from the keras.optimizers module without any errors:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam

model = Sequential()
model.add(Dense(64, input_dim=100, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer=Adam(), metrics=['accuracy'])

Anyway, here are other fixed errors you can consider when somehow you might encounter them.

FAQs

How can I fix the “ImportError: cannot import name adam from keras.optimizers” error?

You can fix the “ImportError: cannot import name adam from keras.optimizers” error by updating Keras and Tensorflow.

Also, check your code for typos, remove conflicting versions of Keras or Tensorflow, or reinstall Keras or Tensorflow.

What is the Adam optimizer?


The Adam optimizer is an adaptive learning rate optimization algorithm that is commonly used in deep learning.

It allows the algorithm to converge faster and more reliably than traditional stochastic gradient descent (SGD) optimizers.

Can I use other optimization algorithms besides Adam in Keras?


Yes, Keras supports a variety of optimization algorithms, including stochastic gradient descent (SGD), RMSprop, Adagrad, Adadelta, and Nadam.

Conclusion

In conclusion, Importerror: cannot import name adam from keras.optimizers error can be frustrating, but they can be fixed by following the solutions outlined in this article.

  • Updating Keras and Tensorflow
  • checking your code for typos
  • removing conflicting versions of Keras or Tensorflow
  • reinstalling Keras or Tensorflow;

And all possible solutions to this error.

I hope this article has helped you fix the error.

Until next time! 😊

Frequently Asked Questions

What is Python ImportError and what causes it?

ImportError is raised when an import fails for any reason. The most specific subtype is ModuleNotFoundError (no such module). Plain ImportError typically means the module exists but a name inside it can’t be imported, e.g. ‘cannot import name X from Y’ (X was renamed, removed, or moved between versions of Y). Common with library version mismatches.

How do I fix ‘cannot import name X from Y’?

Three steps: (1) Check the library version: pip show Y. (2) Check the changelog of Y, X may have been renamed or removed in a recent release. (3) Either pin to an older Y version (pip install Y==1.x.y) or update your code to the new import path. Common 2025-2026 examples: Werkzeug url_decode removed, Pillow ANTIALIAS renamed to LANCZOS.

Why does the import work in REPL but fail in script?

Two reasons. (1) Different Python interpreter: REPL uses one Python, your script uses another. Run python –version both times. (2) Different working directory: REPL is started where you have access to local modules, script is run from a different cwd. Add the project path to sys.path or use python -m to run as a module.

How do I avoid circular import errors?

Circular imports happen when module A imports B and B imports A at the top level. Three fixes: (1) Move one import inside the function that uses it (lazy import). (2) Restructure code so A and B both import from a third module C. (3) Use TYPE_CHECKING for type-hint-only imports: if TYPE_CHECKING: from a import X.

Where can I find more ImportError fixes?

Browse the ImportError reference hub for 67+ specific fixes (Flask, Werkzeug, Django, ML library versions). For missing-module cases see ModuleNotFoundError. For Python setup help see Python Tutorial hub.

Glay Eliver

Programmer & Technical Writer at PIES IT Solution

Glay Eliver is a programmer and writer at PIES IT Solution, author of over 600 tutorials at itsourcecode.com. Specializes in JavaScript tutorials, Microsoft Office how-tos (Excel, Word, PowerPoint), and Python error debugging covering ImportError, TypeError, AttributeError, ModuleNotFoundError, and JavaScript ReferenceError. Authored several of the site’s highest-traffic Excel and MS Office reference articles.

Expertise: JavaScript · MS Excel · MS Word · MS PowerPoint · Python · Python ImportError · Python TypeError · Python AttributeError · ModuleNotFoundError · JavaScript ReferenceError · Pygame  · View all posts by Glay Eliver →