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.
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.
That prints the learning-rate object, which may be a TensorFlow value rather than a plain Python float.
To get the numeric value:
This is the common answer for fixed-rate optimizers.
Old lr Versus Current learning_rate
Older Keras and TensorFlow examples often use:
In current tf.keras, prefer:
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.
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:
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.
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.
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.
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.lrfrom 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_ratedirectly when the optimizer is configured with a schedule. - Printing the object representation instead of extracting the numeric value.
Summary
- In modern
tf.keras, usemodel.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_rateif the behavior is unclear. - Do not copy old
optimizer.lrexamples blindly into current code.
Related reading
- Get length of a dataset in Tensorflow
- Get the bounding box coordinates in the TensorFlow object detection API tutorial
- Get the diagonal of a matrix in TensorFlow
- Get the last output of a dynamic_rnn in TensorFlow
- Get the label mappings from label encoder
- Get the value of some weights in a model trained by TensorFlow
- Getting around tf.argmax which is not differentiable
- Getting attribute error when using AUTOTUNE in Tensorflow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.