Softmax: from scores to attention weights #Softmax#Attention
Exponentiate, normalise, and see what temperature does.
Transcript
Skip one division, and a Transformer's focus can jump to 99.98 percent. Here, one payment's six raw scores wait for a rule to share them. Softmax works on any scores. To see it clearly, take five.
Same five scores, one dial: as the dial turns, the top share runs from sixty-three percent to ninety-six. By the end, you'll know how softmax shares out any scores, what its dial does, and why our TFM's attention divides by eight.
Five raw scores: two, one, zero point two, three point one, zero point six. Scores can be any size, and any sign. Weights can't. They must be zero or more, and together fill exactly one hundred percent.
Step one: raise e, about two point seven two, to the power of each score. This is the exponential. Every result is positive, and the order stays. A score higher by one gives a bar about two point seven times taller. Three point one becomes twenty-two point two. Two becomes seven point three nine.
Step two: divide each bar by the total, thirty-five point three five. The shares: twenty-one, eight, three, sixty-three and five percent. They stack to exactly one hundred.
Now add ten to every score. No share moves. Only the gaps between scores decide the shares. That's why code subtracts the largest score first. The top becomes zero, every exponential stays at one or below, and nothing overflows.
The dial is a temperature, called T. Every score is divided by T before the exponential. At a temperature of two, the top share falls to forty-one percent. The weight spreads out. Lower, and the top takes more: ninety-nine percent at a quarter. At zero point three five, it's ninety-six.
In attention, the dial is fixed, and here is why. A score is a dot product: multiply matching numbers, then add them up. One random pair gives a narrow spread. In our base design, each score is a dot product of sixty-four numbers. Sixty-four such products, added up, spread eight times wider. We measured it on a hundred thousand random pairs: eight point oh oh. The Transformer paper: for independent random numbers of mean zero and variance one, a dot product’s variance equals how many numbers it adds up.
Undivided, scores eight times wider push the top share to ninety-nine point nine eight percent. And how much that weight can still move falls from zero point two three to zero point oh oh oh two. Learning barely shifts it. Divide by the square root of sixty-four, which is eight, and the spread is back to one. The top share returns to sixty-three percent.
In our code, every attention layer calls PyTorch’s scaled dot-product attention. With no scale given, it divides by the square root of sixty-four. Our base design scores with sixty-four numbers, so every score in every attention layer is divided by eight. This maths is a language model's too. What a TFM scores differs: one payment's fields, and next, a card's past payments.
To recap: exponentiate, divide by the total, and the shares fill one hundred percent. Attention fixes the dial at the square root of sixty-four. Here is the nine hundred dollars again, scored against the card's earlier payments: dot products, before any division. When it arrives at three oh seven, which of them get the weight? That's the next episode.
Sources
- Attention Is All You Need — Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin, NeurIPS 2017