15. Shared embeddings and the output
The paper's model keeps one table of word vectors and uses it twice: to turn a word into a vector on the way in, and to score every word on the way out. The softmax turns those scores into the next word's probabilities.
The idea
A model needs words as vectors on the way in (chapter 5) and words as scores on the way out (chapter 6). The paper's model keeps one table for both: one row per word of the vocabulary. Going in, a word's row is looked up. Coming out, the decoder's last vector is dotted with every row of the same table, and each dot product is that word's score, its logit. A word whose row points the same way as the output vector scores high.
This is called sharing or tying the embeddings. It saves weights, and it makes sense: the vector that means “Hund” coming in is the direction the model should point to when it wants to say “Hund”. Our model's English and German words share one vocabulary, so the encoder, the decoder and the output all use the same matrix, as in the paper.
One detail: on the way in, the rows are multiplied by √d_model, here √64 = 8. The table's numbers are small, and the scaling puts the word on a similar footing to the position waves added next (chapter 12), which lie between −1 and 1.
A worked example: writing “Hund”
The model translates “Anna has a small dog . She likes it .”. When it has written “Anna hat einen kleinen”, the decoder reads “kleinen”: its row begins [−0.05, 0.18, −0.16, −0.24, …], and times 8 it begins [−0.39, 1.40, −1.26, −1.90, …].
Two layers later, the decoder's output for that position begins [0.71, −0.04, 1.06, 0.44, …]. Dotted with each of the 201 rows of the same matrix, it gives 201 scores; the five highest: Hund 8.03, Mann 0.87, Katzen 0.78, Vogel 0.70, tragen 0.68.
The softmax turns them into probabilities: “Hund” gets 94.01 %, “Mann” 0.07 %. The model writes “Hund”, and at the next step it looks up the row of “Hund”, the very row that just won.
The formula
- \(E\)
- the shared embedding matrix: one row per word of the vocabulary, d_model numbers each
- \(E_{w_t}\)
- the row of the word read at position t
- \(\sqrt{d_{\text{model}}}\)
- the scaling on the way in: 8 for our 64 numbers, about 22.6 for the paper's 512
- \(h\)
- the decoder's last output vector for one position
- \(h\,E^{\top}\)
- a dot product of h with every row: one score per word
- \(\operatorname{softmax}\)
- turns the scores into probabilities that add up to 1 (chapter 6)
Why it matters
The last step of every language model is this one: a score for every token of the vocabulary, a softmax, a choice. When an API returns “log probabilities” for the tokens it wrote, they come from here; when you set the temperature, it divides these scores before the softmax. And because the output layer has one row per token, a vocabulary of a hundred thousand tokens means a hundred thousand dot products for every token written.
Sharing the matrix is also a lesson in cost: it removes one or two vocabulary-sized tables, which in a small model can be a large share of all its weights.
Where it fails
- Sharing needs a shared vocabulary. The paper's English–German model shares byte-pair pieces across both languages, as our word list does; two languages with separate vocabularies could share only the decoder side.
- One direction for two jobs. A row has to serve as the word's meaning on the way in and as its target on the way out; many later models tie them, others keep them apart.
- The top score is not the whole story. The model writes the most likely word only when decoding greedily; beam search (chapter 19) can prefer a word with a lower score now for a better sentence later.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
On the way in
In the paper's model, what happens to the embedding rows when a word is looked up?
The paper's scaling
The paper's d_model is 512. What is √d_model? One decimal.
How many scores?
How many scores does our model's output layer compute for one position?
What sharing saves
Without sharing, our model would need three tables of 201 × 64 (encoder input, decoder input, output). How many weights does sharing one table save?
In the paper
Section 3.4: the model uses learned embeddings for the input and output tokens and “the usual learned linear transformation and softmax function to convert the decoder output to predicted next-token probabilities.” Then: “In our model, we share the same weight matrix between the two embedding layers and the pre-softmax linear transformation”, and “In the embedding layers, we multiply those weights by \(\sqrt{d_{\text{model}}}\).”
In the paper: section 3.4