Reading the Paper That Started It
What the Attention Equation Actually Computes
Last timeWhat the Paper Was Replacing
Equation one of the paper read matrix by matrix, with the square root in its denominator derived from the variance of a dot product and the mask put where the paper puts it.
Equation one of the paper is a single line, and nearly everything else in the
architecture is bookkeeping arranged around it. It is also short enough to be
memorised without being understood, which is the usual outcome, so it is worth
reading one piece at a time with the shapes carried along.
- applied along each row separately, turning one row of scores into coefficients that sum to one
- every query meets every key, giving one score per ordered pair of positions and nothing else
- the standard deviation of one of those scores, so the division is a change of units
- the coefficients mix the value rows, so each output is a weighted average of values and never of inputs
What the letters are and what shape they have
, and are matrices whose rows are vectors, one row per position.
There are queries of width , and keys of the same width
paired with values of width . Section 3.2.2 fixes the sizes for the
base model: a model width of , heads, and
for each of them. The projection that makes the
queries of one head is therefore a matrix of weights, and the
same for the keys and the values.
The two lengths are separate on purpose. In the self-attention inside either
stack, the queries, keys and values are three projections of the same sequence,
so . In the attention that joins the stacks, described in section 3.2.3,
the queries come from the decoder and the keys and values from the output of the
encoder, so is the length of the output and the length of the input and
the score matrix is not square.
multiplies an by matrix into a by one, giving an
by matrix. Entry of it is the dot product of query with key
, which is a sum of products. Put the standing sizes in. At
positions, one head holds
scores and spends
multiply-adds making them. Across
eight heads that is , which is the
quadratic term in the paper's opening table written out, and across the six
layers of one stack it is scores alive
at the same time, every one of them needed again on the backward pass.
The widths are separate too, and the base model setting both to 64 hides it. A
row of the weight matrix has length , the number of keys, and knows nothing
about . The output row has width . Nothing in the equation requires
the two to match.
From a row of scores to a weighted average
Write for row of the scaled score matrix. The
softmax of that row is
The lesson stops here
9 more paragraphs to go
You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.
The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.
See the planThe contentsThis is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents