Attention Is All You Need · Section 3.4

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.

One matrix, both ways
Sentence
Playback speed

The paper's model has one embedding matrix: a row of 64 numbers for each word of its shared English and German vocabulary. First, the decoder reads a German word: its row is looked up and multiplied by √d_model. Then, at the top of the decoder, the output vector for that position is compared, by a dot product, with every row of the same matrix: one score per word, the logits. The softmax turns the scores into probabilities, and the most likely word is written next, ready to be looked up in the same table.

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].”

Section 3.4 In plain words: One matrix serves three purposes: looking up the input words, looking up the output words, and scoring the next word before the softmax.

“In the embedding layers, we multiply those weights by \(\sqrt{d_{\text{model}}}\).”

Section 3.4 In plain words: When the matrix is used to look words up, its rows are multiplied by the square root of d_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

The symbols of this section, in the paper and in our model
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

The shared embeddings, mapped
  • English word
  • German word
  • in both languages
The model's 197 words placed by their embeddings, projected to two dimensions The two axes keep 10.0 % of the spread of the 64 numbers; the rest is not shown.,,.AAAnnaAnnaApfelApfelAutoAutoAutosAutosBallBallBlumeBlumenBlumenBriefBriefeBriefeBuchBuchBälleBälleBücherBücherDasDasDerDerDieDieEinEineEineErErFrauFrauenFrauenHausHeHeuteHundHundHundeHundeHäuserHäuserJetztKatzeKatzenKindKindKinderKuchenLampeLampenLampenLehrerLenaLenaMannMannMädchenMädchenMännerMännerNowNowOftOftenOftenPaulPaulPferdPferdeSheSieSieSuppeSuppeSuppenTheTheTodayTodayTomTürTürenVogelVogelVögelVögelZeitungZeitungZeitungenaaltealtealtenaltenalteraltesaltesappleapplesballballballsbigbigbirdbirdbirdsblackblackbookbooksbuybuysbuyscakecakescarcarriescarrycarscatcatscatschildchildchildrendasdasdenderderdiedogdogdogsdoordoorsdrawdrawseateatseatseineineineeineneinenesesessenessenfindfindenfindenfindetfindetfindsflowerflowersfreshfrischefrischefrischengirlgirlsgroßegroßengroßengroßergroßergroßesgroßeshashathatheutehorsehorseshousehousesihnisstisstititjetztjetztjungejungejungenjungerjungerjungesjungeskaufenkaufenkauftkauftkleinekleinenkleinerkleinerkleineslamplampslesenlesenletterletterlettersliestliestlikelikesmagmagmanmanmenmögenneueneuenneuesnewnewnewspapernewspapersnowoftoftoftenoldopenopensreadreadsredroterotenrotenrotesrotesschwarzeschwarzenschwarzerschwarzesschwarzesseeseessehensehensiesiehtsmallsoupsoupssoupsteacherteacherteachersthetodaytragentragenträgtträgtwomanwomenyoungzeichnenzeichnenzeichnetzeichnetÄpfelÄpfelöffnenöffnet

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)

  1. bird · 0.70 · English word
  2. letter · 0.65 · English word
  3. man · 0.63 · English word
  4. teacher · 0.59 · English word
  5. 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.

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.

Your answer
Within 0.01 of the answer

One matrix, three uses

How many places use the paper's one shared weight matrix?

Your answer
A whole number

Our matrix

How many rows does our model's shared embedding matrix have, one per token of the vocabulary?

Your answer
A whole number
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.