SGD vs Batch Gradient Descent
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:
One step uses all examples. The gradient is exact and smooth, but each step is expensive.
Stochastic gradient descent (true SGD):
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):
One step uses a random batch of 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
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
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.
Topic Resources
ποΈ
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?