Machine Learning

Mini-Batch Gradient Descent

Optimize parameters efficiently by estimating gradients on small data batches.

Mini-Batch Gradient Descent

Concept Overview

Mini-Batch Gradient Descent is the practical workhorse of modern deep learning. It strikes a balance between the computational efficiency of Stochastic Gradient Descent (SGD) and the stability of full Batch Gradient Descent. By computing the gradient of the cost function over a small, randomly selected subset (a "mini-batch") of the training data, it enables fast, frequent parameter updates while still providing a sufficiently accurate estimate of the true gradient to ensure steady convergence.

Mathematical Definition

Let J(θ) be the cost function parameterized by θ, and let Bt be a mini-batch of size m randomly sampled from the full training dataset of size n (where 1 < m < n). The update rule at iteration t is given by:

θt+1 = θt - α · ∇J(θt; Bt)
where the mini-batch gradient is:
∇J(θt; Bt) = (1/m) · Σi∈Bt ∇L(f(x(i); θt), y(i))

Here, α is the learning rate, and L is the loss function evaluated on a single training example (x(i), y(i)). Because Bt is drawn uniformly at random, the mini-batch gradient is an unbiased estimator of the full batch gradient.

The Spectrum of Gradient Descent

The choice of batch size defines three distinct optimization regimes:

1. Full Batch Gradient Descent (m = n)

Computes the gradient using the entire dataset. It takes a smooth, direct path toward the minimum but requires a full pass over the data before making a single parameter update. This is computationally intractable for modern datasets (e.g., millions of images).

2. Stochastic Gradient Descent (SGD) (m = 1)

Computes the gradient using a single randomly chosen example. It updates parameters rapidly but follows a highly erratic path due to the high variance of the gradient estimate. The noise can help escape shallow local minima but prevents the algorithm from settling precisely at the exact minimum.

3. Mini-Batch Gradient Descent (1 < m < n)

Typically uses batch sizes like 32, 64, 128, or 256. It offers the best of both worlds: lower variance than SGD (leading to more stable convergence) and much faster updates than Batch GD. Crucially, operations on mini-batches can be highly vectorized to maximize GPU utilization.

Key Concepts

  • Epoch vs. Iteration: An iteration is a single update of the model parameters using one mini-batch. An epochis one complete pass over the entire training dataset. For a dataset of size n and batch size m, one epoch consists of roughly n/m iterations.
  • Gradient Noise: The variance of the mini-batch gradient scales inversely with the batch size (∝ 1/m). A smaller batch size injects more noise into the optimization trajectory, which acts as a form of implicit regularization and aids in finding wider, flatter local minima that generalize better to unseen data.
  • Hardware Utilization: Modern hardware accelerators (GPUs and TPUs) perform matrix multiplications efficiently. A batch size of 1 (SGD) underutilizes the hardware. Mini-batches allow for vectorized operations, achieving higher computational throughput.
  • Learning Rate Scaling: When changing the batch size, the learning rate typically must be adjusted. A common heuristic is the "linear scaling rule": if the batch size is multiplied by k, multiply the learning rate by k to maintain similar training dynamics.

Historical Context

While the steepest descent method dates back to Cauchy in 1847, the stochastic formulation was introduced by Robbins and Monro in 1951. As neural networks grew in popularity in the late 1980s and 1990s, researchers observed that full batch methods were impractically slow, while pure online learning (SGD) was too erratic.

The shift toward mini-batch training accelerated in the late 2000s and early 2010s alongside the rise of General-Purpose GPU (GPGPU) computing. Researchers found that mini-batches mapped perfectly to the SIMD (Single Instruction, Multiple Data) architecture of GPUs, making mini-batch gradient descent the foundational optimization algorithm for the deep learning revolution.

Real-world Applications

  • Deep Learning Training: Every major deep learning framework (PyTorch, TensorFlow) defaults to mini-batch processing for training everything from image classifiers to Large Language Models (LLMs).
  • Distributed Training: Large-scale models are trained by splitting mini-batches across hundreds or thousands of GPUs (Data Parallelism), computing gradients locally, and averaging them to update a global model.
  • Federated Learning: Edge devices (like smartphones) compute gradients on small local mini-batches of user data and send only the aggregated updates to a central server, preserving privacy.

Related Concepts

  • Gradient Descent — the foundational full-batch optimization framework
  • Optimization Landscape — visualizing the cost surfaces navigated by GD
  • Learning Rate Schedules — adapting the step size dynamically during training
  • Backpropagation — the algorithm used to efficiently compute gradients for neural networks

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Mini-Batch Gradient Descent module.

Try Mini-Batch Gradient Descent on Riano →

More in Machine Learning