Concepts / Backpropagation in Neural Network Layers

Backpropagation in Neural Network Layers

The embedding layer is a lookup operation: a token ID selects a row of the weight matrix by position.

  • Programming
Interactive lab

Try it: Text Vectorization: Tokens to Vectors

The text-to-tensor pipeline: standardize, split into tokens, build a frequency-ranked vocabulary with padding and [UNK], map tokens to integer ids, pad or truncate, then one-hot or multi-hot encode and look the ids up in an embedding table.

How it works

  1. Standardize: lowercase and delete punctuation.
  2. Split on whitespace into tokens.
  3. Build the vocabulary: 0 = padding, 1 = [UNK], then words by frequency (ties: first appearance), capped at max tokens.
  4. Map each token to its id (1 if it did not make the vocabulary), then pad with 0 or truncate to the sequence length.
  5. Encode: one-hot rows per position or one multi-hot row per text; embed: id k selects row k of the embedding table.

Default run (18 steps): Raw text (59 characters): "The movie was great. The plot was great, the acting wasn't!". A model cannot read strings, so it is turned into numbers step by step. … Embedding lookup: id k selects row k of a fixed 8 x 3 table, giving a (8, 3) dense matrix instead of (8, 8) sparse one-hots. Row 0 (padding) is looked up too; a trained Embedding layer would learn these rows.

Simplified: Bounded ASCII text (up to 200 characters, 40 tokens), whitespace splitting only, and a small FIXED embedding table (a real Embedding layer learns its rows). The multi-hot vector keeps the padding slot (always 0) so its positions line up with the ids.

Educational simulation

Loading the simulation…

Why a Token ID Is Not Yet Useful

Before any model learns anything, each token in a text has already been reduced to an integer. That integer, the token ID, is only a label. It tells the model which entry of its vocabulary the token corresponds to, but it carries no information about meaning. An embedding layer turns that bare label into a vector of numbers that the model can work with.

The question this article answers is how the layer performs that conversion. The short answer is that it does not compute anything complicated. It looks up a row. The rest of the article explains what that means, why the lookup gives exactly the vector you expect, and how it connects to a different way of writing the same operation.

What do you think happens?

An embedding layer receives token ID 3. What does it return?

  • A newly calculated vector that approximates the stored values

  • A copy of the row at position 3 in the weight matrix

  • The integer 3 converted to a decimal vector

Reveal answer
Answer:

A copy of the row at position 3 in the weight matrix

The returned vector is not a fresh calculation. It is identical to a row that already exists in the embedding matrix, and token ID 3 retrieves the fourth row because IDs are zero-based row indices.

Tracing a Token ID Into the Weight Matrix

The embedding layer holds a weight matrix, and each row of that matrix stores the vector for one token. The token ID is used as a position. The layer moves to that position in the weight matrix and reads the row found there. Rows at other positions are not touched for this lookup.

Token ID 3incoming labelRow 0[0.2, -0.5]Row 1[0.7, 0.1]Row 2[-0.3, 0.9]Row 3[0.4, 0.6]Row 4[-0.8, 0.2]selects position 3
When token ID 3 arrives, which row of the weight matrix is selected, and how does the ID's position map to that row?

The diagram shows the same idea in a different form. The five rows are numbered from zero, so the row with position 3 is the fourth row. The values inside the rows are illustrative numbers chosen for this lesson; the point is that only the highlighted row is read.

Zero-Based Indexing and Token ID 3

Token IDs are zero-based row indices. The first row has index 0, so token ID 0 retrieves the first row, and token ID 3 retrieves the fourth row. Counting from one is the most common source of confusion when checking an embedding by hand.

Why the Lookup Matches One-Hot Multiplication

Readers who already know one-hot encoding may recognize the lookup as a shortcut. One-hot encoding represents a token ID as a vector whose length equals the vocabulary size, containing a single 1 at the position of the token and 0 everywhere else. That vector is then multiplied by a weight matrix in a fully connected layer.

Take a vocabulary of five tokens. The one-hot vector for token ID 3 has a 1 in position 3, counting from zero, and zeros in the other four positions. Multiplying it by the weight matrix produces the row at index 3, because every other term in each sum is multiplied by zero. The result is the same row the embedding layer would have returned directly.

Formula

In this formula, x is the one-hot vector for token ID 3, W is the weight matrix, and W_{3,:} is its row at index 3. The one-hot multiplication and the direct lookup therefore give identical vectors.

Token ID 3Token ID 3One-hot vector0, 0, 0, 1, 0Read row at index 3no multiplicationMultiply by weightmatrixevery term summedRow 3 vector[0.4, 0.6]Row 3 vector[0.4, 0.6]encodemultiplyresultuse as positioncopy row
How does multiplying a one-hot vector by the embedding matrix produce the same vector as picking one row directly, and what work is skipped in the lookup form?

