Run it yourself

The notebook

Everything this site shows you, as code you can run and change. Six parts, 19 code cells, no GPU needed.

Three ways to run it

  1. Google Colab

    Nothing to install, and the downloads happen on Google's machines rather than yours. Download the file above, openColab, then choose File, then Upload notebook. A CPU runtime is enough for every part.

  2. Jupyter, on your own machine

    You need Python 3.9 or newer. From a terminal, in a fresh virtual environment:

    pip install jupyterlab gensim transformers torch matplotlib
    jupyter lab modern-nlp-hands-on.ipynb

    Torch is the large one. If the install fights you, Colab sidesteps it entirely.

  3. Read it without running it

    Every result the notebook produces is already on this site, in a form you can poke at. The chapters below say which part backs which explorer, so you can go either way.

What it downloads the first time

About 741 MB of model weights, once, then cached. These are the sizes the hosts report for the files the notebook actually pulls, not estimates.

glove-wiki-gigaword-50
66 MBused by part 1
bert-base-uncased
420 MBused by parts 2, 4 and 5

Part 3 downloads nothing at all: attention is built from scratch in NumPy, so you can run that part on a train.

The six parts

  1. 0

    Setup

    Installs gensim and transformers. On Colab this is the only cell that takes a while.

  2. 1

    Static word embeddings

    Loads real GloVe vectors, finds nearest neighbours, does the king minus man plus woman arithmetic, and draws the space in two dimensions with PCA done by hand.

  3. 2

    Contextual embeddings

    Pulls the vector for one word out of several sentences and measures how far apart the senses sit, layer by layer.

  4. 3

    Attention from scratch

    Builds scaled dot product attention in NumPy, shows what the square root of d_k is for, adds a causal mask, splits it into heads, and checks that without positional encoding the whole thing is blind to order.

  5. 4

    Inside a real BERT

    Reads the attention weights out of all 144 heads and hunts for the ones that link a pronoun to its noun.

  6. 5

    Masked language modelling

    The task BERT was pretrained on. Hide a word, see what it guesses, and how confident it is.

  7. 6

    Sentiment, and exercises

    A fine tuned classifier on your own sentences, then a few things to try on your own.

Where this file comes from

Generated from the source notebook byscripts/publish-notebook.mjs, with the outputs cleared so you get your own. 36 cells, checksum732c28071849. The site reads data produced by this same code, so if a number here disagrees with a number there, one of them is a bug and worth telling me about.