A word as a list of numbers
A word embedding is a list of numbers that stands for a word, set so that words with similar meanings sit close together. Language models are built on embeddings, and knowing how they work also shows where those models go wrong.
This lesson uses tiny invented vectors you can follow by hand. Real ones have hundreds of numbers.
By the end of this lesson
Note
I can explain how words become vectors and where models go wrong.
Key words
| Word | What it means |
|---|---|
| Embedding | a vector of numbers that stands for a word |
| Context | the words around a word |
| Attention | weights that say which words matter for the word being read |
| Bias | a pattern from the training text that treats groups unfairly |
| Made-up answer | fluent text that is not true, also called a hallucination |
The cells students fill in are left blank in this preview.
Meaning becomes nearness
A bag of words sees "essay" and "revision" as two unrelated words. Yet they are used in the same places: near "deadline", "library" and "lecture".
An embedding gives each word a vector, and training moves words used in the same context close together. Nearness is measured with the cosine you already know.
Worked example: invented word vectors
| Word | Work | Study | Food | Cosine with lunch |
|---|---|---|---|---|
| lunch | 1 | 1 | 9 | 1.0 |
| sandwich | 0 | 1 | 8 | 0.994 |
| coffee | 2 | 0 | 7 | 0.98 |
| essay | 3 | 9 | 0 | 0.139 |
| report | 9 | 3 | 0 | 0.139 |
The cells students fill in are left blank in this preview.
Cosine of lunch and sandwich
docker exec jupyter python3 -c "import math; print(round(73/(math.sqrt(83)*math.sqrt(65)),3))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Cosine of lunch and report
docker exec jupyter python3 -c "import math; print(round(12/(math.sqrt(83)*math.sqrt(90)),3))"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Find which word is nearest
import math
words = {"lunch": [1, 1, 9], "sandwich": [0, 1, 8], "report": [9, 3, 0]}
def cosine(a, b):
top = 0
for n in range(len(a)):
top = top + a[n] * b[n]
return top / (math.sqrt(sum(x * x for x in a)) * math.sqrt(sum(x * x for x in b)))
for other in ["sandwich", "report"]:
print("lunch and", other, round(cosine(words["lunch"], words[other]), 3))
# Add "coffee": [2, 0, 7] to the dictionary and to the loop.
A common mistake
Warning
Many people think each number in an embedding means something, like "food". The labels in the table are only there to help you. In a real embedding the numbers have no names. Only the distances between words carry meaning.
Attention picks the words that matter
One word can mean different things. "Bank" is a river edge in one sentence and a place for money in another.
Attention gives each word in the context a weight for the word being read. The model blends the vectors using those weights, so "bank" near "river" ends up near the river words.
Worked example: invented attention weights for bank
| Sentence | Biggest weight | Smaller weights |
|---|---|---|
| the bank by the river | river 0.60 | bank 0.20, by 0.10, the 0.05 twice |
| the bank closed my account | account 0.40 | bank 0.25, closed 0.20, my 0.10, the 0.05 |
The cells students fill in are left blank in this preview.
Models repeat patterns from their training text
A model learns from text that people wrote. If that text pairs one job with one group of people, the vectors for them end up close, and the model repeats the pairing.
This is bias. You find it by testing: swap the group in a sentence and see whether the answer changes. Ask who the training text left out.
Fluent text is not checked text
A language model picks likely words. It has no step that checks the answer against the world. So it can write a made-up answer: a clear, confident sentence that is false, such as a reference to a report that does not exist.
Open the source yourself, redo any numbers, and keep private data out of tools your organisation has not approved.
Going further
Add four words of your own to the vector table, with three numbers each, and use the function to see which are near. Then check where your own choices of numbers were a judgement.
Write three checks you would run before using a model's answer in a report for work.