TensorTau PathToAGI Training Deep Networks Level 07

Level 07 · Training Deep Networks · Chapter 091

Stochastic Gradient Descent

If the full dataset tells us the exact gradient, why would we intentionally use a noisy approximation?

Where We Are

Backpropagation gives gradients. We now need a rule for turning them into learning.

Full-batch gradient descent computes

g=1N∑i=1N∇θLi.g=\frac1N\sum_{i=1}^{N}\nabla_\theta L_i.

Then

θ←θ−ηg.\theta\leftarrow\theta-\eta g.

For modern datasets, computing that exact average before every update is expensive.

1. Use a sample instead

Stochastic gradient descent uses one example—or, in practice, a mini-batch—to estimate the full gradient.

gB=1∣B∣∑i∈B∇θLi.g_B=\frac1{|B|}\sum_{i\in B}\nabla_\theta L_i.

If the batch is sampled fairly, this is a noisy but useful estimate of the population direction.

full gradient:       ─────────▶
SGD step 1:        ↗
SGD step 2:           ↘
SGD step 3:        →
long-run trend:       ────────▶

2. Noise is not only a cost

Noisy updates are cheaper, let us update more frequently, and can help optimization avoid some sharp or fragile regions. But too much noise makes training unstable.

3. Learning rate controls step size

Δθ=−ηg.\Delta\theta=-\eta g.

Too small: learning crawls. Too large: loss oscillates or diverges. The optimizer and learning-rate schedule therefore belong together.

4. PyTorch skeleton

optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
optimizer.zero_grad()
loss.backward()
optimizer.step()

backward() computes gradients. step() updates parameters. Keep those jobs conceptually separate.

What to Remember

SGD trades exact gradients for cheap, frequent, noisy estimates. Deep learning became practical partly because this approximation works remarkably well.

Next → Mini-Batches: how much data should each noisy gradient estimate use?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

  1. Explain: Full-batch GD versus SGD.
  2. Calculate: Apply one update for w=2, gradient -3, lr 0.1.
  3. Predict: What happens if learning rate is 100× too large?
  4. Break: Train with deliberately extreme learning rates.
  5. Build: Implement SGD updates without torch.optim.
  6. Investigate: Compare loss trajectories for full-batch and stochastic updates under equal data passes.