Concepts / Converting a Single Token ID into an Embedding Vector

Converting a Single Token ID into an Embedding Vector

The embedding layer performs a lookup operation, not a calculation on the token ID. Integer token IDs enter the layer, the layer uses each ID to select a row of its weight matrix, and the selected rows leave the layer as embedding vectors.

  • Programming

Why a Model Works on Sequences of IDs

A language model does not read text directly. Text is first split into tokens, and each token is represented by an integer token ID. Those integers are not yet useful for computation on their own, so the model converts each one into an embedding vector, which is a list of numbers it can work with.

Earlier, you saw one token ID turned into one embedding vector. A language model never sees a single token in isolation, though. It receives a sequence of token IDs, and each one needs its own embedding. This article takes the step you already know and applies it to a whole list of IDs at once.

The running example has four input tokens whose IDs are 2, 3, 5, and 1. The stored IDs are placed in a tensor, which is the data container PyTorch uses for numbers. torch.tensor() creates one from a Python list. The variable input_ids holds the integers that the embedding layer will look up.

python

Predicting the Shape of the Output

Before reading the explanation, commit to an answer. You know that each token ID becomes one embedding vector with three values, and that four token IDs are being passed in together. Ask yourself: how many rows will the output have, and how many values will each row contain?

What do you think happens?

Four token IDs with an embedding dimension of 3 are passed to the embedding layer together. What is the shape of the output matrix?

Reveal answer
Answer:

4 rows by 3 columns, written as $4 \times 3$

The number of rows follows the number of token IDs, and the number of columns follows the embedding dimension. Each of the four IDs contributes one row, and each row holds three values.

If your prediction was $4 \times 3$, you reasoned from the structure correctly. If you got a different layout, check which dimension you assumed to be the token count and which you assumed to be the embedding size. The row count comes from the input, and the column count comes from the embedding dimension.

ID 2input position 0weight row 1output row 0ID 3input position 1weight row 2output row 1ID 5input position 2weight row 3output row 2ID 1input position 3weight row 5output row 3selectsselectsselectsselectscopiedcopiedcopiedcopied
If I pass several token IDs at once, how does the output matrix's shape relate to the input, and which output row corresponds to which ID?

The diagram shows the relationship between inputs and outputs. The first output row always corresponds to the first token ID, because the output keeps the same order as the input. The weight matrix has other rows that are not drawn here, since only the rows selected by these four IDs matter for this call.

How Each Row Is Looked Up

The rows of the output are not computed from the token IDs by arithmetic. The embedding layer holds a weight matrix, and each token ID acts as a position to fetch from it. The fetched row is the embedding for that token. The output matrix is simply these fetched rows, placed in the same order as the input IDs.

Definition

A token ID is an integer that names a token. The embedding layer uses it as a key to retrieve one row of its weight matrix. The embedding weight matrix is the stored table of rows from which those vectors are copied.

The lookup is zero-based. Row numbering starts at index 0, so the first row of the weight matrix is row 0. A token ID of 3 therefore selects the row at index 3, which is the fourth row, not the third. Keeping this in mind prevents the most common off-by-one confusion when reading an embedding matrix.

Key point

The embedding layer performs a lookup operation, not a calculation on the token ID. Integer token IDs enter the layer, the layer uses each ID to select a row of its weight matrix, and the selected rows leave the layer as embedding vectors.

Retrieving the Vector for Token ID 3

Next, apply the embedding layer to a single token ID. The call passes a tensor that holds only the ID 3. Before running it, notice what is being asked: one token ID goes in, and the layer must return the vector that belongs to that token. The layer is named embedding_layer in this example, and calling it like a function is how it is used.

python
Notes on the code
  • single_id holds the one token ID 3
  • embedding_layer was created earlier in the example with an embedding dimension of 3
  • calling the layer like a function performs the lookup

The returned result is displayed inside double square brackets. The outer pair means the value is a two-dimensional tensor with a single row, and the three numbers inside that row are the components of the embedding vector for token ID 3. Each embedding vector in this example has three components.

The display also includes a label named grad_fn, with the value EmbeddingBackward0. The source does not explain that label in this section, so treat it as metadata that PyTorch attaches to the result. The important part is the three numbers, which are the values stored in the matching row of the weight matrix.

