Skip to main content

Python TensorFlow/Keras: How to Resolve "ImportError: cannot import name 'adam' from 'keras.optimizers'"

When working with TensorFlow and Keras, you might encounter the ImportError: cannot import name 'adam' from 'keras.optimizers'. This error typically occurs for two main reasons: you are using an outdated import path for modern versions of TensorFlow where Keras is deeply integrated, or you are using incorrect capitalization for the class name.

More recently, with TensorFlow 2.11+, Keras introduced a new set of optimizer implementations, which adds another layer of nuance. This guide will walk you through the correct import statements for modern TensorFlow, explain the difference between the old and new optimizers, and ensure you can resolve this error.

Understanding the Error: Keras and TensorFlow Integration

Initially, Keras was a separate, high-level deep learning library that could run on top of different backends, including TensorFlow. However, starting with TensorFlow 2.0, Keras was adopted as the official high-level API for TensorFlow and is now deeply integrated.

As a result, all Keras modules should be imported from the tensorflow.keras namespace, not the standalone keras package. The error often arises when running older code (which used from keras...) with a modern TensorFlow installation.

Solution 1: Use the Correct tensorflow.keras Import Path (Most Common Fix)

For almost all modern TensorFlow projects, the fix is to update your import statement to pull from tensorflow.keras.

Example of code causing the error:

# This import path is for old, standalone Keras installations and will
# likely fail or cause issues in a modern TensorFlow environment.
try:
from keras.optimizers import Adam
print("Import successful (likely using standalone Keras).")
except ImportError as e:
print(f"Error: {e}")

Solution: prefix the import path with tensorflow.. This ensures you are using the Keras version that is bundled with your TensorFlow installation.

# ✅ Correct: The standard import path for modern TensorFlow
from tensorflow.keras.optimizers import Adam

# You can now create an instance of the Adam optimizer
optimizer = Adam(learning_rate=0.001)

print("Successfully imported Adam from tensorflow.keras.optimizers")
print(f"Optimizer object: {optimizer}")

Output:

Successfully imported Adam from tensorflow.keras.optimizers
Optimizer object: <keras.optimizers.legacy.adam.Adam object at 0x...>

Solution 2: Correct the Capitalization (Adam vs. adam)

Python is case-sensitive. The optimizer class is named Adam (using PascalCase), not adam. A common mistake is to use the wrong case in the import statement.

Example of code causing the error:

try:
# Incorrect capitalization
from tensorflow.keras.optimizers import adam
except ImportError as e:
print(f"Error: {e}")

Output:

Error: cannot import name 'adam' from 'tensorflow.keras.optimizers'
note

As we will see in the next section, this import may work on very recent TensorFlow versions, but it imports a different object than you might expect.

Solution: ensure you use the correct Adam capitalization for the class name.

# ✅ Correct: Use PascalCase for the class name
from tensorflow.keras.optimizers import Adam

print("Successfully imported the Adam optimizer class.")

Output:

Successfully imported the Adam optimizer class.

Understanding the Modern Optimizers (TensorFlow 2.11+)

Starting with TensorFlow 2.11, Keras introduced a new suite of optimizer implementations. To maintain backward compatibility, the older optimizers were moved to a legacy namespace.

This creates two possible imports in the latest versions:

  • from tensorflow.keras.optimizers import Adam: Imports the legacy Adam optimizer class. This is often the default for backward compatibility.
  • from tensorflow.keras.optimizers import adam: Imports the new adam optimizer module. You would then create an instance using adam.Adam().

Example on TensorFlow 2.11+

import tensorflow as tf

# This example assumes you are running TensorFlow 2.11 or newer.

# Imports the new optimizer module (lowercase)
from tensorflow.keras.optimizers import adam

# Imports the legacy Adam optimizer class (PascalCase)
from tensorflow.keras.optimizers import Adam

print(f"New optimizer module: {adam}")
print(f"Legacy Adam class: {Adam}")

# To use the new optimizer:
new_optimizer = adam.Adam(learning_rate=0.001)
print(f"\nInstance of NEW optimizer: {new_optimizer}")

# To use the legacy optimizer:
legacy_optimizer = Adam(learning_rate=0.001)
print(f"Instance of LEGACY optimizer: {legacy_optimizer}")

Output:

New optimizer module: <module 'keras.src.optimizers.adam' from ...>
Legacy Adam class: <class 'keras.src.optimizers.legacy.adam.Adam'>

Instance of NEW optimizer: <keras.src.optimizers.adam.Adam object at ...>
Instance of LEGACY optimizer: <keras.src.optimizers.legacy.adam.Adam object at ...>
note

If you are using an older TensorFlow version (pre-2.11), the lowercase adam import will fail. For maximum compatibility, sticking with from tensorflow.keras.optimizers import Adam is the safest bet unless you specifically need features from the new optimizers.

Conclusion

If you are using...The correct import is...
Any modern TensorFlow version (2.x)from tensorflow.keras.optimizers import Adam
TensorFlow 2.11+ and want the new optimizerfrom tensorflow.keras.optimizers import adam
then optimizer = adam.Adam()

To fix the ImportError: cannot import name 'adam', follow these steps:

  1. Ensure you are importing from tensorflow.keras, not standalone keras.
  2. Use the correct capitalization: Adam for the optimizer class.
  3. If you are on the latest TensorFlow versions and the error persists, be aware of the distinction between the legacy Adam class and the new adam module.

For most users, simply changing the import to from tensorflow.keras.optimizers import Adam will resolve the issue.