A table with one row per token
After the tokenizer, a model has a list of token IDs. Its very first layer is an embedding table: a matrix with one row for every token in the vocabulary. Each row is a vector as long as the model’s hidden size. The model replaces each token ID with its row. That vector is the token’s embedding.
embedding table: vocabulary size × hidden sizeOne row per token.
Llama 3.1 8B: 128,256 × 4,096 = 525 million numbersAbout 6.5 % of the whole model, just for this first lookup.
token “ cat” → its ID → that row → a vector of 4,096 numbersNo multiplication needed: the model just reads one row.
Before training, the rows are random numbers. During training, backpropagation adjusts each row whenever its token appears, just like any other weight. Tokens used in similar ways get pushed in similar directions, because that helps predict what comes next. Meaning ends up as geometry.
What a real table learned
To show you real neighbours instead of a cartoon, we downloaded the embedding table of SmolLM2-1.7B, an open model from Hugging Face (49,152 tokens × 2,048 numbers), and measured the cosine similarity between rows. These are the closest tokens to a few words, leaving out spelling variants such as “Monday” without a space:
| Token | Its closest tokens (cosine similarity) |
|---|---|
| ␣Monday | ␣Tuesday 0. |
| ␣seven | ␣six 0. |
| ␣doctor | ␣doctors 0. |
| ␣Tokyo | ␣Japan 0. |
| ␣Pakistan | ␣Pakistani 0. |
| ␣king | ␣King 0. |
␣ marks the space at the start of a token. Similarity runs from −1 to 1; unrelated tokens score near 0.
Look at what the model worked out by itself, only from predicting the next token: days of the week cluster together, so do numbers, so do neighbouring countries. Capital-letter versions are its closest neighbours, because “King” and “king” are used almost the same way. Different forms of a word stay close: “run” and “ran” score 0.64.
Close or unrelated in this real table? (Close means a cosine above 0.2.)
-
“Monday” and “Tuesday”
Answer: Close
-
“apple” and “seven”
Answer: Unrelated
-
“good” and “bad”
Answer: Close
-
“doctor” and “Tokyo”
Answer: Unrelated
-
“apple” and “banana”
Answer: Close
-
“piano” and “tax”
Answer: Unrelated
Same word, different meanings
One problem: the embedding table gives each token exactly one vector. “ bank” gets the same row in “the river bank” and “the bank account”. In our table, “ bank” is barely related to either “ river” (0.11) or “ money” (0.09): the first layer simply cannot know which bank you mean.
That is fixed by the layers above. As the vectors pass up through the model, attention mixes information between tokens, and each layer adds its findings to the residual stream. A few layers in, the vector for “ bank” in “river bank” has been pulled towards water and land, and in “bank account” towards money. These are called contextual embeddings, and they were the big idea of 2018’s BERT and ELMo models.
- Layer 0“ bank” is the same row of the table in both sentences
- Early layersAttention mixes in neighbours: “river” in one sentence, “account” in the other
- Later layersThe two vectors now point in clearly different directions, one about water, one about money
Quick check
The other end: from vectors back to tokens
At the top of the model, the last vector of the last token must become a score for every possible next token. That is done by another matrix of the same shape, vocabulary × hidden size, often called the output embedding, the unembedding or, in the weight files, lm_head. Each token’s score is the dot product of the final vector with that token’s row. Softmax turns the scores into chances.
Some models use the same matrix at both ends, called tied embeddings. That saves a lot of parameters in small models: SmolLM2-1.7B and Llama 3.2 1B tie theirs, while Llama 3.1 8B keeps two separate matrices, about 1.05 billion numbers in total.
Embeddings for search: RAG
Separate embedding models turn a whole sentence, paragraph or document into a single vector, trained so that texts with similar meaning land close together, even if they share no words. Multilingual ones are trained so that a sentence and its translation land close together too. This powers semantic search and one of the most useful patterns in AI.
- Prepare onceCut your documents into chunks and store each chunk’s embedding in a vector database
- Embed the questionTurn the user’s question into a vector with the same model
- Find the nearest chunksThe few chunks with the highest cosine similarity
- AnswerPaste those chunks into the prompt, and ask the language model to answer from them
RAG gives a model knowledge it was never trained on, such as your own notes, today’s news or a company’s documents, without changing a single weight. The 2020 paper that named it called the model’s weights its “parametric memory” and the document index its “non-parametric memory”.
$ ollama pull nomic-embed-text
$ curl http://localhost:11434/api/embed -d '{"model": "nomic-embed-text", "input": "Where can I renew my passport?"}'
nomic-embed-textis a small open embedding model.- The answer is a JSON list of numbers: the sentence’s embedding. Embed your documents the same way, and compare with cosine similarity.
- llama.cpp’s server offers the same service at
/v1/embeddings, in the OpenAI format.
Remember
- The embedding table has one learned vector per token (vocabulary × hidden size). The model starts by looking up each token’s row.
- Training places similar tokens close together. In a real table, Monday sits beside Tuesday, and Paris − France + Japan lands on Tokyo.
- Opposites like good and bad are close, because they appear in the same places. Unrelated words score near 0.
- The lookup ignores context; the layers above turn it into contextual embeddings. At the top, the unembedding (
lm_head) turns the final vector into scores for every token. - Embedding models turn whole texts into vectors for semantic search and RAG.
Words to know
- Embedding
- A learned vector that stands for a token, or for a whole text.
- Embedding table
- The matrix with one row per token, used at the start of a model.
- Contextual embedding
- A token’s vector after the layers have mixed in its surroundings, so the same word can mean different things.
- Unembedding (lm_head)
- The matrix at the end that turns the final vector into one score per token.
- Tied embeddings
- Using the same matrix at the start and the end of a model, to save parameters.
- Embedding model
- A model that turns a whole text into one vector for search and comparison.
- Vector database
- A store of embeddings that can quickly find the ones closest to a query.
- RAG
- Retrieval-augmented generation: find relevant documents by embedding, then give them to the model with the question.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Efficient Estimation of Word Representations in Vector Space (word2vec)
The first embeddings to show arithmetic with meaning. Compare its examples with the ones measured here.
-
BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
Made contextual embeddings standard. The first paper to read about encoder-style transformers.
-
Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
The paper that named RAG, and showed how much retrieval helps a model answer questions that need knowledge.
-
Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
How to train a model whose sentence vectors can be compared with cosine similarity.
-
SmolLM2: When Smol Goes Big
The open model whose embedding table we measured. A good, readable report on training a small model well.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonAttention: how tokens look at each other