ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Deep Learning
Chapter 1: Neural Network Foundations
From Machine Learning to Deep Learning
Biological Inspiration: The Neuron
The Artificial Neuron: A Mathematical Model
The Perceptron: The Simplest Neural Network
Limitations of Single-Layer Perceptrons
Multi-Layer Perceptrons (MLPs): Adding Depth
Hands-on Practical: Building a Simple Perceptron Model
Quiz for Chapter 1
Chapter 2: Activation Functions and Network Architecture
The Role of Activation Functions
Sigmoid Activation
Hyperbolic Tangent (Tanh) Activation
Rectified Linear Unit (ReLU)
Variants of ReLU (Leaky ReLU, PReLU, ELU)
Choosing the Right Activation Function
Understanding Network Layers: Input, Hidden, Output
Designing Feedforward Network Architectures
Hands-on Practical: Implementing Different Activations
Quiz for Chapter 2
Chapter 3: Training Neural Networks: Loss and Optimization
Measuring Performance: Loss Functions
Common Loss Functions for Regression (MSE, MAE)
Common Loss Functions for Classification (Cross-Entropy)
Optimization: Finding the Best Weights
Gradient Descent Algorithm
Learning Rate
Stochastic Gradient Descent (SGD)
Challenges with Gradient Descent
Hands-on Practical: Visualizing Gradient Descent
Quiz for Chapter 3
Chapter 4: Backpropagation and Advanced Optimization
Calculating Gradients: The Chain Rule
Computational Graphs
The Backpropagation Algorithm Explained
Forward Pass vs. Backward Pass
Gradient Descent with Momentum
RMSprop Optimizer
Adam Optimizer
Choosing an Optimization Algorithm
Hands-on Practical: Backpropagation Step-by-Step
Quiz for Chapter 4
Chapter 5: Building and Training Deep Neural Networks
Introduction to Deep Learning Frameworks (TensorFlow/Keras, PyTorch)
Setting up the Development Environment
Preparing Data for Neural Networks
Defining a Feedforward Network Model
Weight Initialization Strategies
Compiling the Model: Loss and Optimizer Selection
Training the Model: The fit Method
Monitoring Training Progress (Loss and Metrics)
Evaluating Model Performance
Hands-on Practical: Training a Classifier on MNIST
Quiz for Chapter 5
Chapter 6: Regularization and Improving Performance
The Problem of Overfitting
Regularization Techniques Overview
L1 and L2 Regularization
Dropout Regularization
Early Stopping
Batch Normalization
Hyperparameter Tuning Fundamentals
Strategies for Hyperparameter Search (Grid Search, Random Search)
Hands-on Practical: Applying Dropout and Early Stopping
Quiz for Chapter 6
Chapter 7: Introduction to Specialized Architectures
Limitations of Feedforward Networks
Convolutional Neural Networks (CNNs): Motivation
Core CNN Operations: Convolution
Core CNN Operations: Pooling
Typical CNN Architecture
Recurrent Neural Networks (RNNs): Motivation
The Concept of Recurrence and Hidden State
Basic RNN Architecture
Challenges with Simple RNNs (Vanishing/Exploding Gradients)
Overview: LSTMs and GRUs
Quiz for Chapter 7
Designing and Optimizing a Deep Neural Network from Scratch

Gradient Descent with Momentum

Standard Gradient Descent updates weights based solely on the gradient calculated at the current position. While this approach works, it can lead to inefficient training, especially in certain types of loss surfaces. For instance, imagine a narrow ravine or valley in the loss surface. Gradient Descent tends to oscillate back and forth across the steep walls of the ravine, making slow progress along the bottom towards the minimum. It can also get stuck in local minima or slow down considerably on saddle points.

The Intuition: Adding Inertia

To address these issues, we can introduce the concept of momentum, borrowing intuition from physics. Imagine a ball rolling down a hill. Instead of just calculating the steepest slope at its current location and moving slightly in that direction (like standard GD), the ball has momentum. Its current velocity influences its movement in the next step. If it's already moving quickly in a certain direction, it tends to keep going that way, even if the immediate slope changes slightly. This momentum helps it:

  1. Accelerate down consistent slopes.
  2. Dampen oscillations by averaging out opposing gradient directions over time.
  3. Overshoot small local minima or plateaus due to its built-up speed.

Gradient Descent with Momentum applies this physical analogy to the optimization process. It introduces a "velocity" vector, vvv, which accumulates an exponentially decaying moving average of past gradients. The weight update then incorporates both the current gradient and this velocity.

How Momentum Works

