Level 2 — RecallTokenization & Language Modeling

Tokenization & Language Modeling

30 minutes40 marksprintable — key stays hidden on paper

Level 2 — Recall (definitions, standard problems, short derivations) Time limit: 30 minutes Total marks: 40

Answer all questions. Use ...... notation for mathematics where needed.


Q1. Define tokenization in the context of language models. State one reason why subword tokenization is preferred over pure word-level tokenization. (4 marks)

Q2. Describe the core merge operation of Byte-Pair Encoding (BPE). Given the initial corpus of words (with counts) low ×5, lower ×2, newest ×6, widest ×3 and a starting vocabulary of individual characters, state which adjacent symbol pair is merged first and give its frequency. (5 marks)

Q3. Briefly contrast WordPiece and BPE in terms of the criterion used to select which pair to merge. (4 marks)

Q4. State two effects of increasing the vocabulary size in a language model — one advantage and one disadvantage. (4 marks)

Q5. Explain what tied (weight) embeddings means. If the embedding matrix has shape V×dV \times d, how many parameters are saved by tying the input embedding with the output projection? (4 marks)

Q6. Write the training objective (loss) for causal language modeling for a sequence x1,x2,,xTx_1, x_2, \dots, x_T. Explain in one sentence what "causal" enforces. (5 marks)

Q7. Contrast the Masked Language Modeling (MLM) objective of BERT with the causal objective. State the typical masking rate used in BERT. (4 marks)

Q8. Define Next Sentence Prediction (NSP). State the two class labels and how positive/negative examples are constructed. (4 marks)

Q9. A language model assigns the following per-token probabilities to a 3-token test sequence: 0.5, 0.25, 0.50.5,\ 0.25,\ 0.5. Compute the perplexity of the model on this sequence. (6 marks)


Answer keyMark scheme & solutions

Q1. (4 marks)

  • Tokenization = the process of splitting raw text into discrete units (tokens) — characters, subwords, or words — that are mapped to integer IDs for the model. (2)
  • Reason subword preferred: handles rare / out-of-vocabulary words by decomposing them into known subword units, keeping a compact vocabulary while avoiding <UNK> explosion. (2)

Q2. (5 marks)

  • Core operation: repeatedly find the most frequent adjacent pair of symbols in the corpus and merge them into a single new symbol, adding it to the vocabulary. (2)
  • Count adjacent pairs across the corpus:
    • es: from newest(6) + widest(3) = 9
    • st: from newest(6) + widest(3) = 9
    • lo: 5+2 = 7, ow: 5+2 = 7, we: 2+6 = 8, etc.
  • The pair es (or equivalently st) has the highest frequency = 9. First merge = e+ses, frequency 9. (3) (Accept st with freq 9 if consistently justified.)

Q3. (4 marks)

  • BPE merges the pair with the highest raw frequency (count of co-occurrence). (2)
  • WordPiece merges the pair that maximizes the likelihood of the training data, i.e. it picks the pair maximizing count(xy)count(x)count(y)\frac{\text{count}(xy)}{\text{count}(x)\,\text{count}(y)} (a pointwise-mutual-information-like score), not just raw frequency. (2)

Q4. (4 marks)

  • Advantage: larger vocab → shorter token sequences per text, more semantic content per token, fewer subword splits. (2)
  • Disadvantage: larger embedding/output matrices → more parameters and memory, rarer tokens seen less often (data sparsity / undertrained embeddings). (2)

Q5. (4 marks)

  • Tied embeddings = using the same weight matrix for the input embedding layer and the output (pre-softmax) projection layer. (2)
  • Untied: input V×dV\times d + output V×dV\times d = 2Vd2Vd params. Tied: VdVd. Saved = VdVd parameters. (2)

Q6. (5 marks)

  • Objective (negative log-likelihood): L=t=1TlogP(xtx1,,xt1;θ)\mathcal{L} = -\sum_{t=1}^{T}\log P(x_t \mid x_1,\dots,x_{t-1};\theta) (3)
  • "Causal" enforces that the prediction of xtx_t may depend only on preceding tokens (no access to future tokens) — enforced via a causal/autoregressive mask. (2)

Q7. (4 marks)

  • MLM: randomly mask a subset of tokens and predict the masked tokens using bidirectional context (both left and right); loss computed only on masked positions. Causal LM predicts the next token using left context only. (2)
  • Typical BERT masking rate = 15% of tokens. (2) (Bonus context: of those, 80% → [MASK], 10% random, 10% unchanged.)

Q8. (4 marks)

  • NSP = a binary classification task where the model is given two segments A and B and predicts whether B actually follows A in the original text. (2)
  • Labels: IsNext (positive) / NotNext (negative). Positive = B is the real next sentence; negative = B is a random sentence from the corpus (usually 50/50 split). (2)

Q9. (6 marks)

  • Perplexity =exp ⁣(1Tt=1TlnP(xt))=(tP(xt))1/T= \exp\!\left(-\frac{1}{T}\sum_{t=1}^{T}\ln P(x_t)\right) = \left(\prod_t P(x_t)\right)^{-1/T}. (2)
  • Product =0.5×0.25×0.5=0.0625= 0.5 \times 0.25 \times 0.5 = 0.0625. (1)
  • PPL=0.06251/3PPL = 0.0625^{-1/3}. (1)
  • 0.0625=240.0625 = 2^{-4}, so PPL=24/3=21.3332.52PPL = 2^{4/3} = 2^{1.333\ldots} \approx \mathbf{2.52}. (2)
[
  {"claim":"First BPE merge pair 'es' has frequency 9",
   "code":"es=6+3; result = (es==9)"},
  {"claim":"Tied embeddings save V*d parameters",
   "code":"V,d=sympy.symbols('V d'); untied=2*V*d; tied=V*d; result = sympy.simplify(untied-tied - V*d)==0"},
  {"claim":"Product of per-token probs is 0.0625",
   "code":"p=sympy.Rational(1,2)*sympy.Rational(1,4)*sympy.Rational(1,2); result = (p==sympy.Rational(1,16))"},
  {"claim":"Perplexity of sequence equals 2**(4/3) approx 2.5198",
   "code":"ppl=sympy.Rational(1,16)**(-sympy.Rational(1,3)); result = abs(float(ppl)-2.5198420997897464)<1e-6"}
]