Concepts / Embedding Layers and Weight Matrices

Embedding Layers and Weight Matrices

A token embedding is retrieved by looking up the row of the embedding layer's weight matrix that matches the token ID, with rows counted from zero.

  • Programming

A Repeated Word Puzzle

Imagine a short input that contains the same word twice, once near the beginning and once near the end. As a human reader, you see two copies of the same word and know they are the same word. The question for this article is whether a large language model treats the two copies the same way. The answer depends on what the first stage of the model, the embedding layer, does with each token.

Before reading on, make a guess. When the embedding layer receives two copies of one word, does it return the same vector both times, or two different vectors? Keep your guess in mind, because the rest of this article tests it.

Tracing a Token ID Into a Row

An embedding layer holds a weight matrix. Each row of that matrix is a vector, and each token ID selects exactly one row. When the layer receives a token ID, it does not perform any complicated computation. It retrieves the vector stored in the row that corresponds to that ID. A useful way to picture this is a table in which the token ID is the address of a row. The output of the layer is simply the row that was retrieved.

Token ID 5Embedding weightmatrixrows counted from zeroRow 5 (sixth row)-2.8400 -0.7849 -1.4096Output vector-2.8400 -0.7849 -1.4096look upselect rowreturned as output
Given token ID 5, which row of the embedding weight matrix is read out, and how does that row become the token's vector?

The lookup is the whole job of the layer. In the illustrated matrix, rows are numbered from the top, starting at zero. Token ID 5 retrieves the sixth row, whose entries are -2.8400, -0.7849, and -1.4096. The same procedure applies to every other ID: each one fetches exactly the row that carries its number, and nothing else in the sequence influences that retrieval.

Worked example

Looking Up Token ID 5

An embedding matrix has rows numbered from zero. The input token has ID 5. Which row is retrieved, and what vector does the embedding layer return?

  1. 1
    Start counting at zero

    Row numbering begins at zero, so row 0 is the first row from the top.

  2. 2
    Locate the matching row

    The token ID is 5, so the layer selects the row whose index is 5. That is the sixth row, not the fifth.

  3. 3
    Read the stored entries

    The entries of that row are -2.8400, -0.7849, and -1.4096.

  4. 4
    Return the row as the output

    The embedding layer outputs this row unchanged as the vector for token ID 5.

Result

Token ID 5 is represented by the vector -2.8400 -0.7849 -1.4096, taken from the sixth row of the matrix.

Why Repeated Tokens Match

Because the lookup depends only on the token ID, the layer produces the same vector for a given ID wherever that ID appears. Token ID 5 yields the same embedding vector whether it sits in the first position or the fourth. The embedding layer has no input for position, so position cannot influence its output. This is the key to predicting what happens with repeated words.

fox at position 1Token ID 2same ID for bothEmbedding vector1.2753 -0.2010 -0.1606fox at position 4looks uplooks upreturns
When the same word appears at positions one and four, do both occurrences look up the same row, and why does that make their embeddings match?

Consider a sentence in which fox appears at positions one and four. Both occurrences carry token ID 2, so both lookups fetch the same row and both come back as the vector 1.2753 -0.2010 -0.1606. The values depend on the ID alone. The position of each token leaves no trace in the numbers the lookup returns.

The same reasoning holds for any token ID at any position. If the sentence were reordered so that the two fox tokens sit next to each other, the embedding layer would again return the same vectors, arranged differently. The layer produces no distinct signal for the change in order.

What do you think happens?

Two occurrences of the word fox sit at positions one and four of a sentence. What does the embedding layer return for each occurrence?

  • Two identical vectors, because both carry the same token ID

  • Two different vectors, because the positions differ

Reveal answer
Answer:

Two identical vectors, because both carry token ID 2 and the lookup depends only on the ID.

