Unable to import SGD and Adam from 'keras.optimizers'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The import error around SGD or Adam usually comes from mixing two different Keras worlds: standalone Keras and TensorFlow’s bundled tf.keras. The fix is not “find a magical hidden import path”. It is to use the optimizer namespace that matches the package stack actually installed in your environment.
Start by Identifying Which Keras You Are Using
There are two common setups:
- TensorFlow project using
tf.keras - Standalone Keras 3 project using
keras
If your model code is built around TensorFlow, use TensorFlow imports consistently:
If you are intentionally using standalone Keras 3, use:
The import error often happens when a project mixes both styles in one environment and assumes they are interchangeable.
TensorFlow Projects Should Prefer tensorflow.keras
For a normal TensorFlow-based training script, this is the safe import:
This keeps the model, layers, and optimizers all inside the same TensorFlow-managed Keras stack. That consistency is more important than the exact optimizer import line by itself.
Standalone Keras 3 Uses keras.optimizers
If the project is built on standalone Keras 3, the keras.optimizers path is correct:
This is the right choice when you are intentionally using Keras as a separate multi-backend package rather than as TensorFlow’s bundled API.
Do Not Mix keras and tensorflow.keras
A common broken pattern looks like this:
Even if this imports successfully in some environments, it is fragile. You are combining components from two package namespaces that may not match in version or behavior. The safest rule is simple:
- TensorFlow project: use
tensorflow.keras.*everywhere - Standalone Keras project: use
keras.*everywhere
That one rule prevents most optimizer import problems.
Check Your Installed Versions
When the import still fails, print the installed versions and verify what Python is really loading:
If both packages are installed, make sure that is actually intentional. Many projects only need TensorFlow, and a separate keras installation can create confusion rather than helping.
Legacy Optimizers in TensorFlow
Some older TensorFlow codebases rely on optimizer behavior that changed in newer releases. If that is your situation, TensorFlow also exposes legacy optimizers:
This is not the first fix to try for a plain import error. It is mainly useful when upgrading old training code and you need compatibility with previous optimizer behavior.
String Names Are a Simple Alternative
If you do not need custom optimizer arguments, compile by string name:
This avoids an explicit optimizer import entirely. It is not a replacement when you need options such as momentum or custom learning rates, but it is perfectly valid for simple models.
Common Pitfalls
- Importing from
keras.optimizersin a TensorFlow project that should be usingtensorflow.keras.optimizers. - Mixing
keras.*andtf.keras.*in the same script. - Assuming the import path alone is the problem when the real issue is a conflicting package installation.
- Using old examples that rely on outdated optimizer APIs without checking the package version in use.
- Reaching for legacy optimizers before confirming that the base import namespace is correct.
Summary
- Use
tensorflow.keras.optimizersin TensorFlow-based projects. - Use
keras.optimizersonly in standalone Keras projects. - Keep model, layers, and optimizers in the same package namespace.
- Check installed versions when imports behave unexpectedly.
- Use legacy TensorFlow optimizers only for compatibility cases, not as the default first fix.

