Math for AILoading progress…

Stochastic gradients

Perform gradient descent updates and explain learning rate, stochastic gradients, momentum and Adam.

Why estimate the gradient

Training losses are usually averages over a dataset: L(θ) = (1/N) Σ L_i(θ). The full gradient averages N per-example gradients, so one update costs N gradient evaluations. A minibatch averages only k randomly chosen examples. It is a cheaper estimate of the same gradient: on average it is exactly right, but any single minibatch can point somewhat elsewhere.

Our toy dataset has 8 points in the plane, and each example's loss is ½·|θ − c|². The full-data minimum is the mean of the points, [1.5, 1.25]. At θ = [-4, -3] the full gradient is [-5.5, -4.25], while the single example #3 alone says [-7, -5].

Watch the noise

iteration 0
full batch (all 8)minibatch of 1true optimum [1.5, 1.25]
Press Step. Rings mark the examples used in the current minibatch. excess loss: minibatch 24.156 full batch 24.156 noise floor over steps 41 to 60 (minibatch): 1.154

Same seed, same settings, same path: re-run it and nothing changes. Change the seed and the minibatch path changes, while the full-batch path (blue) never does.

Blue: full batch, always the same. Orange: minibatches drawn with a seeded random generator, so the run is repeatable. Rings mark the examples in the current batch. Press Step, then change the batch size or the seed.

With a constant learning rate the minibatch path reaches the neighbourhood of the optimum quickly and then keeps jittering around it, never settling. The size of that jitter, the noise floor, grows with the learning rate and shrinks with the batch size. Two standard remedies are a larger batch and a learning rate that decays over time.

Predict first. Averaged over every possible minibatch of size 2, how does the minibatch gradient compare with the full gradient?

Common mistakes

  • Treating one minibatch as the truth. The minibatch gradient and the minibatch loss are estimates. They bounce from step to step even when training is healthy.
  • Comparing two runs that differ in luck. Different seeds or shuffling give different paths. When comparing batch sizes or learning rates, fix the seed, and repeat with several seeds before believing a small difference.
  • Mixing up the cost. Halving the batch size halves the cost of a step, but the estimate gets noisier. There is no free choice.
Chapter context

The gradient points toward the steepest local increase under Euclidean geometry. To lower a differentiable loss, a basic optimizer steps in the opposite direction. A local direction says little about how large a safe step can be.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

θt+1=θt−η∇L(θt).\theta_{t+1}=\theta_t-\eta\nabla L(\theta_t).

Adam uses mt=β1mt−1+(1−β1)gtm_t=\beta_1m_{t-1}+(1-\beta_1)g_t and vt=β2vt−1+(1−β2)gt2v_t=\beta_2v_{t-1}+(1-\beta_2)g_t^2, coordinatewise. Divide by 1−β1t1-\beta_1^t and 1−β2t1-\beta_2^t before using m^t/(v^t+ϵ)\hat m_t/(\sqrt{\hat v_t}+\epsilon) in the update.

For the special quadratic L(θ)=(θ−c)2L(\theta)=(\theta-c)^2, the residual follows θt+1−c=(1−2η)(θt−c)\theta_{t+1}-c=(1-2\eta)(\theta_t-c). Starting away from c, convergence requires ∣1−2η∣<1|1-2\eta|<1, or 0<η<10<\eta<1. This interval depends on this particular curvature and loss normalization.

Worked example

For L=(theta−3)^2, start at 0 and use eta=0.25. The gradients are −6, −3 and −1.5. Three updates produce theta values 1.5, 2.25 and 2.625. Their losses are 2.25, 0.5625 and 0.140625. A fixed learning rate works here because the example is a simple quadratic.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose
theta = 0.0
positions = []
for _ in range(3):
    theta -= 0.25 * 2*(theta-3)
    positions.append(theta)
assert positions == [1.5, 2.25, 2.625]
assert isclose((theta-3)**2, 0.140625)

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Subtracting loss instead of its gradient is not gradient descent. Squared gradients in Adam are coordinatewise. A zero gradient may identify a saddle point or maximum, not just a minimum.