Unit 1 · Foundations
Lesson 03 · Available to read
Embeddings: Turning Words into Meaning-Rich Vectors
How every token becomes a vector whose position encodes meaning, and how similarity is measured in that space.
A token ID like 2065 is just a label — the number itself means nothing, and token 2066 isn't "close" to it in meaning. To compute with meaning, the model needs a representation where similar things are near each other. That representation is an embedding: a vector of hundreds or thousands of numbers for every token.
The embedding table
An embedding layer is simply a big matrix with one row per token in the vocabulary:
- rows = vocabulary size V (e.g. 100,000)
- columns = embedding dimension d (e.g. 4,096)
Looking up a token's embedding means taking the row at its ID. No arithmetic — just indexing.
These numbers start out random. During training they are adjusted by gradient descent (Lesson 4) along with every other parameter, so that the embeddings become whatever is most useful for predicting the next token. Nobody hand-designs what dimension 517 means.
Meaning as geometry
After training, the embedding space has structure. Tokens used in similar contexts end up with similar vectors — the distributional hypothesis: you shall know a word by the company it keeps.
We measure similarity with cosine similarity, the cosine of the angle between two vectors (see the Math refresher on cosine similarity):
cos(a, b) = (a · b) / (‖a‖ × ‖b‖)
- close to 1 → pointing the same way (similar meaning)
- close to 0 → unrelated
- close to −1 → pointing in opposite directions
Worked example with toy 3-dimensional vectors: let cat = (2, 1, 0) and dog = (2, 2, 0).
- dot product = 2·2 + 1·2 + 0·0 = 6
- ‖cat‖ = √5 ≈ 2.236, ‖dog‖ = √8 ≈ 2.828
- cosine = 6 / (2.236 × 2.828) ≈ 0.95 — very similar.
Directions can carry meaning
Famously, word embeddings trained with word2vec (2013) showed that some relationships appear as consistent directions:
vector("king") − vector("man") + vector("woman") ≈ vector("queen")
The offset from man to woman is roughly the same as from king to queen; similarly for France → Paris and Japan → Tokyo. Treat this as an insight rather than a law: the analogies are approximate, and the best-known examples were somewhat cherry-picked. But the core idea — that concepts correspond to directions in a high-dimensional space — turns out to be central to how researchers study LLMs today (Lesson 19).
Why so many dimensions?
With thousands of dimensions, the space can hold many independent "directions of meaning" at once — formality, topic, tense, sentiment, whether something is a person, and far more. High-dimensional spaces are roomy: you can fit a huge number of nearly perpendicular directions in them, which is part of why models can pack so many concepts into a fixed-size vector.
Static vs. contextual embeddings
The embedding table gives each token one vector, no matter where it appears. But "bank" means different things in "river bank" and "bank loan".
That's fine, because the table lookup is only the starting point. As the vector travels up through the transformer's layers, attention (Lessons 5–6) mixes in information from surrounding tokens. By the middle layers, the "bank" vector in a sentence about rivers has moved far from the "bank" vector in a sentence about money. These contextual embeddings are what the model actually reasons with.
Embeddings beyond the model's input
The same idea powers tools you'll meet later:
- Sentence and document embeddings — an embedding model turns a whole passage into one vector, so you can find passages similar to a question. This is the engine of retrieval-augmented generation (Lesson 15).
- Output side — at the top of the network, the model compares its final vector against every token's (output) embedding to produce the next-token scores. Many models share or "tie" the input and output tables.
Size check
The embedding table is big. With V = 100,000 and d = 4,096 it holds 409.6 million numbers — for a 7-billion-parameter model, roughly 6% of all its parameters sit in this one lookup table.
The takeaway
An embedding is a learned vector for each token; training arranges these vectors so that direction and distance encode meaning, and the transformer then refines them into context-aware representations.
Practice
0 of 7 attempted
- 1Recall
What are the rows and columns of an embedding matrix, and what operation retrieves a token's embedding?
Saved in this browser
- 2Calculate
Compute the cosine similarity of a = (1, 0, 1) and b = (1, 1, 0). Are they similar, unrelated or opposite?
Saved in this browser
- 3Calculate
A model has a vocabulary of 50,000 tokens and an embedding dimension of 2,048. How many parameters are in its input embedding table? If each is stored in 2 bytes, how much memory is that?
Saved in this browser
- 4Multiple choice
Before training begins, what do a model's embedding vectors usually contain?
- 5Explain in your own words
Explain the difference between the static embedding from the lookup table and a contextual embedding. Use the word "bat" (animal vs. sports equipment) as your example.
Saved in this browser
- 6Explain in your own words
What is the distributional hypothesis, and why does next-token training make similar words get similar embeddings?
Saved in this browser
- 7Apply
Your support team has 50,000 past tickets. Describe, in 3–4 steps, how you could use embeddings to automatically suggest similar past tickets when a new one arrives.
Saved in this browser