What is Momentum in Optimization for Deep Learning?

Introduction

Momentum is an optimization technique that helps accelerate gradient descent by taking past gradients into account. It’s inspired by the concept of momentum in physics, where an object continues moving in its direction unless acted upon by an external force.

In the context of neural networks, momentum builds up velocity in directions with consistent gradients and dampens oscillations in directions with fluctuating gradients. This results in faster convergence and smoother updates, especially in loss landscapes with ravines or steep curves.

How Momentum Works

The standard Stochastic Gradient Descent (SGD) update rule: 

$$\theta = \theta – \eta \cdot \nabla J(\theta)$$

With Momentum, it introduces a velocity term v:

$$v_t = \gamma \cdot v_{t-1} + \eta \cdot \nabla J(\theta)$$ $$\theta = \theta – v_t$$
  • $\gamma$: Momentum coefficient (commonly 0.9)
  • $\eta$: Learning rate
  • $\nabla J(\theta)$: Gradient of the loss with respect to parameters
  • $v_t$: Accumulated velocity (updated every step)

Intuition Behind Momentum

momentum in gradient descent
Title: Illustration of how momentum works in gradient descent
Source: Gradient Descent with Momentum, RMSprop And Adam Optimizer

Imagine rolling a ball down a hilly surface:

  • At the start, momentum has no effect ($v_0 = 0$), so the first step is just gradient descent.
  • In subsequent steps, the gradient continues to push the parameters in the descent direction, and velocity builds up.
  • When the gradient becomes zero at a local minimum, ordinary SGD would stop. But momentum keeps the ball moving, possibly helping it escape local minima.
  • In regions where the gradient points opposite to the momentum, if the accumulated velocity is strong enough, the model can overshoot, helping it jump out of narrow or poor local minima.

This is especially helpful for loss surfaces with sharp curvature, where pure gradient descent would oscillate or slow down. Momentum smooths the path, reducing oscillation and allowing faster convergence in the desired direction.

Code Example

This code snippet demonstrates how to apply Stochastic Gradient Descent (SGD) with Momentum in PyTorch for training a simple neural network. The model consists of two linear layers with a ReLU activation in between. The optimizer is configured with a learning rate of 0.01 and a momentum factor of 0.9, which helps accelerate training and reduce oscillations. A batch of dummy input data and random target labels is used to perform a single training step.

Python
import torch
import torch.nn as nn
import torch.optim as optim

# Simple neural network
model = nn.Sequential(
    nn.Linear(784, 128),
    nn.ReLU(),
    nn.Linear(128, 10)
)

# Loss function
criterion = nn.CrossEntropyLoss()

# Optimizer with Momentum
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

# Dummy input and target
x = torch.randn(64, 784)  # batch of 64 samples
y = torch.randint(0, 10, (64,))  # random targets

# Training step
optimizer.zero_grad()
output = model(x)
loss = criterion(output, y)
loss.backward()
optimizer.step()

Advantages of momentum

Momentum helps optimize neural networks by accelerating convergence, especially in scenarios involving steep slopes or narrow valleys in the loss landscape. By accumulating past gradients, it reduces oscillations in directions with high curvature and allows the model to build velocity, helping it escape local minima. Momentum-based techniques also integrate well with more advanced optimizers like Nesterov or Adam, which further improve training stability and efficiency.

Disadvantages of momentum

Despite its benefits, momentum has its drawbacks. One of the main issues is overshooting, where the accumulated velocity can push the model too far past the minimum. Additionally, momentum introduces an extra hyperparameter (the momentum coefficient), which requires careful tuning. If set improperly, it can lead to divergence or unstable training behavior.

Video Explanation

  • The video by Andrew Ng clearly explains momentum in gradient descent, using vivid examples and intuitive visualizations to illustrate how it accelerates convergence and reduces oscillations during optimization (Runtime: 10 mins)
YouTube video
Gradient Descent with Momentum by Andrew Ng

Related Questions:

Author

  • Brown University CS

    Machine Learning Content Writer

Help us improve this post by suggesting in comments below:

– modifications to the text, and infographics
– video resources that offer clear explanations for this question
– code snippets and case studies relevant to this concept
– online blogs, and research publications that are a “must read” on this topic

Leave the first comment

Partner Ad
Find out all the ways that you can
Contribute