The update rule for Gradient Descent with Momentum involves two steps at each iteration ttt:

  1. Update the velocity vector vtv_tvt​:

    vt=βvt−1+η∇L(wt−1)v_t = \beta v_{t-1} + \eta \nabla L(w_{t-1})vt​=βvt−1​+η∇L(wt−1​)

    Here:

    • vtv_tvt​ is the velocity vector at time step ttt.
    • vt−1v_{t-1}vt−1​ is the velocity vector from the previous time step (initialized to zero).
    • β\betaβ is the momentum coefficient, a hyperparameter usually between 0.5 and 0.99 (commonly 0.9). It determines how much past velocity is retained. A higher β\betaβ means past gradients have more influence.
    • η\etaη is the learning rate.
    • ∇L(wt−1)\nabla L(w_{t-1})∇L(wt−1​) is the gradient of the loss function with respect to the weights www at the previous time step t−1t-1t−1.

    This step essentially calculates the new velocity by taking a fraction (β\betaβ) of the old velocity and adding the scaled current gradient (η∇L(wt−1)\eta \nabla L(w_{t-1})η∇L(wt−1​)). If the current gradient points consistently in the same direction as the previous velocity, the velocity magnitude increases. If the gradient direction oscillates, the velocity tends to be dampened because opposing gradient terms partially cancel out over time.

  2. Update the weights wtw_twt​:

    wt=wt−1−vtw_t = w_{t-1} - v_twt​=wt−1​−vt​

    The weights are updated by moving in the direction of the newly computed velocity vector vtv_tvt​.

By incorporating the velocity vtv_tvt​, which holds information about recent gradient history, the updates become smoother and faster, particularly along directions where the gradient is consistent.

Visualizing the Effect of Momentum

For example, optimizing a function with a narrow valley. Standard Gradient Descent might bounce between the walls, while Momentum takes a more direct route.

Optimization paths on a sample loss surface. Standard Gradient Descent (pink) oscillates across the narrow valley, making slow progress towards the minimum (near origin). Momentum (blue) dampens these oscillations and accelerates along the valley floor, taking a more direct path.

The Momentum Coefficient β\betaβ

The momentum coefficient β\betaβ controls the influence of past gradients.

  • If β=0\beta = 0β=0, the update rule reduces to standard Gradient Descent (vt=η∇L(wt−1)v_t = \eta \nabla L(w_{t-1})vt​=η∇L(wt−1​), wt=wt−1−η∇L(wt−1)w_t = w_{t-1} - \eta \nabla L(w_{t-1})wt​=wt−1​−η∇L(wt−1​)).
  • As β\betaβ approaches 1, past gradients are given more weight, leading to smoother updates but potentially causing the optimizer to overshoot the minimum if the learning rate is also high.

Common values for β\betaβ are 0.9 or even higher, like 0.99. It often works well to start with 0.9 and potentially increase it during training. This parameter, like the learning rate, may require tuning for optimal performance on a specific problem.

Implementation in PyTorch

Using momentum in frameworks like PyTorch is straightforward. When defining the optimizer, you simply specify the momentum parameter.

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

# Assume 'model' is your defined neural network (nn.Module)
# Assume 'learning_rate' is your chosen learning rate (e.g., 0.01)

# Define the optimizer using SGD with momentum
momentum_coefficient = 0.9
optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum_coefficient)

# --- Inside your training loop ---
# loss.backward() # Compute gradients

# optimizer.step() # Update weights using SGD with momentum
# optimizer.zero_grad() # Reset gradients for next iteration

By simply adding the momentum argument to the optim.SGD constructor, the optimizer automatically implements the velocity calculation and weight update steps described earlier.

Gradient Descent with Momentum is a significant improvement over standard gradient descent, often leading to faster convergence and better navigation of complex loss surfaces. While it helps overcome many challenges, further refinements led to the development of adaptive methods like RMSprop and Adam, which we will explore next.

  • On the importance of initialization and momentum in deep learning, Ilya Sutskever, James Martens, George Dahl, Geoffrey Hinton, 2013 Proceedings of the 30th International Conference on Machine Learning (ICML), Vol. 28 - A seminal paper that highlights the significant benefits of using momentum, along with proper initialization, for effectively training deep neural networks.
  • Deep Learning, Ian Goodfellow, Yoshua Bengio, Aaron Courville, 2016 (MIT Press) - A standard textbook in deep learning, offering a detailed explanation of optimization algorithms, including the mechanics and advantages of momentum.
  • CS231n: Convolutional Neural Networks for Visual Recognition - Optimization, Stanford University CS231n Course Staff, 2023 - Part of the highly regarded Stanford CS231n course notes, providing an accessible and well-illustrated explanation of Gradient Descent with Momentum.
  • torch.optim.SGD, PyTorch Developers, 2024 - The official PyTorch documentation for the Stochastic Gradient Descent optimizer, clearly outlining the momentum parameter and its direct use in implementation.

© 2026 ApX Machine Learning