Part A · Chapter 5 · The pieces

5. Embeddings

Each token's id picks one row of a big table: its embedding, a vector the model learned. Words the model uses alike get similar rows, as a map of our model's table shows, with one honest surprise.

The model's words on a map
  • 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 word our models know, English and German, as a point: the 64 numbers of its embedding squeezed onto the two directions in which the words are most spread out. English words are circles, German words squares, names and punctuation diamonds. Search for a word, or click one, to see its five nearest neighbours, measured in all 64 dimensions.

The idea

A token id is only a row number. The model's first real step is to look that row up in a table, the embedding table, and to carry the row, a vector of \(d_{\text{model}}\) numbers, from then on. \(d_{\text{model}}\) is 64 in our models and 512 in the paper.

Nobody writes those numbers. They start random, and training nudges them, sentence after sentence, until the rows help the model predict well. Words that can stand in the same places in a sentence end up with rows that point in similar directions, because the model can then treat them alike.

The map shows the result for our paper model. Look up “dog”: its nearest neighbours are mostly other nouns. Look up “sees”: other verbs. The model has grouped its words by the job they do in a sentence.

An honest surprise

You might expect “dog” and its German translation “Hund” to sit side by side: they mean the same thing, and the model keeps English and German in one table. In our model they do not. Their cosine similarity is 0.05, almost unrelated, and Hund's nearest neighbours are a mixed bag with low similarities: Vogel (0.27), Katzen (0.26), a (0.26), child (0.25), big (0.21). Sharing one table does not make a model line two languages up. Ours learned to translate without that, through attention (chapter 18).

A worked example: “She”

“She” has the id 52. Row 52 of the table begins [0.02, 0.08, 0.02, 0.12, …]. The paper multiplies the row by \(\sqrt{d_{\text{model}}}\) (section 3.4), which is 8 for our model, so the encoder starts from [0.18, 0.67, 0.16, 0.99, …], before the position is added (chapter 12).

Compare whole rows with the cosine of chapter 3. The five nearest neighbours of “dog” are bird (0.70), letter (0.65), man (0.63), teacher (0.59), eats (0.56); those of “sees” are reads (0.70), has (0.70), finds (0.67), eats (0.67), carry (0.65).

The formula

The lookup, with the paper's scaling:

x_w = E_{w} \cdot \sqrt{d_{\text{model}}}, \qquad E \in \mathbb{R}^{|V| \times d_{\text{model}}}
\(E\)
the embedding table: one row for every token of the vocabulary, learned in training
\(|V|\)
the number of tokens in the vocabulary
\(E_{w}\)
row w of the table: the embedding of token w
\(\sqrt{d_{\text{model}}}\)
the paper's scaling: 8 for our 64 numbers, about 22.6 for the paper's 512

Looking up a row is the same as multiplying a one-hot vector, all zeros with a single 1 at the token's id, by E: chapter 4's matrix product picks out exactly one row. The paper uses the same table again at the very end, to score every word as the next one (chapter 15).

Why it matters

Embedding APIs return this kind of vector for a whole text, and retrieval systems compare them by cosine similarity. The table is also a real share of a model's size: ours is 201 × 64 = 12,864 numbers; with the paper's vocabulary of about 37,000 tokens and \(d_{\text{model}}\) = 512 it is about 19 million, more than a quarter of the base model's 65 million.

Where it fails

A flat map keeps little. The map's two axes keep only 10.0 % of the spread of the 64 numbers. Two words can look close on the page and be far apart in the dimensions it leaves out; the neighbours are therefore measured in all 64.

Similar is not the same meaning. “old” and “young” are each other's nearest neighbours in our model, with a cosine of 0.64: opposites used in the same places look alike.

One row per word, whatever the sentence. “it” gets the same row whether it stands for a dog or a ball. Telling the two apart is the job of the attention layers that follow (chapter 7).

Remember An embedding is the row of a learned table that a token's id points to. Words used alike get rows that point alike; in our model that means words with the same job and the same language, not a word and its translation.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

Dog's nearest neighbour

In our model, which word is the nearest neighbour of “dog”? Search for it on the map to check.

The size of the table

Our models' embedding table has one row for each of the 201 entries of the vocabulary, and 64 numbers in each row. How many numbers is that?

Your answer
A whole number

The paper's scaling

The paper multiplies every embedding by \(\sqrt{d_{\text{model}}}\). With the paper's \(d_{\text{model}} = 512\), what is that factor? Two decimals.

Your answer
Within 0.01 of the answer

A word and its translation

Are “dog” and “Hund” close to each other in our model's table?

In the paper

Section 3.4 says the model uses “learned embeddings to convert the input tokens and output tokens to vectors of dimension \(d_{\text{model}}\)”, and that “in the embedding layers, we multiply those weights by \(\sqrt{d_{\text{model}}}\)”. The same section shares one weight matrix between both embedding layers and the output, as our models do.

In the paper: section 3.4

Open the Theory tab
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.