Python
Keras
RMSprop
Machine Learning
AttributeError

Error module 'keras.optimizers' has no attribute 'RMSprop'

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The error AttributeError: module 'keras.optimizers' has no attribute 'RMSprop' occurs when the import path for Keras optimizers does not match your installed version of TensorFlow/Keras. In TensorFlow 2.11+, the standalone keras package was restructured, and optimizer class names changed from keras.optimizers.RMSprop to keras.optimizers.rmsprop.RMSprop or tf.keras.optimizers.RMSprop. The fix depends on your TensorFlow version: use tf.keras.optimizers.RMSprop for TF 2.x, or use the string shorthand "rmsprop" in model.compile() which works across all versions.

The Error

python
1import keras
2from keras.optimizers import RMSprop
3# AttributeError: module 'keras.optimizers' has no attribute 'RMSprop'
4
5# Or
6optimizer = keras.optimizers.RMSprop(learning_rate=0.001)
7# AttributeError: module 'keras.optimizers' has no attribute 'RMSprop'
python
1import tensorflow as tf
2
3# TF 2.x — always use tf.keras instead of standalone keras
4optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)
5
6model = tf.keras.Sequential([
7    tf.keras.layers.Dense(128, activation='relu', input_shape=(784,)),
8    tf.keras.layers.Dense(10, activation='softmax')
9])
10
11model.compile(
12    optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001),
13    loss='sparse_categorical_crossentropy',
14    metrics=['accuracy']
15)

Since TensorFlow 2.0, tf.keras is the official Keras API. The standalone keras package may have different import paths or may not be installed.

Fix 2: Use String Shorthand

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(128, activation='relu'),
5    tf.keras.layers.Dense(10, activation='softmax')
6])
7
8# String shorthand — works across all TF/Keras versions
9model.compile(
10    optimizer='rmsprop',  # No import needed
11    loss='categorical_crossentropy',
12    metrics=['accuracy']
13)
14
15# With custom learning rate via string + config
16model.compile(
17    optimizer=tf.keras.optimizers.get({
18        'class_name': 'RMSprop',
19        'config': {'learning_rate': 0.001}
20    }),
21    loss='categorical_crossentropy'
22)

The string "rmsprop" is resolved internally by Keras regardless of the import path structure. This is the most portable approach.

Fix 3: Legacy Optimizer (TF 2.11+)

python
1import tensorflow as tf
2
3# TF 2.11+ introduced new optimizers and moved legacy ones
4# New optimizer (default in TF 2.11+)
5optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)
6
7# Legacy optimizer (if you need TF 2.10 behavior)
8optimizer = tf.keras.optimizers.legacy.RMSprop(learning_rate=0.001)
9
10# Check your TensorFlow version
11print(tf.__version__)
12# 2.15.0

TF 2.11 replaced the optimizer implementations with new versions that have different default behaviors (e.g., Amsgrad support, different weight decay handling). Use tf.keras.optimizers.legacy.RMSprop if you need backward-compatible behavior.

Fix 4: Install Correct Packages

bash
1# Check what's installed
2pip show tensorflow keras
3
4# Ensure tensorflow is installed (includes tf.keras)
5pip install --upgrade tensorflow
6
7# Remove standalone keras if it conflicts
8pip uninstall keras
9pip install --upgrade tensorflow
10
11# For TF 2.16+, keras is a separate package again (Keras 3)
12pip install --upgrade tensorflow keras
python
1# Verify the correct import works
2import tensorflow as tf
3print(tf.__version__)
4print(tf.keras.optimizers.RMSprop)
5# <class 'keras.src.optimizers.rmsprop.RMSprop'>

All Available Optimizers

python
1import tensorflow as tf
2
3# Common optimizers in tf.keras.optimizers
4optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
5optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)
6optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)
7optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.01)
8optimizer = tf.keras.optimizers.Adadelta(learning_rate=1.0)
9optimizer = tf.keras.optimizers.Adamax(learning_rate=0.002)
10optimizer = tf.keras.optimizers.Nadam(learning_rate=0.002)
11optimizer = tf.keras.optimizers.Ftrl(learning_rate=0.001)
12
13# String shorthands
14model.compile(optimizer='adam', ...)
15model.compile(optimizer='sgd', ...)
16model.compile(optimizer='rmsprop', ...)
17model.compile(optimizer='adagrad', ...)

Keras 3 (Multi-Backend)

python
1# Keras 3 (TF 2.16+) supports multiple backends
2import os
3os.environ["KERAS_BACKEND"] = "tensorflow"  # or "jax", "torch"
4
5import keras
6
7# Keras 3 import path
8optimizer = keras.optimizers.RMSprop(learning_rate=0.001)
9
10# Also available via tf.keras (when using TF backend)
11import tensorflow as tf
12optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.001)

Keras 3 is a standalone multi-backend library. Import paths may differ from TF-bundled Keras. Check your keras and tensorflow versions to determine the correct import.

Common Pitfalls

  • Mixing import keras with import tf.keras: The standalone keras package and tf.keras may be different versions with different APIs. Use tf.keras consistently in TF 2.x projects to avoid import mismatches.
  • Using capitalization incorrectly: The class is RMSprop (capital R, M, S), not Rmsprop or rmsprop. The string shorthand "rmsprop" is case-insensitive, but the class name is exact.
  • Not upgrading after TF version change: TF 2.11 moved optimizers to a new implementation. Code that worked on TF 2.10 may fail on 2.11+ if it imports from internal paths like keras.optimizers.rmsprop_v2. Use public API paths only.
  • Installing keras without tensorflow: The standalone keras package (v3) requires a backend (tensorflow, jax, or torch). Installing keras alone without setting KERAS_BACKEND and installing a backend produces import errors.
  • Using legacy optimizer unknowingly: tf.keras.optimizers.legacy.RMSprop and tf.keras.optimizers.RMSprop have different default parameters in TF 2.11+. The legacy version matches TF 2.10 behavior. If model training behaves differently after upgrading TF, check which optimizer class you are using.

Summary

  • Use tf.keras.optimizers.RMSprop(learning_rate=0.001) for TensorFlow 2.x projects
  • Use the string "rmsprop" in model.compile() for maximum portability across versions
  • Use tf.keras.optimizers.legacy.RMSprop for backward-compatible behavior in TF 2.11+
  • Always import from tf.keras, not standalone keras, to avoid version mismatches
  • Run pip install --upgrade tensorflow to ensure compatible packages are installed
  • Check tf.__version__ and print(tf.keras.optimizers.RMSprop) to verify the import path works

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.