Lesson 6.8core6 h

Project: character-level language model

From bigram counts to an attention-equipped seq2seq, all from scratch

You now know every ingredient of a neural language model. Building one from counts upward — following Karpathy's makemore — makes the next-token objective, embeddings, perplexity and sampling concrete, and prepares you to build GPT itself in 7.8 Build GPT from scratch (nanoGPT walkthrough). Adding a tiny attention-based seq2seq model closes Phase 6 by implementing the equations of 6.7 Attention is born with your own hands.

You'll be able to

  • Build a bigram model from counts and reproduce it as a trained one-layer network with identical loss
  • Implement an MLP character language model with learned embeddings and evaluate it by perplexity
  • Sample from a language model with temperature and interpret the results
  • Implement a small attention-based seq2seq model with greedy and beam-search decoding

Key ideas

  1. A bigram model from counts and a one-layer network trained with NLL are the same model: the one-hot input selects a row of W, so W learns log-counts (≈2.45 nats on names).
  2. L2 regularization on the weights behaves like add-k smoothing on the counts.
  3. An MLP over embedded previous characters shares statistics across contexts and beats bigrams (dev NLL ≈ 2.1–2.2).
Show 4 more key ideas
  1. Initial loss should be ≈ log(V); a much larger value means the initialization makes confident wrong guesses.
  2. Temperature divides logits before softmax: < 1 sharpens (safe, repetitive), > 1 flattens (creative, noisy).
  3. A GRU seq2seq with dot-product attention and teacher forcing solves date normalization nearly perfectly; attention plots show where the decoder looks.
  4. Keep results comparable: same splits, same tokenization, perplexity per character.

Suggested path through this lesson