Back
intermediate

Transformer basics

A short history of NLP

Rebuild a small text-classification problem through counts, embeddings, contextual models, and transfer learning.

Lesson 1 of 44About 25 min with practice

You need to distinguish “The film was good” from “The film was not good.” A word-count model sees nearly the same vocabulary. Instead of treating NLP history as a list of inventions, use this failure to ask what each representation makes easier or harder.

Before you begin: Understand features, embeddings, and held-out evaluation from the beginner track.

Count what is present

A bag-of-words representation counts tokens while discarding their order. It can work well for tasks with strong lexical signals, such as many document categories. Its weakness is not that counting is primitive; it is that the representation deliberately omits some information.

For the two film reviews, the presence of “not” may help a trained classifier. But a bag of isolated words has difficulty distinguishing “not good” from a sentence where “not” modifies something else. Adding n-grams, short sequences of adjacent tokens, lets a feature represent “not good” directly.

This increases coverage of local order at the cost of more sparse features. A phrase absent from training may still be difficult. The tradeoff is about representation and data, not a simple progression from useless to intelligent.

Learned embeddings give words dense vectors that can capture useful relationships from training contexts. A model may transfer some information between “excellent” and “good” rather than learning every word independently.

A single static vector still conflates senses. “Bank” in a loan question and “bank” in a fishing report start with the same word representation in a static embedding table. Contextual models build representations that depend on the surrounding sequence, helping later decisions distinguish these uses.

Which information did the representation throw away?

The two sentences contain the same words, but they assign the action to different participants. A bag of word counts cannot recover that distinction once order has been discarded. A later classifier may be powerful, yet it cannot reliably reconstruct information that its input representation does not preserve.

Read the comparison before choosing a model. Ask which part of the sentence determines the desired label, then inspect whether the representation keeps that part. This habit connects older NLP methods to current ones: first identify the information a task needs, then choose a representation and a learning method that can use it.

Example

“The tutor helped; the machine did not.”

What changes

Swap tutor and machine while keeping the word counts identical.

Result

Who helped changes, but a bag-of-words vector remains the same.

An identical representation cannot distinguish two cases by itself. The example isolates the information lost by ignoring order.

Make the sequence matter

RNNs and LSTMs process sequences through recurrent states. They offered a learned way to incorporate order and context. Attention later gave models more direct access to relevant positions, while transformers made attention central and enabled highly parallel training over positions.

These architectures did not erase the need for good labels or representative data. If a review dataset leaks the rating in a filename, many architectures can exploit that shortcut. A stronger representation does not repair an invalid experiment.

Reuse learning across tasks

Transfer learning starts from a model trained on a broad task and adapts it to a narrower one. This reduces the need to learn every representation from a small labeled dataset. ULMFiT, contextual representation methods, BERT, and generative pretrained models explored different ways to reuse language learning.

The choice includes what to reuse: frozen features, a model whose weights are fine-tuned, or a model prompted with examples. These mechanisms differ in data requirements, computation, and how updates are deployed. Keep them separate when reading papers that all use the word “pretrained.”

Compare on one controlled problem

Build a small review set with negation, mixed opinions, and unfamiliar synonyms. Compare a keyword baseline, a count-based classifier if you can train one, and a pretrained model. Keep the test split and label definition fixed. Report which errors change, not only the final score.

For a paper-only exercise, inspect the representation each method receives. The sentence “Not only was the film good, it was beautifully paced” is a useful challenge to the rule “not means negative.” A model needs to interpret the construction rather than treat the token as a universal negative flag.

Choose a representation for the job

Your task is to route documents containing exact product codes. A large model handles prose well but occasionally changes a code. Would replacing exact matching with embeddings necessarily improve the system?

Keep the useful old method

No. Exact codes are often well served by lexical matching or a validated parser. You can combine exact matching for identifiers with semantic methods for natural-language descriptions. Historical methods remain useful when their assumptions fit the task. Evaluate the combined system rather than treating novelty as a requirement.

The next chapter reads the original transformer as a solution to sequence modeling, with attention to what its architecture actually contained.

Practice with feedback

Lesson challenge

Choose a text representation for one controlled problem

Two messages: "the tutor helped, the machine did not" and "the machine helped, the tutor did not". You must tell them apart.

Match bag-of-words, static embeddings, and contextual embeddings to what each can and cannot express.

Check your understanding

Question 1 of 3
What does a bag-of-words representation give for these two messages?
Score: 0/0

Your task

Demonstrate the failure empirically: show bag-of-words cannot separate the pair, then state the minimum representation that can.

These notes stay on this page. Download them before leaving. Code in this field is not executed.

What to include

  • Both vectors use the same vocabulary ordering
  • You show equality explicitly rather than eyeballing it
  • Bigrams are shown to separate the pair
  • You name a case bigrams still miss, such as long-range dependency
Compare with a worked answer

Here is one way to answer. Check how it uses the information in the task.

from collections import Counter

a = 'the tutor helped the machine did not'.split()
b = 'the machine helped the tutor did not'.split()

vocab = sorted(set(a) | set(b))
vec = lambda toks: [Counter(toks)[w] for w in vocab]
print(vec(a) == vec(b))          # True: indistinguishable

bigrams = lambda t: Counter(zip(t, t[1:]))
print(bigrams(a) == bigrams(b))  # False: ('tutor','helped') vs ('machine','helped')

# Bigrams separate this pair because the distinguishing relationship spans
# two adjacent tokens. They still fail when the dependency is long, e.g.
# 'the tutor, who arrived late after the bus broke down, helped' - the pair
# (tutor, helped) is now 9 tokens apart and no fixed n-gram window sees it.
# That gap is what attention closes.

When you are signed in, opening the challenge carries your edited working notes into its draft in this browser. The challenge has its own completion record. Practising here does not award points or mark it complete.

Sources

Word2vec, ULMFiT, and BERT provide primary examples of representation and transfer-learning approaches. Their historical results are not presented as current product rankings.

Practise this lesson

Choose a text representation for one controlled problem

Match bag-of-words, static embeddings, and contextual embeddings to what each can and cannot express.

About 9 min55 points3 checks and one applied task
Loading your lesson progress...