The embedding layer selects a row by token ID alone and has no input for position. Both occurrences of fox therefore return the same row, 1.2753 -0.2010 -0.1606.

  • Common mistake

    Assuming that repeated words get different embeddings because they appear in different places in the sentence

    What it looks like

    Believing that the first fox and the fourth fox in a sentence have different vectors.

    Why it fails

    The embedding layer's lookup uses only the token ID. Position is not an input to this stage.

    Do this instead

    Expect identical embedding vectors for identical token IDs, and look for a separate position signal if the distinction matters.

  • Common mistake

    Reading row 5 as the fifth row when counting by hand

    What it looks like

    Treating token ID 5 as the row that appears fifth from the top of the matrix.

    Why it fails

    Row counting starts at zero, so token ID 5 is the sixth row.

    Do this instead

    Count rows from zero, and check the ID against the row index before reading the entries.

  • Common mistake

    Treating the embedding layer as a computation rather than a lookup, which leads people to expect the output to change with context at this stage

Self-Attention Without Position

The self-attention mechanism relates tokens to one another inside an LLM. On its own, however, it has no notion of where a token falls in a sequence or what order the tokens appear in. Self-attention receives the vectors produced by the embedding layer. When identical IDs produce identical vectors, self-attention receives no built-in signal that separates the first occurrence of a word from the second.

dogmanbitesbitesmandog
If the same token vectors are fed into self-attention in a different order, what stays the same and what changes in the output?

The two columns above contain the same token vectors. Each word has the same embedding in both orders, because the lookup depends only on the token ID. Only the arrangement differs. Without a position signal, the mechanism receives identical material in both cases.

How Word Order Gets Lost

Consider the two sentences dog bites man and man bites dog. They use the same words, and each word has the same embedding vector in both sentences. If an LLM read only token embeddings, the two sentences would arrive at self-attention as the same set of vectors. Everything that distinguishes who bit whom would have to come from somewhere other than the token vectors.

This is why the model needs an additional source of position information. Position is the only remaining signal that can tell the first occurrence of a repeated token from the second, and that tell the model which word came first in each sentence. Once position information is added, the same vectors can be read in a meaningful order.

In the real world

A reader who sees the phrase the dog chased the cat knows which animal does the chasing only because of word order. An LLM working from token embeddings alone would face the same ambiguity for any sentence whose meaning depends on arrangement.

Practice Reasoning From the Lookup Rule

EASY

A sentence contains the token ID 7 at positions two and six. Without any position information, what can you say about the two embedding vectors produced by the embedding layer, and why? Explain your answer using the lookup rule.

Hints
  • Ask what input the embedding layer uses to choose a row.
  • Consider whether position is one of those inputs.
  • If both occurrences select the same row, what must be true of the vectors they return?
MEDIUM

Explain, in your own words, why adding a position signal is needed before an LLM can tell apart the two occurrences of the same word, and what would happen to word order if the signal were removed.

Hints
  • Think about what self-attention receives from the embedding layer.
  • Compare what the model sees for dog bites man and man bites dog.

Key Takeaways

  1. 1
    An embedding layer holds a weight matrix in which each row is a vector, and each token ID selects exactly one row.
  2. 2
    Rows are counted from zero, so token ID 5 is the sixth row of the matrix.
  3. 3
    Identical token IDs produce identical embedding vectors, regardless of where they appear in the sequence.
  4. 4
    Self-attention has no built-in notion of position or order, so token embeddings alone do not encode word order and an additional position signal is required.

At a glance

  • An embedding layer turns a token ID into a vector by retrieving the row of its weight matrix that matches the ID, with rows counted from zero.
  • Identical token IDs always produce identical embedding vectors, so repeated tokens look the same at the embedding stage regardless of position.
  • Self-attention has no built-in sense of position or order, so token embeddings alone cannot tell apart dog bites man from man bites dog.
  • An additional position signal must be supplied so that the model can distinguish repeated tokens by where they sit.

Related concepts

More concepts starting with E