MML204

SGD vs Batch Gradient Descent

⏱6 min readπŸ‘οΈEye-Care Reading Mode

What is it?

Batch gradient descent computes the gradient using all training examples before taking one step. Stochastic gradient descent (SGD) takes a step after every single example, using an approximate gradient. Mini-batch gradient descent is the middle ground β€” a small random sample of examples per step.

In practice, almost all modern ML uses mini-batch gradient descent and simply calls it "SGD".

Why we need it

If your dataset has 10 million examples, computing the full gradient takes 10 million forward passes before you can update the parameters even once.

That is too slow. You would update the model once per hour instead of thousands of times per second.

SGD fixes this by approximating the gradient using a small random subset β€” a mini-batch of, say, 32 or 256 examples. The approximation is noisy, but it is good enough and far faster.


Explained in detail

Batch gradient descent:

w←wβˆ’Ξ±β‹…1nβˆ‘i=1nβˆ‡Li(w)w \leftarrow w - \alpha \cdot \frac{1}{n}\sum_{i=1}^{n} \nabla L_i(w)

One step uses all nn examples. The gradient is exact and smooth, but each step is expensive.

Stochastic gradient descent (true SGD):

w←wβˆ’Ξ±β‹…βˆ‡Li(w)forΒ aΒ singleΒ randomΒ iw \leftarrow w - \alpha \cdot \nabla L_i(w) \quad \text{for a single random } i

One step uses one random example. Very fast, but the gradient is very noisy β€” the path zigzags.

Mini-batch gradient descent (what everyone calls SGD):

w←wβˆ’Ξ±β‹…1bβˆ‘i∈Bβˆ‡Li(w)w \leftarrow w - \alpha \cdot \frac{1}{b}\sum_{i \in \mathcal{B}} \nabla L_i(w)

One step uses a random batch of bb examples (usually 32–512). The gradient is a good approximation, the path is moderately noisy, and the hardware (GPU) runs efficiently.

Batch GD is like asking every student in a school of 10 000 what they think before making one decision. SGD is like asking one random student and acting immediately. Mini-batch is like asking a class of 32 β€” fast, representative, and practical.

Why does noise help?

Surprisingly, the noise in SGD is often useful. It helps the optimizer escape sharp local minima and find flatter, more generalizable solutions. Pure batch gradient descent can get stuck in a sharp minimum that happens to fit the training data but fails on new data.

Batch GD vs Mini-batch SGD β€” 2D Loss Landscape

Steps:0
Batch size:32
Batch Loss:10.262
SGD Loss:10.262
Batch size32
1256
Presets:

Blue = Batch GD (smooth path). Orange = SGD (noisy but fast). The green circle is the minimum. Both start at the same point.

What to try

  • Press Play on both paths. The blue path (Batch GD) moves smoothly and directly. The orange path (SGD) zigzags because each step uses noisy gradient estimates.
  • Use the Batch size slider: larger batch β†’ smoother path, closer to Batch GD. Smaller batch β†’ more noise.
  • Notice that SGD reaches the region near the minimum faster even though it zigzags β€” it takes many more steps but each step is cheap.
  • Try "Escape local min" preset: watch SGD bounce out of a local minimum that traps Batch GD.

Notes & Formula

Mini-batch gradient descent

w←wβˆ’Ξ±bβˆ‘i∈Bβˆ‡Li(w)w \leftarrow w - \dfrac{\alpha}{b} \displaystyle\sum_{i \in \mathcal{B}} \nabla L_i(w)

SymbolMeaning
bBatch size β€” number of examples used per step (e.g. 32, 128, 256)
ℬThe mini-batch β€” a random subset of b training examples
βˆ‡Lα΅’Gradient of the loss on the i-th example alone
epochOne full pass through the entire training dataset
πŸ’‘

Batch size 32–256 is the most common range. Larger batches give smoother gradients but need more memory and can generalize worse. Smaller batches are noisier but can generalize better and use less memory.

ℹ️

One epoch = every training example has been seen once. With 10 000 examples and batch size 100, one epoch = 100 gradient steps.

⚠️

Common mistake: using batch size 1 (true SGD) for a large dataset β€” it is noisy and slow on modern GPUs which are optimized for parallel computation on batches.


Quiz

🧩

Ready to test yourself?

10 questions Β· explanations shown after

🎯

Mastered this concept?

Tick this box when you feel confident explaining it.

πŸ“
Study Notes & Files0 files

Topic Resources

GitHub↗

πŸ—‚οΈ

No notes uploaded for this topic yet

Lecture notes, summaries, and notebook exercises will be added here. You can also view or contribute notes on GitHub.

Was this clear?