Phase 5 - NLP & Transformers
Text is not numbers, and every step from one to the other throws something away. This phase measures each step: which words to keep, how to split them, what “similar” means, and what attention buys over recurrence.
Two results frame everything else. A linear model over word counts beat every neural model here — 0.8632 against a transformer’s 0.8462, in 8.4 seconds against 460. And on a task that needs two composed operations, one attention block scored 0.3120 while two scored 0.9990. Neither is a slogan; both are measurements, and they point in opposite directions about when architecture matters.
The pages
Section titled “The pages”| # | Page | The measured headline |
|---|---|---|
| 441 | Text Preprocessing | lowercasing this corpus changes 0 words — it was pre-processed; br is the 7th most frequent token |
| 441.5 | Subword Tokenization | BPE at 2,070 pieces reaches OOV 0.0000 against 0.0293 for 20,000 words |
| 442 | Bag of Words & TF-IDF | sublinear TF-IDF 0.8748 in 0.4 s; TF-IDF at 2,000 features beat counts at 30,000 |
| 443 | Word Embeddings | sentiment-trained vectors collapse to cosine 1.000; skip-gram finds movie/film at 0.746 |
| 444 | Sentiment Analysis | five families: TF-IDF 0.8632 wins, transformer 0.8462 at 55× the cost |
| 445 | Named Entity Recognition | token accuracy 0.8011, entity F1 0.4389 — and all-O scores 0.5653 |
| 445.5 | Attention from Scratch | one block 0.3120, two blocks 0.9990; unscaled attention peaks at 0.9355 |
| 446 | The Transformer Architecture | every ablation helps at 1 block; removing layer norm costs 0.0612 at 4 |
| 447 | Sequence-to-Sequence | attention took exact match 0.6250 → 0.9770 for 768 parameters |
| 447.5 | Evaluating Language Models | neural 189.81 vs bigram 226.47; BLEU gives a correct reordering 0.0000 |
flowchart TD A["441 tokens
Zipf, Heaps, OOV"] --> B["441.5 subwords
BPE, zero OOV"] B --> C["442 counts
BoW, TF-IDF"] C --> D["443 embeddings
dense, learned"] D --> E["444 the bake-off
five families"] D --> F["445 tagging
spans, not tokens"] E --> G["445.5 attention
Q, K, V"] G --> H["446 the block
norms, residuals"] H --> I["447 encoder-decoder
teacher forcing"] I --> J["447.5 evaluation
perplexity, BLEU"]
Four results that contradict the usual story
Section titled “Four results that contradict the usual story”- The simplest model won. TF-IDF with logistic regression scored 0.8632 against an average embedding’s 0.8575, a convolution’s 0.8495, a transformer’s 0.8462 and an LSTM’s 0.8415 — with 9,768 parameters and 8.4 seconds. Sentiment is close to a keyword task, and 8,000 reviews is not much data.
- A transformer block’s components do not help a shallow transformer. At one block, removing the feed-forward network, the residuals or the layer norms all raised accuracy. Layer normalisation only earns its 128 parameters at depth, where removing it costs 0.0612.
- Sentiment-trained embeddings collapse.
terrible,poor,wasteandbadend up at cosine 1.000 — parallel vectors — because a one-dimensional loss needs exactly one direction. The same corpus trained on co-occurrence givesmovie/filmat 0.746. - Attention’s win is convergence, not accuracy — until composition is needed. The transformer matched a bag of words (−0.0002) but reached its best score in epoch 1. On the induction task, where the model must match a cue and shift one position, one block cannot do it at all.
Four corpora that were too easy, and what fixing them showed
Section titled “Four corpora that were too easy, and what fixing them showed”Three modules in this phase produced perfect or meaningless scores on the first attempt. All three are now rebuilt, and the fixes are the most transferable content here.
| Experiment | First attempt | The flaw | After fixing |
|---|---|---|---|
| NER | every model 1.0000 | entity strings shared between train and test — the benchmark measured a word list | test pools disjoint, 38% unknown tokens → best F1 0.4389 |
| seq2seq | everything 1.0000 | a bare date is trivially learnable | date buried in filler → attention worth +0.3520 exact match |
| skip-gram | accuracy 0.4585 (below chance) | positives built first, so validation_split took only negatives | shuffled → 0.6470, and neighbours became semantic |
| perplexity | n-grams and neural in one table | scored on different test sets (231,210 tokens vs 30,000 windows) | same windows → neural 189.81 beats bigram 226.47 |
A fifth was a metric that flattered rather than a corpus that was easy: reporting the final epoch’s perplexity instead of the best gave 4,062.44 where the minimum was 189.81.
The habit this phase teaches
Section titled “The habit this phase teaches”Every page compares against something that costs nothing:
| Task | The do-nothing baseline | What it exposed |
|---|---|---|
| vocabulary choice | coverage at each cap (0.7624 at 1,000 words) | 23.8% of the text discarded silently |
| classification | TF-IDF + logistic (0.8632) | four neural families that never beat it |
| tagging | predict O everywhere (0.5653) | a model at 0.5750 token accuracy finding almost nothing |
| language modelling | uniform = vocabulary size (4,997.00) | whether a perplexity is meaningful at all |
| generation | greedy exact match, not per-character | 0.9217 character accuracy = 0.6250 exact |
What carries into the rest of the module
Section titled “What carries into the rest of the module”| Idea from this phase | Where it comes back |
|---|---|
| attention over a set of tokens | diffusion U-Nets, every modern generative model |
| pre-norm blocks with residuals | Phase 6’s architectures, Phase 8’s scaling |
| teacher forcing and exposure bias | autoregressive image and audio generation |
| perplexity and its tokenizer dependence | Phase 6’s text generation page |
| “measure against the do-nothing baseline” | every remaining phase |
-
TF-IDF with logistic regression scored 0.8632 in 8.4 seconds against a transformer's 0.8462 in 460. What is the defensible conclusion?
pch.quizShowAnswer
B — On 8,000 reviews of a near-keyword task, the extra machinery has nothing to learn that counts do not already capture - which is a claim about this budget and this task, not about transformers
-
One attention block scored 0.3120 on the induction task while two scored 0.9990. Why can one block not do it?
pch.quizShowAnswer
B — The task needs two composed operations - match a cue, then shift one position - and a single attention layer can only perform one round of look-up before the output is read
-
Embeddings trained on the sentiment objective put `terrible`, `poor`, `waste` and `bad` at cosine 1.000 - exactly parallel. What caused that?
pch.quizShowAnswer
B — A one-dimensional loss needs exactly one direction, so every word useful for the task collapses onto that axis and all other structure is discarded
-
NER token accuracy was 0.8011 while entity F1 was 0.4389, and labelling everything `O` already scores 0.5653 on tokens. What is the lesson?
pch.quizShowAnswer
B — Token accuracy on a task dominated by one class is close to a do-nothing baseline; the unit you evaluate has to be the unit you care about, which for NER is the whole span
-
At one transformer block, removing the feed-forward network, the residuals or the layer norms all RAISED accuracy; at four blocks, removing layer norm cost 0.0612. What does that pattern say?
pch.quizShowAnswer
B — Those components solve problems that only appear with depth, so a shallow ablation measures their cost without ever exposing their benefit
pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading