Skip to main content
Open In Colab Training a model means searching the weight space for the parameters that minimize a loss. This section builds that search from the ground up: plain gradient descent on a convex objective, then the stochastic and mini-batch variants that let it scale to large datasets, and finally the same loop expressed with PyTorch. It is the engine behind the linear regression fits and, eventually, deep-network training. The loss being minimized is the empirical risk defined on the empirical and expected risk page.

Gradient descent

Optimization minimizes a function L(θ)L(\boldsymbol{\theta}) by adjusting θ\boldsymbol{\theta}. The derivative L(w)L'(w) gives the slope: to first order L(w+ϵ)L(w)+ϵL(w)L(w + \epsilon) \approx L(w) + \epsilon\, L'(w), so moving a small step against the derivative decreases LL. For a vector of weights the gradient θL\nabla_{\boldsymbol{\theta}} L collects all partial derivatives, and gradient descent repeatedly steps downhill, θk+1=θkηθL(θk),\boldsymbol{\theta}_{k+1} = \boldsymbol{\theta}_k - \eta\, \nabla_{\boldsymbol{\theta}} L(\boldsymbol{\theta}_k), where the learning rate η\eta scales the step. On a convex bowl the iterates march steadily to the unique minimum.
Output from cell 3

The learning rate

The learning rate sets the size of each step and is the most important knob. Too small and progress is glacial; too large and the steps overshoot the minimum and the iterates oscillate or diverge. For the quadratic above the update contracts the distance to the minimum by a factor 1η|1 - \eta| per step, so anything with η>2\eta > 2 blows up.
Output from cell 5

From full-batch to stochastic

In learning the loss is an average over the training set, and so is its gradient. For squared error with mm examples, L(θ)=1mi=1m(θxiyi)2,θL=2mi=1m(θxiyi)xi.L(\boldsymbol{\theta}) = \frac{1}{m}\sum_{i=1}^{m}\big(\boldsymbol{\theta}^{\top}\mathbf{x}_i - y_i\big)^2, \qquad \nabla_{\boldsymbol{\theta}} L = \frac{2}{m}\sum_{i=1}^{m}\big(\boldsymbol{\theta}^{\top}\mathbf{x}_i - y_i\big)\mathbf{x}_i. Full-batch gradient descent sums over all mm examples for every single update. With millions of examples that is prohibitive. Instead you estimate the gradient on a random mini-batch B\mathcal{B} of size BB; the special case B=1B = 1 is stochastic gradient descent. The mini-batch gradient is an unbiased but noisy estimate of the full gradient, so each step is cheap and frequent, at the cost of a wandering trajectory. To see all three on one picture, fit a straight line y=θ0x+θ1y = \theta_0 x + \theta_1 to noisy data: the loss over the two parameters is a bowl whose contours you can draw.
Output from cell 7

Gradient noise and learning-rate schedules

With a constant learning rate the stochastic gradient never vanishes, even at the optimum a single mini-batch still pulls in some direction. The iterate therefore settles into a noise ball around the minimum whose radius scales with η\eta, bouncing rather than converging. Shrinking the learning rate over time, for example ηt=η0/(1+γt)\eta_t = \eta_0 / (1 + \gamma t), lets the ball contract so the iterate homes in. This is exactly the gap between the constant-rate and converged fits seen on the regression SGD example.
Output from cell 9

The same loop in PyTorch

Frameworks automate two things you did by hand: autograd computes θL\nabla_{\boldsymbol{\theta}} L from the forward computation, and an optimizer object applies the update rule. The mechanics are identical, loss.backward() fills in the gradient and optimizer.step() performs θθηθL\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} - \eta\nabla_{\boldsymbol{\theta}} L.

Takeaways

  • Gradient descent steps against the gradient; the learning rate η\eta sets the step size, and too large a value diverges.
  • The training loss is an average over data, so its gradient is too. Full-batch updates cost a complete pass, while mini-batch and stochastic updates trade gradient noise for cheap, frequent steps that make far more progress per unit of computation.
  • A constant learning rate leaves stochastic gradient descent circling in a noise ball; a decaying schedule lets it converge.
  • Autograd plus an optimizer object package this exact loop. The optimizer zoo section adds momentum and adaptive methods that handle the ill-conditioned, ravine-shaped landscapes where plain SGD struggles.
Key references: (Bottou et al., 2016; Goodfellow et al., 2014; Ruder2016-overview)

References

  • Bottou, L., Curtis, F., Nocedal, J. (2016). Optimization Methods for Large-Scale Machine Learning.
  • Goodfellow, I., Vinyals, O., Saxe, A. (2014). Qualitatively characterizing neural network optimization problems.