Keras
Optimizers
SGD
Adam
ImportError

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:

python
from tensorflow.keras.optimizers import Adam, SGD

If you are intentionally using standalone Keras 3, use:

python
from keras.optimizers import Adam, SGD

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:

python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3from tensorflow.keras.optimizers import Adam, SGD
4
5model = models.Sequential([
6    layers.Input(shape=(4,)),
7    layers.Dense(8, activation="relu"),
8    layers.Dense(1)
9])
10
11model.compile(
12    optimizer=Adam(learning_rate=0.001),
13    loss="mse"
14)

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:

python
1import keras
2from keras import layers
3from keras.optimizers import Adam, SGD
4
5model = keras.Sequential([
6    layers.Input(shape=(4,)),
7    layers.Dense(8, activation="relu"),
8    layers.Dense(1)
9])
10
11model.compile(
12    optimizer=SGD(learning_rate=0.01, momentum=0.9),
13    loss="mse"
14)

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:

python
1import tensorflow as tf
2from keras.optimizers import Adam
3
4model = tf.keras.Sequential([
5    tf.keras.layers.Dense(1)
6])

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:

python
1import tensorflow as tf
2
3print("TensorFlow:", tf.__version__)
4
5try:
6    import keras
7    print("Standalone Keras:", keras.__version__)
8except ImportError:
9    print("Standalone Keras not installed")

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:

python
from tensorflow.keras.optimizers.legacy import Adam, SGD

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:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(1)
6])
7
8model.compile(optimizer="adam", loss="mse")

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.optimizers in a TensorFlow project that should be using tensorflow.keras.optimizers.
  • Mixing keras.* and tf.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.optimizers in TensorFlow-based projects.
  • Use keras.optimizers only 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.

Course illustration
Course illustration

All Rights Reserved.