Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 27

Optimizing Learning Rates

Recommended resources

Hello! Welcome to your next lesson on optimization.

In our last session, we explored the trade-offs between Batch, Mini-Batch, and Stochastic Gradient Descent. We learned that Mini-Batch GD is the practical choice for most deep learning tasks, offering a balance between computational efficiency and stable convergence.

However, all these methods share a critical hyperparameter: the learning rate, denoted by . Choosing the right value for is arguably one of the most important decisions you'll make when training a neural network. A poor choice can lead to painfully slow training, or worse, a model that fails to learn anything at all.

Today, we will tackle this challenge head-on. Our learning outcome is to diagnose and address issues with learning rates that are too high or too low. We'll learn to interpret the tell-tale signs from our model's training logs and explore powerful techniques for finding a good learning rate and adjusting it during training.

What is the Learning Rate, Again?

As a quick recap, the learning rate is the "step size" we take in the direction opposite to the gradient. It scales the gradient to determine the magnitude of the update to our model's weights in each iteration.

The update rule we've been using is:

Here, directly controls how large of an update we make.

Learning Rate in a Neural Network explained

For a quick conceptual refresher on how the learning rate fits into the update process, let's watch this brief clip from the deeplizard YouTube channel.

Watch from 0:42 to 1:54. Focus on the core idea: the learning rate is a small number that scales the gradient before it's subtracted from the weights.

The key takeaway is that this small number has a massive impact on the training process. Let's see what happens when we get it wrong.


Diagnosing Learning Rate Issues from Loss Curves

The most powerful tool for diagnosing learning rate problems is the loss curve, which plots the loss value at each epoch or iteration. The shape of this curve tells a story about how your model is learning.

Impact of Learning Rate on Gradient Descent Convergence
This image illustrates the three common scenarios for a learning rate: too low, just right, and too high. It shows how the step size affects the path to the minimum of the loss function.

Let's break down how these scenarios manifest in the loss curves you'll see during training. The article "Interpreting Learning Curve Patterns" from the Hugging Face LLM Course provides excellent visual examples.

Understanding Learning Curves - Hugging Face LLM Course

We will now read specific sections from the Hugging Face article to understand how to interpret different loss curve shapes.

Please read the following sections from the article: 'Healthy Learning Curves': Note the characteristics of a good training run. 'Practical Examples', specifically the 'During Training' subsection: This gives you a checklist of what to monitor, including the learning rate's effect. 'Underfitting': Notice how a learning rate that is too low can be a cause of underfitting, where the loss curve flattens out at a high value. 'Erratic Learning Curves': This section shows the classic symptoms of a learning rate that is too high.

Based on the reading and the image above, we can summarize the key symptoms:

1. Learning Rate is Too High

  • Symptom: The loss doesn't decrease. It might fluctuate wildly, jump around, or even increase, leading to an "exploding" loss (NaN values).
  • Why it happens: The steps you're taking are too large. You're overshooting the minimum of the loss landscape and landing somewhere with an even higher loss. The optimization process becomes unstable and diverges.
  • What the curve looks like: Erratic and spiky, or trending upwards.

2. Learning Rate is Too Low

  • Symptom: The loss decreases, but incredibly slowly. The model takes a huge number of epochs to converge, or it may appear to plateau prematurely because the updates are too small to make meaningful progress.
  • Why it happens: The steps are too small, leading to extremely slow progress down the loss gradient. It can also cause the model to get stuck in a shallow local minimum, unable to make a large enough step to get out.
  • What the curve looks like: The curve is very smooth but almost flat, decreasing at a snail's pace.

3. The Learning Rate is "Just Right"

  • Symptom: The loss shows a steady, reasonably steep decline, which gradually becomes less steep as training progresses and the model approaches a minimum.
  • Why it happens: The step size is large enough to ensure fast progress but small enough to allow the model to settle in a good minimum.
  • What the curve looks like: A healthy, downward-sloping curve that eventually flattens out (converges).
Test your understanding!

You are training two different models and get the following training loss curves after 50 epochs.

  • Model A: The loss starts at 2.5 and drops very slowly, reaching 2.1 by epoch 50.
  • Model B: The loss starts at 2.5, immediately jumps to 10.3, then 50.8, and finally becomes NaN.

What is the most likely issue with the learning rate for each model?

Show answer
  • Model A: The learning rate is likely too low. The loss is decreasing, but far too slowly, indicating that the parameter updates are tiny.
  • Model B: The learning rate is definitely too high. The loss is exploding, a classic sign of divergence where the updates are so large they overshoot the minimum and send the parameters to extreme values.

Strategies for Finding and Managing the Learning Rate

Diagnosing the problem is the first step. Now, let's look at how to fix it. We won't just guess and check; there are systematic methods for managing the learning rate.

How to Choose the Right Learning Rate in Deep ...

The article 'How to Choose the Right Learning Rate' by Samia Sahin is an excellent practical guide. It covers several state-of-the-art techniques for finding and adjusting the learning rate.

