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:
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.
from tensorflow.keras.optimizers import Adam
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:
from tensorflow.keras.optimizers import adam
except ImportError as e:
print(f"Error: {e}")Output:
Error: cannot import name 'adam' from 'tensorflow.keras.optimizers'Solution: ensure you use the correct Adam capitalization 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 legacyAdamoptimizer class. This is often the default for backward compatibility.from tensorflow.keras.optimizers import adam: Imports the newadamoptimizer module. You would then create an instance usingadam.Adam().
Example on TensorFlow 2.11+
import tensorflow as tf
from tensorflow.keras.optimizers import adam
from tensorflow.keras.optimizers import Adam
print(f"New optimizer module: {adam}")
print(f"Legacy Adam class: {Adam}")
new_optimizer = adam.Adam(learning_rate=0.001)
print(f"\nInstance of NEW optimizer: {new_optimizer}")
legacy_optimizer = Adam(learning_rate=0.001)
print(f"Instance of LEGACY optimizer: {legacy_optimizer}")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 ...>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 optimizer | from tensorflow.keras.optimizers import adamthen optimizer = adam.Adam() |
To fix the ImportError: cannot import name 'adam', follow these steps:
- Ensure you are importing from
tensorflow.keras, not standalonekeras. - Use the correct capitalization:
Adamfor the optimizer class. - If you are on the latest TensorFlow versions and the error persists, be aware of the distinction between the legacy
Adamclass and the newadammodule.
For most users, simply changing the import to from tensorflow.keras.optimizers import Adam will resolve the issue.