TFMLab
0:00 / 0:00

AdamW vs SGD: how an optimizer takes a step #Optimizers#AdamW#SGD

5 Oct 2026Beginner0:59

Two optimizers take 40 steps down the same long valley: SGD uses one step size for every weight, and AdamW gives each weight its own step size and adds a separate weight decay.

An optimizer turns each gradient into a weight update. SGD multiplies every gradient by the same learning rate η. A weight with large gradients, such as the weight on amount, which appears in every transaction, swings back and forth. A weight with small gradients, such as one for a rarely seen country, barely moves.

AdamW keeps two running averages for each weight: one of the gradient (momentum) and one of its squared size. It divides the first by the square root of the second, so every weight takes a step of roughly the same size. The W stands for weight decay. AdamW applies the decay as a separate shrink toward zero instead of mixing it into the gradient.

The paths come from real arithmetic on a toy two-weight loss. They are not from a trained transaction foundation model (TFM).

Transcript

Here is a loss landscape for just two weights: a long, narrow valley, with the lowest point far from the start. The loss is made up, but the arithmetic is real.

Every batch of transactions gives a gradient. Amount appears in every one, so its weight gets big gradients; a rare country gets small ones.

Plain stochastic gradient descent uses one step size for every weight. So it zigzags across the valley on amount, and crawls along it on country.

Adam W keeps running averages of each gradient and of its usual size, and divides one by the other. Every weight takes a similar step toward the bottom.

The W is weight decay: a gentle pull toward zero, kept apart from the gradient.

After forty steps, Adam W sits far closer to the lowest point. A TFM has many weights at very different scales, which is why Adam W is a common choice.

In code (PyTorch)

import torch
model = torch.nn.Linear(16, 1)  # stand-in for a TFM head
sgd   = torch.optim.SGD(model.parameters(), lr=0.15)
adamw = torch.optim.AdamW(model.parameters(), lr=1e-3, betas=(0.9, 0.999), weight_decay=0.01)
for x, y in loader:
    loss = torch.nn.functional.binary_cross_entropy_with_logits(model(x).squeeze(-1), y)
    adamw.zero_grad(); loss.backward(); adamw.step()