Keras
machine learning
learning rate
model training
tensor flow

Get learning rate of keras model

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

In modern tf.keras, the learning rate is usually stored on model.optimizer.learning_rate, not on the older optimizer.lr attribute that many old examples still show. The exact way you read it depends on whether the optimizer uses a fixed scalar, a tf.Variable, or a learning-rate schedule.

Read the Learning Rate from the Optimizer

For a standard fixed learning rate, access the optimizer directly after compiling the model.

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

That prints the learning-rate object, which may be a TensorFlow value rather than a plain Python float.

To get the numeric value:

python
lr_value = tf.keras.backend.get_value(model.optimizer.learning_rate)
print(lr_value)

This is the common answer for fixed-rate optimizers.

Old lr Versus Current learning_rate

Older Keras and TensorFlow examples often use:

python
model.optimizer.lr

In current tf.keras, prefer:

python
model.optimizer.learning_rate

That is the more stable and current API shape. If you are maintaining older code, the old property may still appear, but it should not be your default assumption.

When the Learning Rate Is a Schedule

The situation changes if the optimizer uses a learning-rate schedule instead of a fixed scalar.

python
1import tensorflow as tf
2
3schedule = tf.keras.optimizers.schedules.ExponentialDecay(
4    initial_learning_rate=0.01,
5    decay_steps=1000,
6    decay_rate=0.96,
7)
8
9optimizer = tf.keras.optimizers.Adam(learning_rate=schedule)

Now optimizer.learning_rate may not be a simple number. It may be a schedule object or a value derived from the current optimizer iteration.

To evaluate the current rate at a given step:

python
step = optimizer.iterations
current_lr = schedule(step)
print(float(current_lr.numpy()))

That distinction matters because "get the learning rate" is ambiguous unless you know whether the value is fixed or time-dependent.

Inspect During Training

A common use case is logging the rate at the end of each epoch.

python
1class LearningRateLogger(tf.keras.callbacks.Callback):
2    def on_epoch_end(self, epoch, logs=None):
3        lr = self.model.optimizer.learning_rate
4        try:
5            value = tf.keras.backend.get_value(lr)
6        except Exception:
7            value = lr(self.model.optimizer.iterations).numpy()
8
9        print(f"epoch={epoch + 1}, learning_rate={value}")

This is useful when:

  • you use a scheduler
  • a callback changes the rate over time
  • you want to confirm the optimizer really uses the value you expect

Set the Learning Rate Too

Sometimes the real task is not only reading the rate, but checking it before updating it.

python
1import tensorflow as tf
2
3optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
4print(tf.keras.backend.get_value(optimizer.learning_rate))
5
6optimizer.learning_rate.assign(0.0005)
7print(tf.keras.backend.get_value(optimizer.learning_rate))

This works for variable-like learning rates. If the optimizer is driven by a schedule object, assignment may not be the right model.

Optimizer-Specific Caveat

Most built-in optimizers expose learning_rate, but the exact object type may differ. So if you are writing reusable inspection code, check what you actually received.

python
lr = model.optimizer.learning_rate
print(type(lr))

That is often the fastest way to understand whether you are dealing with:

  • a float-like tensor
  • a variable
  • a schedule
  • a wrapped object managed by the optimizer

Common Pitfalls

  • Using optimizer.lr from old examples in modern code without checking the current API.
  • Assuming the learning rate is always a plain float.
  • Reading a schedule object as if it were the current scalar rate.
  • Updating learning_rate directly when the optimizer is configured with a schedule.
  • Printing the object representation instead of extracting the numeric value.

Summary

  • In modern tf.keras, use model.optimizer.learning_rate.
  • For fixed rates, tf.keras.backend.get_value(...) usually gives the numeric value.
  • For schedules, evaluate the schedule at the current step.
  • Inspect the type of learning_rate if the behavior is unclear.
  • Do not copy old optimizer.lr examples blindly into current code.

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.