Checking a Returned Vector Against the Matrix

Verification is simple once the indexing is clear. You take the token ID, find the row at that zero-based position in the weight matrix, and compare the stored numbers with what the layer returned. The returned vector is not approximated, so the match should be exact.

Worked example

Verifying the vector for token ID 3

A five-token vocabulary has an embedding matrix with rows 0 to 4, shown in the diagram above. The layer returns [0.4, 0.6] for token ID 3. Does the returned vector match the matrix, and if not, which row should it have matched?

  1. 1
    Identify the zero-based index

    Token ID 3 is a zero-based row index, so it names the row at position 3.

  2. 2
    Count the rows from zero

    Row 0 is [0.2, -0.5], row 1 is [0.7, 0.1], row 2 is [-0.3, 0.9], and row 3 is [0.4, 0.6].

  3. 3
    Compare the stored row

    The stored row at position 3 is [0.4, 0.6], which exactly equals the returned vector.

  4. 4
    Interpret a mismatch

    If the returned vector instead equals [-0.3, 0.9], the lookup used position 2, which is the third row. The mismatch points to the index used for the lookup, not to the matrix values.

Result

The returned vector [0.4, 0.6] matches row 3 exactly. A vector equal to row 2 would indicate that the lookup used the wrong index.

Locating Where an Output Diverges

When an embedding result looks wrong, follow the path from token ID to returned vector and check each step. The important consequence from the source is that the returned vector is identical to the stored row, so a mismatch points to the index used for the lookup rather than to some hidden calculation.

Token ID receivedfor example 3Index within matrixrowsrows 0 to 4Index used for lookupzero-based positionStored row at thatindexcopied, not computedReturned vectorshould equal that rowcheckyesread rowreturn
Along the path from token ID to returned vector, at which step does the output stop matching the expected matrix row?

The divergence almost always occurs at the index step. If the vector returned for token ID 3 matches the third row instead of the fourth, the index used for the lookup was off by one. The matrix values and the returned copy are not the place to look first. Check which position the lookup actually used, then count rows from zero to confirm the expected row.

Mistakes When Checking an Embedding

  • Common mistake

    Counting rows from one instead of zero

    What it looks like

    Expecting token ID 3 to return the third row of the matrix.

    Why it fails

    Token IDs are zero-based row indices, so token ID 3 retrieves the fourth row.

    Do this instead

    Label the rows 0, 1, 2, 3, 4 before comparing, and take the row labeled 3.

  • Common mistake

    Expecting the returned vector to differ from the stored row

    What it looks like

    Looking for a computed value that is close to but not equal to the matrix row.

    Why it fails

    The returned vector is not a fresh calculation. It is identical to a row that already exists in the matrix.

    Do this instead

    Compare the returned numbers to the stored row directly. An exact match is the expected result.

  • Common mistake

    Searching for a formula in the lookup output

    What it looks like

    Trying to reverse-engineer the embedding from the printed tensor as if it came from arithmetic.

    Why it fails

    The output is the evidence of the lookup, not the product of a formula. The explanation is what you check the output against.

    Do this instead

    Read the printed row, identify the index that produced it, and verify that index against the matrix.

  • Common mistake

    Assuming the lookup result must come from a different row because the values look unfamiliar

Practice: Verify a Lookup

EASY

Using the five-row matrix from the worked example, where row 0 is [0.2, -0.5], row 1 is [0.7, 0.1], row 2 is [-0.3, 0.9], row 3 is [0.4, 0.6], and row 4 is [-0.8, 0.2], an embedding layer returns [-0.8, 0.2] for a token ID. Which token ID was used, and what does that tell you about the lookup?

Hints
  • Find the row whose values match the returned vector exactly, then note its zero-based index.
  • Row 4 is the fifth row, so the index is 4 rather than 5.
  • A correct lookup returns the row at the index it was given, so the returned row identifies the index that was actually used.

Key Takeaways

  • An embedding layer is a lookup: a token ID is used as a zero-based position, and the layer returns the row of its weight matrix at that position.
  • The returned vector is an exact copy of the stored row, so verifying an embedding means comparing it to the row at the correct zero-based index.
  • Token ID 3 retrieves the fourth row, so counting from zero is essential when checking results.
  • Multiplying a one-hot vector by the weight matrix produces the same row, and the direct lookup is the more efficient implementation of that operation.
  • When an output does not match, the divergence is at the index step, which points to the index used for the lookup.

Related concepts

More concepts starting with B