Start studying
Module 4 · NLP Module contents
Module 4  ·  Card 18 / 20intermediate

Language Modelling

A language model assigns a probability to text, which amounts to predicting what comes next.

Train it on enough text and the prediction task alone forces it to learn grammar, facts and style.

No labels are needed, since the next word in the corpus is its own answer.

That is why the internet could be used as training data, and why the models grew so large.

Everything a chat model does is still this one operation, run over and over.

Learn in depth ↓
Module 04 · NLP · 20 concepts
In depth  /  15 lines

Language Modelling, in more detail

A language model answers one question: how likely is this sequence of words?

By the chain rule of probability, that question decomposes into predicting each word given the ones before.

So a language model is, mechanically, a next-word predictor and nothing more.

The reason this matters is that the training target comes free with the data.

Every position in every document supplies both an input and the correct answer.

That makes it self-supervised learning, and it is why the entire web became usable as training data.

No annotation budget bounds the corpus, so the only limits are compute and available text.

The surprise of the last decade is how much is learned in service of that narrow objective.

Predicting the next word well requires syntax, since the answer must be grammatical.

It requires facts, because completing the capital of France is has exactly one good answer.

It requires tracking who is being discussed, and it requires some sense of style and register.

None of that was asked for; all of it is instrumental to lowering the prediction error.

The measure of a language model is perplexity, the effective number of words it was choosing between.

The earliest models counted n-grams, then recurrent networks arrived, then transformers.

Nothing about the objective changed across those generations; only the capacity to fit it did.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Language Modelling

The chain rule of probability

A language model assigns a probability to a sequence, and it does so by factorising that probability into a product of conditionals.

P(x1,…,xn)=∏i=1nP(xi∣x1,…,xi−1)P(x_1, \ldots, x_n) = \prod_{i=1}^{n} P(x_i \mid x_1, \ldots, x_{i-1})

This is not an approximation. It is an identity, true of any joint distribution over any ordered variables.

P(A,B,C)=P(A) P(B∣A) P(C∣A,B)P(A, B, C) = P(A) \, P(B \mid A) \, P(C \mid A, B)

The modelling choice is not the factorisation, it is deciding to learn each conditional with a neural network and to condition on everything before.

Why the left-to-right order

The identity holds for any ordering, so left to right is a choice rather than a necessity.

It is chosen because it matches generation: to produce text you need the next token given the past, which is exactly what this factorisation provides.

A model factorised in some other order would assign the same probability to a full sequence and be useless for writing one.

The objective

L=−1n∑i=1nlog⁡Pθ(xi∣x<i)\mathcal{L} = -\frac{1}{n} \sum_{i=1}^{n} \log P_\theta(x_i \mid x_{<i})

Maximising the likelihood of the training text is minimising this, which is the cross-entropy between the true next-token distribution and the model's.

Every token in the sequence is a training example, so a document of 2000 tokens gives 2000 supervised predictions from one unlabelled document.

That is the property that made the whole field possible: the supervision is free, because the next word is already there.

Teacher forcing, and the gap it leaves

train: P(xi∣x<itrue)generate: P(xi∣x<igenerated)\text{train: } P(x_i \mid x_{<i}^{\text{true}}) \qquad \text{generate: } P(x_i \mid x_{<i}^{\text{generated}})

During training the model always conditions on the real prefix, even when its own prediction for the previous token was wrong.

During generation it conditions on what it produced. One mistake enters the context and every later prediction is made from a prefix the model never saw in training.

That mismatch is called exposure bias, and it is part of why long generations drift while short ones are fine.

One training step, in order

  1. Take a document and tokenise it.
  2. Feed the whole sequence in at once, with a causal mask so no position sees its own future.
  3. Read off one predicted distribution per position.
  4. Compute cross-entropy against the actual next token at every position.
  5. Average and backpropagate.

Step two is why transformers train so much faster than recurrent models: all nn predictions are made in one parallel pass, not nn sequential ones.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
Why does language modelling need no labelled data?
NLP

20 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close