3.4 Embeddings and softmax
How tokens become vectors on the way in and how the decoder's last vector becomes next-word probabilities on the way out, with one matrix doing both.
What this section says
On the way in, every token is replaced by a learned vector of d_model numbers, its embedding: one row of a table with a row for every token of the vocabulary. On the way out, the decoder's last vector is multiplied by a matrix that gives every token a score, a logit, and the softmax turns the scores into probabilities for the next token. Both steps were usual in translation models.
The paper's twist is to use one matrix for all three: the English embeddings, the German embeddings and the scoring before the softmax (shared embeddings). This works because the vocabulary is shared by both languages, and it saves weights: one table instead of three. Scoring a token is then the dot product of the decoder's vector with that token's own embedding: a word scores high when the decoder's vector points the way its embedding does.
One detail: when the matrix is used to look tokens up, its rows are multiplied by √d_model, about 22.6 for the paper's 512. The paper gives no reason. A common reading is that it makes the embeddings large next to the positional encoding added to them (section 3.5), whose numbers lie between −1 and 1.
In the paper's words
“In our model, we share the same weight matrix between the two embedding layers and the pre-softmax linear transformation, similar to [30].”
“In the embedding layers, we multiply those weights by \(\sqrt{d_{\text{model}}}\).”
Quoted from Vaswani et al., Attention Is All You Need, arXiv:1706.03762v7(opens in a new tab). The figures on this page are redrawn, not copied.
The notation
| Symbol | Meaning | In the paper | Our miniature |
|---|---|---|---|
| \(d_{\text{model}}\) | numbers in each embedding | 512 | 64 |
| \(\sqrt{d_{\text{model}}}\) | what an embedding is multiplied by when it is looked up | about 22.6 | 8 |
| \(\text{vocabulary}\) | the tokens the model knows, shared by both languages | about 37,000 byte-pair pieces | 201 words and marks |
| \(\mathrm{softmax}\) | turns the scores of all tokens into probabilities that add up to 1 | over 37,000 tokens | over 201 tokens |
Try it
- English word
- German word
- in both languages
The two axes keep 10.0 % of the spread of the 64 numbers; the rest is not shown.
The nearest neighbours of “dog” (cosine similarity, all 64 numbers)
- bird · 0.70 · English word
- letter · 0.65 · English word
- man · 0.63 · English word
- teacher · 0.59 · English word
- eats · 0.56 · English word
Every row of our model's shared embedding matrix as a point in two dimensions, English and German words together. Pick a word to see its nearest neighbours: the words the model treats alike.
How our miniature differs
Our vocabulary is 201 whole words and marks, shared by English and German, so the shared matrix has 201 rows of 64 numbers. The map above shows what the model made of it: words group by their job in the sentence and by language, and a word and its translation are not neighbours, because the model never needs them to be.
Prepared in the Course by
Check yourself
Three questions a reader of this section should now be able to answer. The lab checks your answer against the paper and our models.
The scaling
The paper's d_model is 512. By how much is each embedding multiplied when it is looked up? Give two decimals.
One matrix, three uses
How many places use the paper's one shared weight matrix?
Our matrix
How many rows does our model's shared embedding matrix have, one per token of the vocabulary?