token ID 3row index 3fourth row, counted fromzeroembedding vectorthree values from that rowused as indexcopied out
How does a token ID map to its embedding vector, and what is the relationship between the ID, the row, and the returned vector?
Worked example

Finding the row for token ID 3

A token ID of 3 is passed to the embedding layer. Which row of the weight matrix is returned, and how many values does it contain?

  1. 1
    Read the token ID

    The input is the single integer 3.

  2. 2
    Apply zero-based indexing

    Rows are counted from index 0, so index 3 is the fourth row of the weight matrix.

  3. 3
    Copy the row

    The layer returns the stored row as the embedding vector, without computing anything new from the ID.

  4. 4
    Check the shape

    The result is a two-dimensional tensor with one row and three values, because the embedding dimension is 3.

Result

The embedding vector for token ID 3 is identical to the fourth row of the embedding matrix, which is the row at index 3.

Where to Inspect a Mismatched Lookup

When a lookup result looks wrong, you do not need to guess. Trace three quantities in order: the input token ID, the zero-based row index that the ID selects, and the returned vector. Comparing these three shows the step where your expectation and the actual result diverge.

token IDthe integer passed inzero-based row indexcompare with the expectedrowweight matrix rowthe stored row at thatindexreturned vectorcompare with the expectedvaluesselect rowfetchreturn
What is the sequence of steps from token ID to returned vector, and at which step can I compare the expected and actual values?

The check is most useful at the decision point between the ID and the row. If the ID is 3 but you expected the second row, the mismatch comes from counting rows from one instead of zero. If the row index is right but the vector differs from what you expected, the comparison should be made against the matching row of the embedding matrix itself.

Mistakes Beginners Make with IDs and Vectors

  • Common mistake

    Treating the token ID and the embedding vector as two versions of the same thing

    What it looks like

    Assuming the ID 3 is already a list of numbers that describes the token.

    Why it fails

    The token ID is a single integer that names a token. The embedding vector is a list of numbers that the layer reads from a stored row.

    Do this instead

    Think of the ID as a key, and the embedding vector as the value stored under that key.

  • 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

    Python starts with a zero index, so token ID 3 corresponds to the row at index 3, which is the fourth row.

    Do this instead

    Always count rows from index 0 when matching an ID to its row.

  • Common mistake

    Expecting arithmetic to produce the vector

    What it looks like

    Looking for a formula that turns the ID 3 into its three numbers.

    Why it fails

    The embedding layer performs a lookup, not a calculation on the token ID. It reads a stored row.

    Do this instead

    Compare the returned vector to the matching row of the weight matrix rather than searching for a computation.

  • Common mistake

    Assuming the output row order does not matter

    What it looks like

    Reordering the output rows after the call because they look unsorted.

    Why it fails

    Output rows keep the same order as the input IDs, so the first output row always corresponds to the first token ID.

    Do this instead

    Read output rows by input position, not by sorting them.

Why Position Must Be Added Next

The matrix built by the lookup tells the model what each token is, but nothing in it says where that token sits in the text. The same token ID can appear at the start or at the end of a sentence, and the lookup returns the same embedding either way.

For this reason, the embedding matrix holds token identity but not position. The next step adds positional information to these embedding vectors, so that the model can distinguish the same token appearing in different places in a sequence.

Checking Your Understanding with a New ID

EASY

Suppose the embedding layer receives the token IDs 4, 0, and 2 together. Predict the shape of the output, and state which output row corresponds to the token ID 0.

Hints
  • The number of rows follows the number of token IDs passed in.
  • The number of columns follows the embedding dimension, which is 3 in this example.
  • Output rows keep the same order as the input IDs, so the second input becomes the second output row.
  • Count the weight matrix row for ID 0 from index zero.

Key Takeaways

  • An embedding layer given several token IDs returns one row per ID, so the output has as many rows as there are input IDs and as many columns as the embedding dimension.
  • Each output row is a lookup: a token ID selects a row of the embedding weight matrix by zero-based index, and that stored row is copied into the output in input order.
  • The embedding layer performs retrieval rather than arithmetic on the token ID, so a mismatched result can be traced by comparing the token ID, the row index, and the returned vector.
  • The embedding matrix encodes token identity but not position, so positional information is added to these vectors as the next step.

Related concepts

More concepts starting with C