๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #96

Mini-Batch Gradient Descent

Mini-Batch Gradient Descent is the practical compromise between batch and stochastic gradient descent โ€” and it's what virtually every real deep learning training run actually uses, whether or not the optimizer is casually called "SGD" in code.

Formula

\[ \nabla L(\mathbf{w}) \approx \frac{1}{B}\sum_{i=1}^{B} \nabla L_i(\mathbf{w}) \qquad \text{(a mini-batch of size } B\text{, e.g. 32, 64, 128, 256)} \]

Instead of one example (SGD) or the whole dataset (batch GD), each update uses a small, randomly sampled subset โ€” a "mini-batch."

Why This Specific Compromise Wins

Batch GDSGDMini-Batch GD
Gradient noiseNoneHighModerate โ€” averages out much of SGD's noise
Update frequencyOnce per epochOnce per exampleOnce per batch โ€” frequent, but not wastefully so
GPU/hardware utilizationGood (large matrix ops), but rare updatesPoor โ€” one example doesn't fill a GPU's parallel capacityExcellent โ€” batches are sized to exploit GPU parallelism efficiently
Memory requirementMust hold the full dataset's gradient computationMinimalModerate, tunable via batch size

Mini-batches, especially at sizes like 32โ€“256, are large enough to average out most of SGD's noisy variance and small enough to fit comfortably in GPU memory while keeping matrix operations efficiently parallelized โ€” the sweet spot that made it the default.

Code โ€” Connecting to PyTorch's DataLoader

import torch
from torch.utils.data import DataLoader, TensorDataset

X = torch.randn(10000, 5)
y = torch.randn(10000)
dataset = TensorDataset(X, y)
loader = DataLoader(dataset, batch_size=64, shuffle=True)   # this IS mini-batch gradient descent

w = torch.zeros(5, requires_grad=True)
optimizer = torch.optim.SGD([w], lr=0.01)

for epoch in range(5):
    for X_batch, y_batch in loader:   # each iteration = one mini-batch update
        predictions = X_batch @ w
        loss = ((predictions - y_batch) ** 2).mean()
        loss.backward()
        optimizer.step()
        optimizer.zero_grad()

Note that torch.optim.SGD is the same optimizer class used here, whether you feed it one example, a mini-batch, or the whole dataset at once โ€” the "SGD vs mini-batch vs batch" distinction is about how you construct your DataLoader and training loop, not a different optimizer class.

Choosing a Batch Size

Batch SizeEffect
Small (e.g. 8โ€“32)More noise (closer to SGD), more frequent updates, lower memory use
Large (e.g. 256โ€“1024+)Smoother gradient estimate (closer to batch GD), fewer updates per epoch, higher memory use, often requires a proportionally larger learning rate

Common Mistakes

  • Choosing a batch size purely for GPU memory convenience without considering its effect on gradient noise and generalization โ€” very large batch sizes can sometimes generalize slightly worse without other adjustments (like learning rate scaling).
  • Forgetting to shuffle the dataset before batching โ€” without shuffling, each epoch sees mini-batches in the same fixed order, which can introduce unwanted correlation between consecutive updates.

Interview Relevance

Q: "When people say a model was trained with 'SGD,' what are they usually actually describing?" Almost always mini-batch gradient descent โ€” using an optimizer like torch.optim.SGD with a DataLoader that yields batches of, say, 32 or 64 examples per update, not literal single-example stochastic gradient descent. The "SGD" naming is a historical holdover.

Practice Question

A dataset has 50,000 examples. With a batch size of 100, how many weight updates happen in one epoch?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Mini-Batch Gradient Descent โ€“ FAQs

Quick answers about learning Mini-Batch Gradient Descent in Deep Learning.

This free note from CodingNow 2.0 explains Mini-Batch Gradient Descent in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Mini-Batch Gradient Descent, is 100% free with no signup required.
With focused practice, most students grasp Mini-Batch Gradient Descent in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now