Skip to main content

Exercises — Embedding space

Self-check questions, exercises, and worked answers

← Back to the lesson: Embedding space

Check your understanding

Hands-on exercises are grouped in the Text data collection, under Practice. This page belongs to that section and keeps the self-check questions below; it is not part of the main learning path.

  1. An embedding matrix has \(V\) rows and vectors of dimension \(d\). How many parameters does it contain, and what does ID \(i\) select?
Show the reasoning

The matrix contains \(V\times d\) parameters. ID \(i\) selects row \(E_{i,:}\); its integer value is an index, not a semantic coordinate. Positional processing and contextualization happen afterwards, depending on the architecture (see the two dedicated lessons).

  1. A 2D projection shows two clearly separated point clusters. Does this prove that the model correctly distinguishes these categories on new data?
Show the reasoning

No. A 2D projection preserves only selected aspects of the geometry and may create or exaggerate visual separation. Define a task, metric, and evaluation protocol on relevant held-out data; an exploratory plot is not evidence of predictive performance.

You have understood this step if you can explain that an ID selects a table row, distinguish the row index from the stored vector, and separate the initial vector from contextualized states.

  1. Compare two architectures with the same tokenizer and token embedding table. In A, a learned positional embedding is added to token lookups before the first block. In B, RoPE is applied to queries and keys inside each attention layer. For the same sequence of token IDs, what can you say about the lookup vectors, where positional information enters, and the contextualized states? Why would it be wrong to say that both architectures “add a positional vector to the lookup”?
Show the reasoning

Both architectures share the token embedding table. They therefore read the same row for a given ID at any position. In A, the learned positional vector is added to the embeddings before the blocks. In B, RoPE transforms query and key components in the attention computation; it does not add a positional vector to the lookup result. In both architectures, Transformer layers then produce contextualized states that depend on the other accessible positions and on the positional mechanism. Keep the initial token vector, the architecture-specific positional mechanism, and the contextualized state distinct.

Success criterion: describe the two locations where positional information enters without generalizing A’s mechanism to every model, and distinguish the initial lookup from a contextualized state.

Recall cards

Try to answer before opening each card. These cards rehearse the key distinctions; the exercises and experiment below ask you to apply them.

Card 1 — What does ID \(i\) identify?

Row \(i\) in matrix \(E\), not a coordinate or a measure of proximity.

Card 2 — What is the shape of matrix \(E\)?

\(V \times d\): one row per vocabulary ID and \(d\) components per vector.

Card 3 — Do two occurrences of the same ID have the same initial vector?

Yes, for a fixed matrix: both retrieve the same row before positional processing and contextualization.

Card 4 — Where does contextualization happen?

In later network computations, including Transformer blocks, not during the initial lookup.

Card 5 — What does a cosine neighbour establish?

Proximity under a specified model, vectors, normalization, and metric; not synonymy or semantic equivalence.

Inspect a tiny input embedding table

This model-independent exercise uses a deliberately tiny NumPy matrix. It needs no tokenizer, checkpoint download or GPU. The helper functions operate on input embedding rows, not contextualized hidden states.

import numpy as np
from llm_lab.foundations.embedding_inspection import (
    cosine_top_k,
    lookup_embedding_rows,
)

embedding_matrix = np.array(
    [[1.0, 0.0], [0.8, 0.6], [-1.0, 0.0], [0.0, 1.0], [0.0, 0.0]]
)

# IDs, not decoded strings, select and identify the rows.
print(lookup_embedding_rows(embedding_matrix, [1, 0, 1]))
neighbor_ids, similarities = cosine_top_k(embedding_matrix, token_id=0, k=3)
print(list(zip(neighbor_ids.tolist(), similarities.tolist(), strict=True)))

The lookup preserves order and repeated IDs. The cosine search excludes the query ID by default, excludes zero-norm candidate rows (cosine is undefined for them), and breaks ties by ascending ID. Its output is a directed top-k neighbourhood; it is not automatically a symmetric graph.

Try it: change the query ID, compare k=1 and k=3, and predict the order before running the code. Then explain why the result says nothing by itself about contextual meaning or sentence-level retrieval quality.

You have understood this exercise if you can trace every returned vector back to its integer row ID and state the exact candidate, self-neighbour and tie policies.

Guided exploration — Embedding Atlas

Open Embedding Atlas · Documentation.

Before the experiment, choose a documented dataset and record model/version, preprocessing, metric, and provenance. Predict which metadata field will be associated with the visible neighbourhoods, and state an observation that would change your mind.

  1. Load vectors and metadata; locate the projection, nearest-neighbour search, and automatic clusters/labels.
  2. Filter one category. Describe which points disappear, which groups change, and any outliers.
  3. Open several examples and compare their original content with the displayed neighbours. If possible, also compute neighbours in the original vector space.
  4. Change one projection or clustering setting and record what changes.

Debrief: proximity in the projection does not guarantee equivalent proximity in the original space; an automatic cluster does not prove a natural semantic category. Distances and shapes can be distorted, and groups depend on the representation, metric, and settings. Metadata helps test hypotheses and identify confounders, but does not prove causality.

Success criterion: distinguish original geometry, projection, clustering, and metadata, and make the exploration reproducible from recorded settings.

Language: Français · English