# Word Embeddings

> Guess which two words in your grid behave alike, prove it by swapping them in your text, and check one distance by hand, then let the widget do all the pairs at once.

Your bigram <GlossaryTerm id="grid">grid</GlossaryTerm> already describes every word in it: the row says
what tends to follow that word, the column says what tends to come before it.
Two words with much the same row and much the same column are, as far as the
model is concerned, the same kind of word. This module finds a pair like that
and checks it three ways: by eye, by swapping the two words in the text, and by
one piece of arithmetic.

![](../../assets/images/hero-word-embeddings.avif)

## You will need

- your completed [bigram grid](https://www.llmsunplugged.org/modules/training/), built from a few sentences
  or more
- the original text you trained on
- pen and paper
- a screen for the last step (one between a pair is plenty)

## Your goal

Predict which two words in your grid behave most alike (call them the twins),
then test the prediction. Stretch goal: find a close pair in the widget that
nobody in the room guessed, and work out what the two words have in common.

## Key idea

A word's row is a list of numbers: how often each other word followed it. That
list is the word's <GlossaryTerm id="embedding">embedding</GlossaryTerm>---a
fingerprint of the company it keeps. Two words with similar fingerprints appear
in the same contexts, so you can swap them in the text and it still reads.
Because they occupy the same slots in a sentence, they usually mean similar
things.

## Find the twins (5 min)

Pen and paper, no arithmetic. Look down your grid and pick the two words you
think behave most alike:

- compare **rows**: which two words are followed by the same words?
- compare **columns**: which two words follow the same words?

A twin pair matches on both. Write your guess down before you go any further,
since an unwritten guess tends to shift once the answer appears.

## The swap test (5 min)

Take the original text, swap your two candidate words everywhere they appear,
and read it aloud. If it still reads as English---odd perhaps, or untrue, but
grammatical---they're twins. If it collapses, they're not.

For the text `the cat sat . the dog sat . the cat ran . the dog slept .`:

- swap `cat` and `dog`: "the dog sat. the cat sat. the dog ran. the cat slept."
  (still grammatical)
- swap `cat` and `sat`: "the sat cat. the dog cat. the sat ran…" (breaks down)

Words that keep the same company can stand in for each other. "The cat slept"
may be false about your particular cat, yet the swap passes because the test
checks only whether the sentence holds together.

## One distance by hand (5 min)

Now put a number on it. The distance between two words is the sum of the
absolute differences between their rows, cell by cell, with blanks counting as
zero. Here is the grid for the same short text:

<LmGrid tokens="the cat sat . the dog sat . the cat ran . the dog slept ." />

Reading the rows off as vectors, with the columns in the order `the` `cat`
`sat` `.` `dog` `ran` `slept`:

- `cat` → 0 0 1 0 0 1 0
- `dog` → 0 0 1 0 0 0 1
- `sat` → 0 0 0 2 0 0 0
- `the` → 0 2 0 0 2 0 0

And the arithmetic:

- d(`cat`, `dog`) = 0 + 0 + 0 + 0 + 0 + 1 + 1 = **2**
- d(`cat`, `sat`) = 0 + 0 + 1 + 2 + 0 + 1 + 0 = **4**
- d(`cat`, `the`) = 0 + 2 + 1 + 0 + 2 + 1 + 0 = **6**

The twins score lowest. Do the same for your own twin pair and for one pair
you're confident isn't, and check the numbers agree with your ears.

<Callout type="warning" title="The number only reads the rows">

This distance compares only the rows (what follows each word). The column evidence
you used in step 1 doesn't appear in it at all, and the widget below has the
same blind spot. On a short text it rarely changes the answer, but two words
could share a row and differ completely in their columns and still come out at
distance 0.

</Callout>

## Interactive widget

Click any row to see its vector, click a second row to get the distance between
the two, and read the <GlossaryTerm id="similarity-matrix">similarity matrix</GlossaryTerm>
underneath as every pair at once. The
text box is shared with the [Training](https://www.llmsunplugged.org/modules/training/) widget, so you can
paste your own text in and compare it with the grid on your desk.

This is the one module where the screen does the heavy lifting; tell the class
why. Seven words means 21 pairs; ten words means 45. Working through all of
them is an afternoon of arithmetic that teaches nothing new after the third
pair. The one distance you did on paper is there so you know what the numbers
mean, and the machine can do the rest.

Notice what the widget turns up in the worked example that most rooms don't
guess: `ran` and `slept` sit at distance 0, because each one only ever appears
before a full stop.

<EmbeddingWidget client:visible />

<Callout type="tip" title="Grid models only">

This module needs rows to compare, so it assumes the [grid](https://www.llmsunplugged.org/modules/training/)
version of the <GlossaryTerm id="bigram-model">bigram model</GlossaryTerm>. With
a cutouts spread you'd first have to gather each word's cutouts into a pile and
count them, which is the grid again, the long way round.

</Callout>

## Instructor notes

### Discussion questions

- did the swap test agree with your written guess? Which of the two would you
  trust on a longer text?
- `ran` and `slept` came out identical, and so, nearly, did `cat` and `dog`.
  What would you have to add to the text to pull each pair apart?
- the distance only compares rows. Which pair in your grid would it get wrong?
- the method measures position in a sentence. Which pairs of _opposites_ would it
  call twins, and why do `hot` and `cold` keep almost the same company?
- if you trained on ten times as much text, would your twins end up closer
  together or further apart?
- your grid gives a word one row and one fingerprint. Is `bank` one word?

### Connection to current LLMs

The linguist J.R. Firth summed up this principle in 1957 as "you shall know a
word by the company it keeps", and it underpins modern embeddings: words used in
the same contexts tend to mean similar things.

- **counts versus learned vectors**: your row is a count vector---one number per
  word in your <GlossaryTerm id="vocabulary">vocabulary</GlossaryTerm>, nearly all of them zero, every number a tally you
  wrote down yourself. A real embedding is _learned_: a few hundred to several
  thousand numbers, none of them a count of anything, nudged during <GlossaryTerm id="training">training</GlossaryTerm>
  until words used alike end up near each other.
- **learning is what makes it generalise** (also explored in
  [Search Sheets](https://www.llmsunplugged.org/modules/search-sheets/)): your grid rates any pair it
  never saw as flatly impossible, because an empty cell is a zero chance. Because a
  learned model spreads what it knows across shared numbers instead of separate
  cells, what it learns about `cat` reaches `dog`, and it can rate a pair it has
  never seen as plausible. Two words can even end up close having
  never shared a single exact context, each being close to words the other kept
  company with.
- **the famous analogy**: king − man + woman ≈ queen, from the word2vec work of
  Mikolov and colleagues in 2013. The vectors come out arranged so that
  arithmetic on them lands somewhere sensible. It works for some analogies and
  not others, and the usual evaluation excludes the three input words from the
  candidate answers, which flatters the result.
- **modern embeddings are contextual**: in an <GlossaryTerm id="llm">LLM</GlossaryTerm> a word's vector is computed
  from the sentence it appears in, so the `bank` of a river and the `bank` that
  holds your money get different vectors.

Source: <https://www.llmsunplugged.org/modules/word-embeddings/>\
Licence: [CC-BY-SA-4.0](https://creativecommons.org/licenses/by-sa/4.0/)\
Cite as: Swift, B. (2026). _LLMs Unplugged: Understand how AI language models work by building one yourself._ https://doi.org/10.5281/zenodo.17403824
