Gradient Descent & Its Variants

Whole dataset, one example, or a small batch per step, and why batches won.

🟡 intermediate4 min readoptimizationmust-know
Gradient descent trains a model by repeatedly nudging its weights downhill on the loss. The variants differ in how much data they look at per nudge. Batch gradient descent uses the whole dataset each time, which is accurate but slow. Stochastic gradient descent uses a single example each time, which is fast but jumpy. Mini-batch, using around 32 to 256 examples per step, is what everyone actually uses. It fits GPUs well, and its slight noise even helps the model escape poor spots.

What is Gradient Descent?

Gradient Descent finds parameter weights θ\theta that minimize a loss function J(θ)J(\theta) by taking steps proportional to the negative gradient:

θnew=θold−η∇θJ(θ)\theta_{\text{new}} = \theta_{\text{old}} - \eta \nabla_{\theta} J(\theta)

How many data samples should we process before computing ∇θJ(θ)\nabla_{\theta} J(\theta) and updating weights?

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ 1. BATCH GRADIENT DESCENT│ 2. STOCHASTIC (SGD)      │ 3. MINI BATCH GD         │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Uses ALL N samples.      │ Uses 1 single sample.    │ Uses B samples (32-256). │
│ Smooth deterministic path│ High gradient noise.     │ Industry standard!       │
│ Slow on large datasets.  │ Fast updates, poor GPU.  │ Vectorized GPU speed.    │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

1. Batch Gradient Descent

Computes loss and gradients across 100 percent of the training dataset:

∇θJ(θ)=1N∑i=1N∇θL(f(x(i)),y(i))\nabla_{\theta} J(\theta) = \frac{1}{N} \sum_{i=1}^N \nabla_{\theta} L(f(x^{(i)}), y^{(i)})

2. Stochastic Gradient Descent (SGD - Batch Size = 1)

Computes loss and updates weights after every single sample:

θ=θ−η∇θL(f(x(i)),y(i))\theta = \theta - \eta \nabla_{\theta} L(f(x^{(i)}), y^{(i)})

3. Mini-Batch Gradient Descent (The Production Standard)

Splits data into small mini batches of size BB (typically B=32,64,128,256B = 32, 64, 128, 256):

θ=θ−η1B∑i=1B∇θL(f(x(i)),y(i))\theta = \theta - \eta \frac{1}{B} \sum_{i=1}^B \nabla_{\theta} L(f(x^{(i)}), y^{(i)})
  Mini Batch Optimization Pipeline:
  Dataset (1,000,000 Samples) ──► Split into Mini Batches (Batch Size 64)
  - Batch 1: Compute 64 Forward Passes ──► Compute Gradient ──► Update Weights!
  - Batch 2: Compute 64 Forward Passes ──► Compute Gradient ──► Update Weights!

Say this out loud

Batch Gradient Descent computes gradients across the full dataset before taking one step, making it slow on large data. Stochastic Gradient Descent updates weights after every single sample, creating noisy updates that miss GPU parallel speed. Mini Batch Gradient Descent updates weights every 32 to 256 samples, combining fast GPU matrix parallelization with helpful gradient noise.

Followups to expect

  1. How does learning rate interact with mini batch size? When increasing mini batch size by factor K, scale the learning rate up by K (Linear Scaling Rule) alongside warmup to maintain stable weight updates per epoch.
  2. What is an Epoch vs an Iteration? One Epoch is one complete pass through the entire dataset. One Iteration (Step) is a single weight update on one mini batch.

Check yourself

Question 1 of 3

Why is Batch Gradient Descent impractically slow for training machine learning models on 10 million data points?

More in Classical ML

See all →
Bias–Variance Tradeoff4 minOverfitting vs Underfitting3 minLinear Regression4 min