AttributeError: module 'keras.optimizers' has no attribute 'rmsprop' (or 'sgd', 'adam') means the optimizer name is written the old way. In current Keras the optimizers are classes with capitalised names: keras.optimizers.RMSprop, keras.optimizers.SGD and keras.optimizers.Adam, and the learning rate argument is learning_rate, not lr. This guide reproduces each optimizer error with Keras 3 and shows the working code.
Every error and fix was run with Python 3.12.5, TensorFlow 2.21.0 and Keras 3.15.1 in the Windows Command Prompt. See the official Keras optimizers documentation.
Quick fix
from keras.optimizers import RMSprop, SGD, Adam # not rmsprop / sgd / adam
model.compile(optimizer=RMSprop(learning_rate=0.001), loss="mse") # learning_rate, not lr
# or simply a string:
model.compile(optimizer="rmsprop", loss="mse")
| Old code (fails) | Error | Keras 3 code |
|---|---|---|
keras.optimizers.rmsprop() | AttributeError | keras.optimizers.RMSprop() |
keras.optimizers.sgd() | AttributeError | keras.optimizers.SGD() |
keras.optimizers.adam() | AttributeError | keras.optimizers.Adam() |
SGD(lr=0.01) | ValueError: Argument(s) not recognized | SGD(learning_rate=0.01) |
RMSprop(decay=1e-6) | UserWarning, decay ignored | A learning-rate schedule |
tf.keras.optimizers.legacy.SGD() | ImportError | keras.optimizers.SGD() (or tf_keras) |
Why the AttributeError happens
Old Keras 1/2 code and many tutorials used lowercase aliases such as optimizers.rmsprop. Keras 3 (installed with TensorFlow 2.16 and later) only has the class names:
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # hide TensorFlow start-up notices
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
import keras
for name in ["rmsprop", "sgd", "adam", "RMSprop", "SGD", "Adam"]:
try:
opt_class = getattr(keras.optimizers, name)
print(f"keras.optimizers.{name:<8} OK -> {opt_class.__name__}")
except AttributeError as e:
print(f"keras.optimizers.{name:<8} AttributeError: {e}")
Output:
keras.optimizers.rmsprop AttributeError: module 'keras.optimizers' has no attribute 'rmsprop'
keras.optimizers.sgd AttributeError: module 'keras.optimizers' has no attribute 'sgd'
keras.optimizers.adam AttributeError: module 'keras.optimizers' has no attribute 'adam'
keras.optimizers.RMSprop OK -> RMSprop
keras.optimizers.SGD OK -> SGD
keras.optimizers.Adam OK -> Adam
The same applies to imports: from keras.optimizers import rmsprop raises ImportError: cannot import name 'rmsprop', while from keras.optimizers import RMSprop works.
ValueError: Argument(s) not recognized: {‘lr’: …}
The next error after fixing the name is usually the learning rate argument. lr was renamed to learning_rate, and decay is no longer supported:
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # hide TensorFlow start-up notices
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
import warnings
import keras
warnings.simplefilter("always")
try:
keras.optimizers.SGD(lr=0.01) # old argument name
except ValueError as e:
print("ValueError:", e)
opt = keras.optimizers.RMSprop(learning_rate=0.001, decay=1e-6) # decay is ignored now
print("created:", type(opt).__name__, "learning_rate =", round(float(opt.learning_rate), 6))
Output:
ValueError: Argument(s) not recognized: {'lr': 0.01}
C:\pyguides\tfvenv\Lib\site-packages\keras\src\optimizers\base_optimizer.py:86: UserWarning: Argument `decay` is no longer supported and will be ignored.
warnings.warn(
created: RMSprop learning_rate = 0.001
lr= fails and decay= is ignored with a warning.Replacing decay with a learning-rate schedule
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # hide TensorFlow start-up notices
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
import keras
schedule = keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01, decay_steps=1000, decay_rate=0.9) # replaces the old decay= argument
opt = keras.optimizers.SGD(learning_rate=schedule, momentum=0.9)
for step in [0, 1000, 5000]:
print(f"step {step:>5}: learning rate = {float(schedule(step)):.5f}")
Output:
step 0: learning rate = 0.01000
step 1000: learning rate = 0.00900
step 5000: learning rate = 0.00590
Pass the schedule object as learning_rate; the optimizer updates the rate on every training step.
tf.keras.optimizers.legacy is not supported in Keras 3
Code that used the legacy optimizers (common with older TensorFlow 2 versions and on Apple M1/M2 guides) now fails with an ImportError:
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # hide TensorFlow start-up notices
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
import textwrap
import tensorflow as tf
try:
opt = tf.keras.optimizers.legacy.SGD(learning_rate=0.01)
except ImportError as e:
print(textwrap.fill("ImportError: " + str(e), width=110))
Output:
ImportError: `keras.optimizers.legacy` is not supported in Keras 3. When using `tf.keras`, to continue using a
`tf.keras.optimizers.legacy` optimizer, you can install the `tf_keras` package (Keras 2) and set the
environment variable `TF_USE_LEGACY_KERAS=True` to configure TensorFlow to use `tf_keras` when accessing
`tf.keras`.
tf_keras.Switch to keras.optimizers.SGD (recommended), or install tf_keras and set TF_USE_LEGACY_KERAS=1 as explained in No module named tensorflow.keras.
Working example: RMSprop, SGD and Adam
The same small regression model trained with each optimizer, using the Keras 3 names and arguments:
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" # hide TensorFlow start-up notices
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
import numpy as np
import keras
from keras.optimizers import RMSprop, SGD, Adam # class names start with capital letters
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 3))
y = X @ np.array([1.5, -2.0, 0.5]) + 0.3
for opt in [RMSprop(learning_rate=0.01), SGD(learning_rate=0.05, momentum=0.9), Adam(learning_rate=0.05)]:
model = keras.Sequential([keras.Input(shape=(3,)), keras.layers.Dense(1)])
model.compile(optimizer=opt, loss="mse")
history = model.fit(X, y, epochs=30, verbose=0)
print(f"{type(opt).__name__:<8} final loss: {history.history['loss'][-1]:.4f}")
Output (the loss values vary slightly between runs):
WARNING:tensorflow:TensorFlow GPU support is not available on native Windows for TensorFlow >= 2.11. Even if CUDA/cuDNN are installed, GPU will not be used. Please use WSL2 or the TensorFlow-DirectML plugin.
RMSprop final loss: 1.1217
SGD final loss: 0.0000
Adam final loss: 0.0000
Which optimizer should you use?
| Optimizer | Typical use |
|---|---|
Adam | Good default for most networks |
RMSprop | Recurrent networks and noisy gradients |
SGD(momentum=0.9) | Image models and when you tune the learning rate schedule carefully |
Check your Keras and TensorFlow versions
import tensorflow as tf, keras
print(tf.__version__, keras.__version__)
If Keras is 3.x, use the code in this guide. If you must keep old Keras 2 code running unchanged, pin an older TensorFlow in a separate virtual environment or use tf_keras.
Other TensorFlow and Keras errors explained:
- ModuleNotFoundError: No module named ‘tensorflow.keras’
- No module named ‘tensorflow.keras.utils.np_utils’
- Install TensorFlow
- Upgrade TensorFlow to the latest version
Frequently asked questions
How do I fix module ‘keras.optimizers’ has no attribute ‘rmsprop’?
Use the class name with capital letters: keras.optimizers.RMSprop(learning_rate=0.001), or pass the string "rmsprop" to compile().
How do I fix module ‘keras.optimizers’ has no attribute ‘SGD’?
Use keras.optimizers.SGD from the same Keras you build the model with; the error with capital letters usually comes from mixing an old standalone keras package with tf.keras. Check both versions.
Why does SGD(lr=0.01) raise ValueError?
The lr argument was renamed. Use SGD(learning_rate=0.01).
What replaced the decay argument in Keras optimizers?
Learning-rate schedules such as keras.optimizers.schedules.ExponentialDecay, passed as learning_rate.
Can I still use tf.keras.optimizers.legacy?
Not with Keras 3. Use the normal optimizers, or install tf_keras and set TF_USE_LEGACY_KERAS=1 before importing TensorFlow.
How do I import RMSprop in Keras 3?
from keras.optimizers import RMSprop or from tensorflow.keras.optimizers import RMSprop.
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn