Bitpath

Lesson 2 · Unit 01

Adam and AdamW

A step size per weight, and weight decay done right

13 exercisesabout 3 min

What you practise

  • A step size per parameter from running averages of the gradient and its square, bias correction on the first steps, weight decay and why AdamW applies it separately

Try a question from this lesson

Pick one

One weight sees gradients around 100, another around 0.001. SGD gives both the same learning rate, 0.01. What goes wrong?

Hint

An SGD step is the learning rate times the gradient. Work it out for each weight.

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

Adam: a step size per weight

Adam keeps two running averages for every weight: m, the average gradient, and v, the average squared gradient. The step is m divided by the square root of v.

Dividing by the gradient's typical size cancels its scale. Each weight moves about lr per step, whether its gradients are huge or tiny.

Python
m = 0.9 * m + 0.1 * g
v = 0.999 * v + 0.001 * g * g
w = w - lr * m / (v ** 0.5 + 1e-8)

Concept

Weight decay, two ways

Weight decay shrinks every weight a little on each step. It pulls the model toward small weights, which overfit less.

There are two ways to code it. The old way adds decay * w to the gradient. The other shrinks the weight directly, apart from the gradient step. Under plain SGD the two are the same thing. Under Adam they are not.

Python
g = g + decay * w        # in the gradient
w = w - lr * decay * w   # on the weight

10 more exercises in the app

  • 3× multiple choice
  • 3× type the answer
  • 2× true or false
  • 1× fill the blanks
  • 1× match the pairs

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy