LLMUnit 1 · The foundations · Day 01

The Unreasonable Effectiveness of Recurrent Neural Networks

Train a network only to predict the next character, and it learns spelling, syntax and document structure for free.

AuthorAndrej Karpathy
Year / venue2015 · blog post
Core ideaNext-token prediction
Study time2–4 h

Original: karpathy.github.io · Code, notebook and exercises: 30u30 Day 1

Start here (no maths needed)

If a computer gets really good at guessing the next letter, it ends up learning how words, sentences and even computer code are put together.

Think of it like this

Play a game with a friend: you read out a sentence one letter at a time, and they guess the next letter. "The cat sat on th…" Your friend shouts "e!" To be good at this game, your friend has to know spelling, grammar and what usually comes next. Nobody taught them those rules for the game. They just know them from reading a lot. This computer program learns the same way, by playing the guessing game millions of times.

How it works

  1. The program reads one letter.
  2. It keeps a little "memory note" about everything it has read so far.
  3. It uses the letter plus the note to guess the next letter.
  4. If it guessed wrong, it adjusts itself a tiny bit so it does better next time.
  5. Repeat this millions of times. Now it can write its own text, one letter at a time.

Why robots care

A robot also sees the world one moment at a time: camera picture, then the next one, then the next. Guessing "what happens next?" is how a robot can learn how the world works. For example, it can learn that a ball rolling toward the table edge is about to fall.

Try it

Words to know

Neural network
A computer program that learns from examples instead of being given rules.
RNN
A neural network with a memory note it carries from one step to the next.
Training
Showing the program lots of examples so it slowly gets better.
Temperature
A dial for how adventurous the guesses are.

Going deeper: everything below is the full version, with some maths. On a first read, skim the headings and the "Why it matters for robotics" box, and come back for the rest later.

1. Summary

TL;DR

Karpathy trained character-level LSTMs on Shakespeare, Wikipedia, LaTeX and the Linux kernel source. With nothing but "predict the next character", the models produced text with correct form: speaker names in capitals, balanced brackets, indented code, plausible markup. They didn't get the meaning right. The post made a broad audience see that a simple predictive objective can force a network to learn deep structure. That's the same bet modern LLMs scale up.

2. The core idea

At each time step t, the RNN reads one input x_t (a one-hot character) and updates its hidden state:

h_t = tanh(W_xh · x_t + W_hh · h_{t−1} + b_h)
y_t = W_hy · h_t + b_y
p_t = softmax(y_t / T)        # T = sampling temperature

Three weight matrices, reused at every step. h_t is the network's entire memory of the past. Training unrolls the loop over a window of characters and back-propagates through all of it. This is backpropagation through time (BPTT), usually truncated to a fixed window such as 25–100 steps.

Generation runs the model in a loop: sample a character from p_t, feed it back in as the next input, repeat. Temperature T reshapes the distribution. Low T is safe and repetitive. High T is creative, then chaotic.

3. Deep dive

Why prediction forces understanding of structure

To lower its loss on Linux source, the model must know that { needs a matching }, possibly hundreds of characters later. That means storing "how many braces are open" in h_t. No one programs that counter. Gradient descent finds it because it reduces prediction error. This is the key intuition behind self-supervised learning: the data supplies its own labels, and a hard enough prediction task makes useful representations necessary.

What it couldn't do, and why that matters

Temperature, made concrete

Interactive: softmax temperature

Model logits for the next character after "th". Drag T and watch the distribution sharpen or flatten.

P(top choice)
–
Entropy (bits)
–

4. Why it matters for robotics

5. How to study it

  1. Read Karpathy's post end to end (about 1 h). Note the visualisations of individual neurons.
  2. In the 30u30 Day 1 folder, read implementation.py and run train_minimal.py on a small text. Watch samples improve over training.
  3. Do the exercises in order, then sweep temperature on your trained model.
  4. Stretch: train on a log file from a robot or any machine, and see what structure it picks up.

6. Self-check

What is the RNN's only memory of the past?

The hidden state vector h_t. Its size is fixed regardless of how long the input is.

Why does a character model learn to balance braces without being told?

Predicting a closing brace correctly lowers the loss. Storing the open-brace count in the hidden state is the cheapest way to do that, so gradient descent finds it.

What does lowering temperature toward 0 do to sampling?

It approaches greedy (argmax) decoding: the most likely character every time, which tends to loop and repeat.

Why is BPTT usually truncated?

Memory and compute grow with the unrolled length, and gradients over very long spans vanish or explode anyway.

7. Further reading