Ran Wei/ Maths for AI and LLMs
中文
Maths for AI and LLMs · Optimisation

Module 6Mini-batches, Adam and regularisation

Compare a full gradient to a sampled gradient and trace one adaptive update.

75 minYou need Modules 2 and 5

In this module you will

  • Explain when the mini-batch mean gradient is unbiased
  • Compute bias-corrected Adam moments and their first update
  • Distinguish an L2 penalty from decoupled weight decay in an adaptive update
1

Estimate a full gradient

An empirical loss is the average of nn example losses. Its gradient is the mean of the nn per-example gradients at a fixed parameter vector. If a mini-batch contains bb independently and uniformly drawn indices with replacement, its mean gradient is unbiased. Independence gives covariance Σ/b,\Sigma/b\mathclose{\text{,}} where Σ\Sigma is the covariance of a single draw. Sampling a uniformly chosen subset of fixed size without replacement also gives an unbiased mean, but the covariance uses a finite-population correction.

Example 6.1Average over draws✓ Checked

For scalar per-example gradients 22 and −6,-6\mathclose{\text{,}} the full gradient is −2.-2\mathclose{\text{.}} With one uniform draw the expected update gradient is still −2,-2\mathclose{\text{,}} although neither draw returns that value. The single-draw variance is 16.16\mathclose{\text{.}} For four independent draws, the variance of their average is 4.4\mathclose{\text{.}}

When training for next-token loss, choose the unit whose average defines the objective. For example, sample scored tokens uniformly for a token-weighted estimate. Sampling documents uniformly and averaging their losses equally estimates the mean document loss. Within a document, token gradients can be correlated, so the independent-draw variance calculation requires care.

2

Track gradient moments

Adam keeps two exponential moving averages, initialised at zero. For gradient gt,g_t\mathclose{\text{,}} its first moment is mt=β1mt−1+(1−β1)gtm_t=\beta_1m_{t-1}+(1-\beta_1)g_t and its raw second moment is vt=β2vt−1+(1−β2)gt2.v_t=\beta_2v_{t-1}+(1-\beta_2)g_t^2\mathclose{\text{.}} Squaring and all divisions below are coordinatewise. For 0≤β1,β2<1,0\le\beta_1,\beta_2<1\mathclose{\text{,}} use m^t=mt/(1−β1t)\hat m_t=m_t/(1-\beta_1^t) and v^t=vt/(1−β2t)\hat v_t=v_t/(1-\beta_2^t) to correct for zero initialisation. The update subtracts ηm^t/(v^t+ε)\eta\hat m_t/(\sqrt{\hat v_t}+\varepsilon) for a positive ε.\varepsilon\mathclose{\text{.}} The denominator adapts the scale of the step in each coordinate.

Adding λ∥w∥2/2\lambda\lVert w\rVert^2/2 to the loss adds λw\lambda w to its gradient. Decoupled weight decay instead subtracts ηλw\eta\lambda w directly alongside the parameter update. In plain gradient descent these operations agree. In Adam, adding the penalty to the gradient changes the moment estimates and their scaling, while direct decay leaves the moments based on the data gradient.

Compute itThe first step

At the first step, the bias-corrected first and second moments are g1g_1 and g12g_1^2 for any allowed moment coefficients. Use two coordinates to compare update signs.

Python
from math import sqrt

rate, eps = 0.1, 1e-8
for gradient in (2.0, -6.0):
    first = 0.1 * gradient / (1 - 0.9)
    second = 0.001 * gradient**2 / (1 - 0.999)
    update = -rate * first / (sqrt(second) + eps)
    print(f"{update:.3f}")
Output
-0.100
0.100
3

Exercises

Exercise 6.1★Mean of a batch

A batch has equally weighted gradients of 1,1\mathclose{\text{,}} 33 and 5.5\mathclose{\text{.}} Find the update gradient.

Show solution✓ Checked

Their mean is 3.3\mathclose{\text{.}}

Exercise 6.2★★Raw and corrected moments

Find m1m_1 and m^1\hat m_1 for g1=5g_1=5 and β1=0.8.\beta_1=0.8\mathclose{\text{.}}

Show solution✓ Checked

m1=(1−0.8)5=1m_1=(1-0.8)5=1 and m^1=1/(1−0.8)=5.\hat m_1=1/(1-0.8)=5\mathclose{\text{.}}

Exercise 6.3★★★One decay step

With zero data gradient and zero moment history, find the parameter after a decoupled decay step starting at w=2,w=2\mathclose{\text{,}} with η=0.1\eta=0.1 and λ=0.2.\lambda=0.2\mathclose{\text{.}}

Show solution✓ Checked

Multiplying by 1−ηλ=0.981-\eta\lambda=0.98 gives w=1.96.w=1.96\mathclose{\text{.}}

4

Check yourself

1
What is the expectation of a uniform mini-batch mean at fixed parameters?
2
What does Adam’s vtv_t track?
3
Where does decoupled decay act?
Finished this module?