Availit World How AI really works, in your language

Lesson 14 of 4613 minFacts checked October 20, 2026

Embeddings: where meaning becomes geometry

The first thing a model does with each token is look up a vector for it. Nobody designs those vectors; training moves them around until Monday sits next to Tuesday, and Paris minus France plus Japan lands on Tokyo. We measured that in a real model for this lesson.

In this lesson you will

  • Explain the embedding table: one learned vector per token, looked up by its ID
  • Read real nearest neighbours from a model’s embeddings, and say what they reveal
  • Understand that a token’s vector changes with its context as it passes through the layers
  • Use embedding models for search and RAG, and know the output side of a model
On this page
  1. A table with one row per token
  2. What a real table learned
  3. Same word, different meanings
  4. The other end: from vectors back to tokens
  5. Embeddings for search: RAG
  6. Remember
  7. Check yourself

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 size

One row per token.

Llama 3.1 8B: 128,256 × 4,096 = 525 million numbers

About 6.5 % of the whole model, just for this first lookup.

token “ cat” → its ID → that row → a vector of 4,096 numbers

No 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:

Nearest neighbours in SmolLM2-1.7B’s embedding table (measured 20 October 2026)
TokenIts closest tokens (cosine similarity)
␣Monday␣Tuesday 0.80, ␣Wednesday 0.73, ␣Thursday 0.70, ␣Friday 0.68
␣seven␣six 0.71, ␣eight 0.70, ␣nine 0.69, ␣five 0.68
␣doctor␣doctors 0.77, ␣physician 0.60
␣Tokyo␣Japan 0.49, ␣Japanese 0.45, ␣Seoul 0.42
␣Pakistan␣Pakistani 0.66, ␣Bangladesh 0.42, ␣Nepal 0.37
␣king␣King 0.81, King 0.72, ␣kings 0.67, ␣queen 0.56

␣ 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.)

0 of 6 sorted
  • “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.

One token, two journeys
  1. Layer 0“ bank” is the same row of the table in both sentences
  2. Early layersAttention mixes in neighbours: “river” in one sentence, “account” in the other
  3. Later layersThe two vectors now point in clearly different directions, one about water, one about money

Quick check

Why does a model need layers after the embedding table to tell “river bank” from “bank account”?

Show the answer

Answer: The table gives “ bank” the same vector in both, so context must be mixed in later The lookup ignores the neighbours. Attention in the following layers brings in the surrounding words and changes the vector.

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.

Retrieval-augmented generation (RAG)
  1. Prepare onceCut your documents into chunks and store each chunk’s embedding in a vector database
  2. Embed the questionTurn the user’s question into a vector with the same model
  3. Find the nearest chunksThe few chunks with the highest cosine similarity
  4. 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”.

Get an embedding from a model on your own computer (Ollama)bash
$ 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-text is 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.

Check yourself

5 questions. Tap the answer you think is right.

0 of 5 answered

What is in each row of the embedding table?

Show the answer

Answer: A learned vector for one token Each row is the vector for one token, as long as the hidden size, and it is learned during training.

In a real table, which pair had the higher similarity: “good”–“bad” or “cat”–“dog”?

Show the answer

Answer: “good”–“bad” (0.40 against 0.30) Opposites appear in the same places in sentences, so learning from context puts them close together.

Why are the vectors for “bank” in two different sentences the same at the first layer?

Show the answer

Answer: Because the table gives one row per token, whatever the context The lookup only uses the token ID. The layers above mix in context and separate the two meanings.

What does lm_head do?

Show the answer

Answer: Turns the final vector into a score for every token in the vocabulary It is the output matrix: a dot product of the final vector with each token’s row gives that token’s score.

In RAG, how are the right documents found?

Show the answer

Answer: By comparing the question’s embedding with the documents’ embeddings The question and the chunks are embedded with the same model, and the closest chunks by cosine similarity are given to the language model.

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
Previous lesson All lessons

Choose your language