Adam Optimizer
In one sentence: Adam keeps a running average of each setting’s slope and of how large those slopes have been, then uses both to choose a step size for every setting separately.
Read it out loud: “theta sub t equals theta sub t minus 1, minus eta times m-hat sub t, divided by the square root of v-hat sub t, plus epsilon. Here theta is the settings of the model, and the subscript t counts steps. Eta, said AY-tuh, is the learning rate. M-hat is the corrected average of the slope, and v-hat is the corrected average of the squared slope. Epsilon, said EP-sih-lon, is a tiny safety number.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Settings, slopes and subscripts
The settings of the model are written , the Greek letter theta, said THAY-tuh. The subscript counts steps, starting from 1, so is the settings after step and is the settings one step earlier. The gradient is the slope of the loss at the settings before step . It says which way the loss rises fastest.
- Running averages and decay rates
A running average is an average that is updated after every step, so each new value enters a little and the old values fade. The fraction of the old average that is kept each step is called the decay rate. Adam uses two decay rates, (beta one, usually 0.9) for the gradient average and (beta two, usually 0.999) for the squared-gradient average. Because is closer to 1, its average changes more slowly.
- Hats, squares and the bias correction, like
A small hat over a letter, as in (read m-hat), marks a corrected version of a value. The correction adjusts the averages for their slow start. A square such as means times . It is never negative, so the average of squares measures how large the slopes have been, while ignoring whether they went up or down.
- Square roots,
The square root is the number that, multiplied by itself, gives . For example , because . Taking a square root turns a squared quantity back into the same size as the original slopes. That is why Adam divides by the square root of the averaged squares.
- Powers of ten, like
Very small numbers are written with a power of ten. The exponent says how many times to divide by 10. So , which is 0.00000001: a decimal point, then seven zeros, then a 1. Adam’s safety number is set to , so it is tiny, but not zero. Writing it this way keeps the number short and easy to read.
- Powers of a decay rate, like
The power means multiplied by itself times. With , after one step it is 0.9, after two steps , and after three steps . These powers shrink toward zero as grows, and they are used in the bias correction.
What each symbol means
| Symbol | Meaning |
|---|---|
| The settings of the model after step . Step counting starts at 1 for the first update. The settings are a list of numbers, and in this toy example they are unitless. | |
| The settings before step , one update earlier. The gradient is measured at this point, so it is the place where the slope is read. | |
| The learning rate, the base size of each step. It is a small positive number, and the Adam paper suggests 0.001 as a starting value. It has no unit in this formula, but it sets how far the settings move per step. | |
| The bias-corrected average of the gradient, read m-hat sub t. It tracks the direction the slope has been pointing recently. Its unit is the same as the gradient’s unit. | |
| The bias-corrected average of the squared gradient, read v-hat sub t. Squares are never negative, so this number measures how large the slopes have been. Its unit is the square of the gradient’s unit. | |
| The square root of the averaged squared gradient. It gives the typical size of the recent slopes in the same unit as the gradient, so dividing by it makes each step depend on how steady the slopes are. | |
| A tiny positive number, usually , added to the bottom of the fraction so the step never divides by zero. It is unitless, and it has almost no effect unless the typical slope size is extremely small. | |
| The gradient at step , the slope of the loss at the settings before that step. It has the unit of the loss divided by the unit of the settings. It is the raw input to both running averages. | |
| The decay rate for the gradient average, the Greek letter beta with subscript 1. Usually 0.9, so the old average keeps 90 percent of its weight each step. | |
| The decay rate for the squared-gradient average, the Greek letter beta with subscript 2. Usually 0.999, so the old squared average keeps 99.9 percent of its weight each step, which makes it change slowly. |
The formula piece by piece
The averaged slope, corrected for the early steps. It points in the direction the loss has been falling, averaged over recent steps, so one noisy slope cannot decide the whole move.
The typical size of recent slopes, with the tiny safety number added so that nothing divides by zero. Taking the square root returns the squared average to the original size of the slopes.
The direction divided by the typical size. When the slopes have been steady, this ratio is close to plus or minus 1, so each step is about the learning rate. When the slopes flip sign, the average shrinks and the step gets smaller.
The learning rate times that ratio: the size of one step, in the units of the settings. Each setting gets its own ratio, so each setting moves at its own pace.
The whole rule in one sentence: the new settings equal the old settings minus the learning rate times the averaged slope divided by its typical size, with a tiny safety number to keep the division safe.
What the formula is doing, and why
What goes in and what comes out. Adam takes the current slope for each setting and returns a new value for that setting. Inside, it keeps two running averages for every setting. The first, , is an average of the slopes , so it shows which way the slope has been pointing. The second, , is an average of the squared slopes, so it shows how large the slopes have been. They update as and . Each new slope takes a small share, and the old average keeps most of its weight.
Why correct the early steps? Both averages start at zero, so the first few values are too small, because they have not yet collected enough slopes. The bias correction fixes this: and . The denominators are small early on, which makes the division bigger, and they approach 1 as grows. So the correction matters mostly for the first steps.
Why divide by the typical slope size? The update divides the averaged slope by the square root of the averaged squared slope, . Dividing by the typical size means that each setting moves by roughly , whatever the raw size of its slopes was. Settings with steady, large slopes do not take huge steps, and settings with small slopes still move. The tiny only keeps the division safe when the typical slope size is almost zero.
Defaults and their limits. The Adam paper by Kingma and Ba reports , and , with the learning rate , as settings that worked well on the problems it tested. Many libraries use the same defaults. They are a starting point, not a guarantee of the best result on a new problem.
An everyday picture
Picture a driver who remembers two things about the road: which way it has been bending, and how bumpy the ride has been. On a smooth stretch the driver steers firmly. On a rough, jerky stretch the driver takes smaller, careful steps. Adam gives each setting in the model its own memory of how bumpy its road has been.
Worked example: Two steps of Adam on one setting
One setting, , has a bowl-shaped loss , with its lowest point at 3. Its slope is . We start at with both averages at zero, and . We use the defaults , and . For easy numbers, the learning rate is . All values are unitless.
- Step 1: find the slope. At the slope is . The slope is negative, which means the loss falls as the setting goes up, so the step should move the setting upward.
- Step 1: update the two averages. The gradient average becomes . The squared average becomes . Each average takes only a small share of the new value, because the decay rates keep most of the old average.
- Step 1: correct the early averages. Both averages are still small, because they started at zero. We divide by and by . That gives and , which match the slope we actually measured.
- Step 1: move the setting. The ratio . So the step is the learning rate times that ratio: , approximately. The first step moves by exactly the learning rate, which is what this ratio is designed to do when the slopes are steady.
- Step 2: find the new slope and update the averages. At the slope is . The gradient average is . The squared average is .
- Step 2: correct the averages and move. Dividing by gives . Dividing by gives . The ratio is , so . The second step is again close to the learning rate, even though the raw slope was 5.8.
Answer: After two steps the setting is . Both steps are almost exactly the learning rate of 0.1, even though the raw slope was 6 at step 1 and 5.8 at step 2. Adam sets the step size from the ratio of the averaged slope to its typical size, not from the raw slope alone.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Skipping the bias correction. Because and start at zero, the early averages are too small, and the first few steps come out several times too big for these defaults.
- Expecting every step to be exactly . The step is about only when the slopes are steady. When the slopes flip sign, shrinks compared with , and the step gets smaller.
- Setting too large. When is small, a big dominates the bottom of the fraction and quietly shrinks every step, most of all for settings with small slopes.
- Keeping the default learning rate without checking it. The defaults come from the paper’s test problems and are a starting point, not a rule for every model.
Where you will meet it
- Training large text models, such as transformers, where Adam or a close variant called AdamW is a common choice.
- Training image and speech models, including many deep networks for recognizing pictures and sounds.
- Deep learning libraries, where Adam is one of the built-in optimizers, the rules that update the settings during training.
- Fine-tuning pretrained models, which means adjusting an existing model on new data, often with a small learning rate and Adam together.
Questions people ask
Why is it called Adam?
The name comes from adaptive moment estimation. The first moment is the averaged slope and the second moment is the averaged squared slope, so the name describes the two running averages that Adam keeps.
Does Adam always beat plain gradient descent?
No. Adam often trains quickly with little tuning, but on some problems gradient descent with momentum on small batches reaches a better final result. Trying both is sensible when the model matters.
What is the difference between m and v?
is the average of the slope itself, which carries the direction. is the average of the squared slope, which carries the size. Adam uses both to decide how far each setting moves.
Do I need to change beta 1 and beta 2?
Usually not at first. The defaults 0.9 and 0.999 are the starting point in most libraries.
Related formulas
- Momentum Optimizer: Momentum lets gradient descent remember its recent steps, so it speeds up where the slope keeps pointing the same way and wobbles less where the slope keeps flipping.
- Gradient Descent: Gradient descent nudges a model’s settings a small step in the direction that makes the error smaller, then repeats that step again and again until the error stops falling.
- Backpropagation and the Chain Rule: Backpropagation finds how much each weight affects the error by multiplying the small slopes along the chain from that weight to the loss, one link at a time.