Please read the sections 'Best Practices to Find the Optimal Learning Rate in Neural Networks' and 'Learning Rate Schedulers in PyTorch'. Focus on understanding the core idea behind each of these techniques: Learning Rate Finder: How does it help you find a good starting LR? Learning Rate Decay/Schedules: Why would you want to decrease the LR during training? (e.g., Step Decay, Cosine Annealing) Adaptive Methods: What is the main advantage of optimizers like Adam? Warmup: Why would you start with a small LR and increase it?

Let's summarize these powerful strategies.

1. The Learning Rate Range Test (LR Finder)

This is a fantastic technique to find a good initial learning rate. Instead of guessing, you do a quick experiment:

  • Start with a very small learning rate (e.g., ).
  • Run your model for one epoch, but on each mini-batch, you exponentially increase the learning rate.
  • Plot the loss against the learning rate (on a log scale).

You will get a plot like this:

Learning Rate vs. Loss Plot
This plot shows loss versus the learning rate. Initially, the loss is flat (LR too low), then it drops steeply, and finally, it explodes (LR too high). A good learning rate to start with is in the region of the steepest decline.

A good rule of thumb is to pick a learning rate from the part of the curve where the loss is decreasing most steeply, for instance, or in the plot above.

2. Learning Rate Schedules

It's often beneficial to change the learning rate during training. A common strategy is to start with a relatively high learning rate for faster initial convergence and then decrease it. This allows the model to take smaller, more precise steps as it gets closer to a minimum, helping it to settle in a more optimal spot. This process is called learning rate decay or annealing.

Common schedules include:

  • Step Decay: Reduce the LR by a factor (e.g., 0.1) every N epochs.
  • Cosine Annealing: Smoothly vary the learning rate from a high value to a low value following a cosine curve. This is very popular and effective.
  • ReduceLROnPlateau: Monitor a metric (like validation loss) and reduce the LR if it stops improving for a "patience" number of epochs.

3. Adaptive Optimizers

As mentioned in the article, optimizers like Adam (which we will cover in detail later) automatically adapt the learning rate for each model parameter. They are much less sensitive to the initial learning rate choice and are often a great default choice, simplifying the tuning process.


Deeper Connections: Why Learning Fails

Sometimes, a learning rate appears to be the problem, but the root cause lies deeper. Your engineering background will appreciate that this is a system with interacting parts. A failure in one component can manifest as a problem in another.

Let's watch a segment from a classic lecture by Andrej Karpathy that explores two critical concepts that are deeply intertwined with the learning rate: weight initialization and batch normalization.

CS231n Winter 2016: Lecture 5: Neural Networks Part 2

In this video from the Stanford CS231n course, we'll see how the initial state of the network can doom the learning process before it even begins, and how a clever technique can fix it.

Please watch these two key segments: Weight Initialization (36:52 - 44:47): Pay close attention to what happens when initial weights are too small (activations vanish to zero) or too large (neurons saturate and gradients become zero). If the gradients are zero, what effect will any learning rate have? Batch Normalization (51:06 - 58:29): Focus on how Batch Normalization helps solve the initialization problem. Critically, notice the claim that it 'allows for higher learning rates'. This directly connects to our topic.

This gives us two profound insights:

  1. Bad Initialization Can Mimic a Bad Learning Rate: If your initial weights are chosen poorly, they can cause gradients to become either infinitesimally small (vanishing gradients) or astronomically large (exploding gradients).
    • If gradients vanish, your weights won't update, which looks just like a learning rate that is too low.
    • If gradients explode, your loss will become NaN, which looks just like a learning rate that is too high.
  2. Batch Normalization Stabilizes Training: By re-normalizing the activations within the network at every layer, Batch Normalization ensures that gradients are healthier. This makes the optimization process more stable and less dependent on perfect initialization. As a direct result, you can often get away with using a higher learning rate, which accelerates training.

Conclusion

Mastering the learning rate is a mix of science and art, but the techniques we've discussed today move it firmly into the realm of engineering. You now have a systematic toolkit for diagnosing and addressing one of the most common failure points in model training.

Key Takeaways:

  • Diagnose via Loss Curves: A flat or slowly decreasing loss suggests the learning rate is too low. An erratic, fluctuating, or exploding loss suggests it's too high.
  • Find a Good Starting Point: Use a Learning Rate Range Test to quickly find an optimal range for your initial learning rate, avoiding guesswork.
  • Manage the LR During Training: Use learning rate schedules (like Cosine Annealing or Step Decay) to decrease the LR over time, allowing for faster initial training and finer-grained convergence later.
  • Consider the Whole System: Remember that issues like poor weight initialization can manifest as learning rate problems. Techniques like Batch Normalization can stabilize training and allow for higher, more effective learning rates.

Preview of the Next Lesson:

While adjusting the learning rate helps control the size of our steps, what about the direction? The noisy gradients from Mini-Batch GD can cause our optimization path to zigzag inefficiently. In the next lesson, we will explore how momentum can help accelerate and stabilize gradient descent, effectively smoothing out the path to the minimum.