Lecture 4 · Tue, 20 Oct 2026 · Leif Anders Thorsrud & Vegard H. Larsen

Learning from prediction 1: n-gram language models

N-grams, character-level language models, and the fundamentals of next-token prediction

Open slides

Overview

Predicting the next token is the idea behind every modern language model — this lecture builds the smallest possible version of it, one character at a time. From 32,033 names we count a 27×27 bigram table that can already generate new names; then a one-layer neural network trained by gradient descent rediscovers the same solution (loss 2.46 vs 2.45); then an MLP with embeddings and a five-character context beats both (2.17). From there to GPT is scale, not magic — the loss function never changes.

Topics

  • The chain rule, the Markov assumption, and n-gram models
  • Counting a character-level bigram model: estimation, sampling, smoothing
  • Evaluating with negative log likelihood — the loss every LLM minimises
  • The same model as a neural network; why the learning framework scales
  • The MLP upgrade: embeddings, longer context, hidden layer

Readings

  • J&M chapters 3 and 7

Asynchronous work

Materials

Before lecture 5

  • Run the three notebooks — practical day 1 builds on them
  • Watch the Karpathy video if you haven’t

GRA 4164 Text as Data · Fall 2026 · BI Norwegian Business School