Skip to main content

Exercises — Text and data

Self-check questions, exercises, and worked answers

← Back to the lesson: Text and data

Check your understanding

For integrated practice beyond these short checks, use the Text data exercise collection.

  1. A tokenizer assigns ID 17 to the token cat. This ID appears twice in a sequence. Do both occurrences select the same initial vector? Can you conclude that the number 17 encodes an amount of meaning?
Show the reasoning

Yes: the same ID selects the same row of the embedding table, hence the same initial vector (for fixed parameters). No: 17 is an arbitrary identifier in the vocabulary, not a measure of meaning, frequency, or similarity. The representations may later differ depending on the context processed by the Transformer.

  1. Two tokenizers split the same sentence into sequences of different lengths. Can their token IDs be compared directly, token by token?
Show the reasoning

No. IDs are defined only within a tokenizer’s vocabulary; ID 17 in one vocabulary need not denote the same token in another. Compare decoded tokens, conventions, and measured properties (length, coverage, cost), while recording each tokenizer’s version.

  1. Two faithful translations of the same passage produce sequences of different lengths with the same tokenizer. If the model has a fixed context window, what practical consequence can this have? Can we infer that the model handles the language using fewer tokens better?
Show the reasoning

The translation using more tokens consumes a larger share of the context budget: with the same budget, less room remains for history, documents, or the answer. Depending on the system, the longer sequence may also increase processing cost. But this does not show that the model handles the language using fewer tokens better: length measures representation efficiency on these texts, not the model’s language quality. Downstream results would need to be evaluated separately, by language and on comparable tasks.

Encode the prefix and its continuation

The film replies name aqueducts, sanitation, roads, and irrigation. Several members of the group speak in turn. To trace a single-speaker continuation, use “They built roads between distant towns.” Encode the prefix and this continuation separately with the tokenizer being studied to compare the splits. The continuation is absent from the context the model uses at the first generation step.

Success criterion: each ID list decodes exactly to its source text, the prefix and continuation are encoded separately with no merge at the junction, and the decomposition printed by the trace confirms the prefix split.

From text to embedding rows

A toy tokenizer splits “cat follows the cat” into [“cat”, “follows”, “the”, “cat”]. Its vocabulary maps those tokens to IDs [17, 42, 8, 17]. The embedding table \(E\) has shape \((V,d)\) and its rows are indexed from zero.

  1. Describe the objects obtained from text through vector lookup. What are the shapes of the ID sequence and the vector array?
  2. List the rows of \(E\) read in order and compare the lookup vectors for the two occurrences of “cat”.
  3. The vocabulary swaps IDs 17 and 42 and keeps the same segmentation. Which rows are read if \(E\) stays as it is? What change to \(E\) restores the original sequence of vectors?
Show the reasoning
  1. The tokenizer splits the text into tokens, then the vocabulary supplies their IDs. The ID array has shape \((4,)\) and the lookup produces an array of shape \((4,d)\).
  2. The lookup reads rows 17, 42, 8, and 17 of \(E\), in that order. Both “cat” tokens select the same initial vector. The ID serves as a row index. Later contextualized states can differ according to the context accessible at each position.
  3. With the renumbered vocabulary and the original table, the lookup reads rows 42, 17, 8, and 42. Swapping rows 17 and 42 in the table as well restores the original vectors at all four positions. The vocabulary and the table must use the same indexing convention.

Return to the review plan · From text to tokens · From IDs to representations

From embedding to first-block input

Use the original sequence of IDs \((17,42,8,17)\). Set \(d=2\) and \(E[17]=(2,-1)\) in a GPT that adds a position vector before the first block. The two occurrences of “cat” are at positions \(0\) and \(3\), with \(P[0]=(0,1)\) and \(P[3]=(1,0)\).

  1. Compute the two first-block inputs for “cat.” Which operation produces their difference?
  2. After studying the Transformer. Which operation then allows each state to depend on the other tokens in its prefix?
Show the reasoning
  1. Both occurrences read the same embedding, \(E[17]\). The inputs are \(E[17]+P[0]=(2,0)\) and \(E[17]+P[3]=(3,-1)\). The difference comes from the positional term.
  2. Causal self-attention combines information from the accessible positions. It can contextualize the two states using their respective prefixes.

Return to the review plan · From IDs to representations · Understand Transformer computation

Language: Français · English