← LibraryBlog · AI

The Unreasonable Effectiveness of Recurrent Neural Networks

Andrej Karpathy
✓ Source-grounded

Training recurrent neural networks is not just optimization over functions, but optimization over programs, allowing them to learn arbitrary sequential logic and structures.

Read the original

In one sentence

The fundamental shift in RNNs is that they treat training as an optimization over programs rather than mere function mapping, enabling the model to simulate Turing-complete logic and learn complex sequential dependencies.

Why read this (and who it's for)

This text solves the conceptual hurdle of why some models "understand" sequence structure while others only memorize noise. It is essential for machine learning engineers and researchers who want to move beyond "black box" thinking to understand how hidden states act as internal variables in a running program. After reading this, you will be able to conceptualize RNN architectures as procedural logic and identify why certain structures (like LaTeX or C code) fail based on memory horizon limits.

The big idea

Most neural networks (Vanilla NNs or CNNs) map fixed-sized inputs to fixed-sized outputs via a fixed number of computational steps. They are essentially learning a static function: $f(x) = y$.

Recurrent Neural Networks (RNNs) break this mold by operating over sequences of vectors. Because the recurrent transformation is fixed and repeatable, there are no pre-specified constraints on sequence length. This allows the network to combine an input vector with a "hidden state" via a learned function to produce a new state. You can interpret this as running a fixed program with internal variables.

Because RNNs are Turing-Complete, they can simulate arbitrary programs given the proper weights. The core thesis is that while training vanilla nets is optimization over functions, training recurrent nets is optimization over programs. This allows the model to discover "hidden" structures—like the syntax of Markdown or the coordinate system of a URL—without them being hardcoded into the architecture.

Key ideas

Optimization over Programs

What it is — The shift from mapping $x \to y$ to maintaining an internal state $h$ that evolves over time. Why it matters — It means the model isn't just predicting a label; it is maintaining a "memory" of what has happened so far to inform what happens next. In the book — Karpathy argues: "If training vanilla neural nets is optimization over functions, training recurrent nets is optimization over programs."

Character-Level Modeling

What it is — A framework where characters are one-hot encoded and the model learns the probability distribution of the next character given all previous ones. Why it matters — It allows the model to learn spelling, grammar, and syntax simultaneously from raw text without explicit rules. In the book — The author uses this to train on Paul Graham's text, Shakespeare, and Wikipedia, showing how the model learns "coherent English" and structured Markdown.

Emergent Word-Space Structure

What it is — The observation that models first learn the general structure of language (short words, spaces) before they master specific vocabulary. Why it matters — It explains why training often looks like "noise" early on but suddenly "snaps" into coherence as the model discovers the underlying distribution. In the book — In an evolution of War and Peace, the model first learns spaces/punctuation (Iteration 300), then short words ("we", "He"), and only much later learns proper names and longer words (Iteration 2000).

Latent Feature Extraction (Neuron Visualization)

What it is — Identifying specific neurons that activate only for certain types of data (e.g., URLs or Markdown brackets). Why it matters — It proves that end-to-end training discovers useful quantities (like quote detection) automatically without human intervention. In the book — The author identifies a "URL Detector" and a "Time-aligned Coordinate System" that provides a linear coordinate system across [[ ]] scopes.

Hard Attention & Non-differentiability

What it is — A mechanism where a model samples a specific chunk of memory rather than looking at everything (Soft Attention). Why it matters — Because sampling is non-differentiable, you cannot use standard backpropagation; you must use Reinforcement Learning (RL) techniques like REINFORCE. In the book — Karpathy points to research like Show Attend and Tell and Reinforcement Learning Neural Turing Machines.

Memory Horizon Limitations

What it is — The reality that RNNs "forget" information if the dependency distance exceeds the model's effective memory reach. Why it matters — It explains why a model might generate valid-looking LaTeX but fail to close a \begin{proof} block correctly. In the book — In the LaTeX experiment, the model fails because the dependency is "too long-term"; it forgets the initial environment by the time it finishes the block.

Frameworks & tools

The Vanilla RNN Step Function

To build an RNN, you must implement this update equation:

  1. Hidden State Update: $h_t = \tanh(W_{hh} h_{t-1} + W_{xh} x_t)$
    • $W_{hh}$: Matrix for the previous hidden state.
    • $W_{xh}$: Matrix for the current input.
    • $\tanh$: Non-linearity to squash activations to $[-1, 1]$.
  2. Output Calculation: $y = W_{hy} h$
  3. Parameters: The model learns ${W_{hh}, W_{xh}, W_{hy}}$.

Character-Level Modeling Pipeline

To train a text generator:

  1. Encoding: Use 1-of-k (one-hot) encoding for characters.
  2. Loss Function: Use a standard Softmax classifier (cross-entropy loss) on every output vector simultaneously.
  3. Optimization: Use mini-batch Stochastic Gradient Descent with RMSProp or Adam.
  4. Sampling: At test time, sample from the distribution and feed it back as the next input to generate text.

Temperature Sampling

To control the "creativity" of generated text:

  1. Low Temperature ($\approx 0$): High confidence; leads to high-quality but repetitive loops (e.g., "is that they were all the same thing...").
  2. High Temperature: Higher diversity/randomness at the cost of more grammatical mistakes.

Memorable examples & stories

The Wikipedia Markdown Experiment

The model was trained on 96MB of raw markdown and learned to generate valid structured markdown (headings, lists) and correctly opened/closed parentheses, but it "hallucinated" URLs. Lesson: Models learn syntax perfectly but struggle with specific factual grounding.

The LaTeX Failure Mode

Trained on a 16MB source file, the model produced plausible-looking math but often failed to close environments (e.g., opening \begin{proof} and ending with \end{lemma}). Lesson: Memory horizon is a hard physical limit of the architecture.

The Linux Source Code Success/Failure

Trained on 474MB of C code, the model maintained pointer notation and indents well but used undefined variables and returned non-existing variables. It also amusingly recited the GNU license character-by-character before coding. Lesson: Models can learn "style" (indentation, syntax) much faster than "logic" (variable tracking).

The URL Detector Neuron

By visualizing hidden state activations, the author found a neuron that turned off sharply after the first "w" in "www". Lesson: Deep learning models discover useful quantities without hardcoding.

Key quotes

How to apply it

This week

Ongoing

Common pitfalls

Connects to

If you remember only three things

Internalize it

Fidelity note

Confidence: High. This summary captures the core mathematical definitions ($\tanh$ update), the specific experimental results (Linux code, LaTeX failures, Wikipedia successes), and the philosophical shift to "optimization over programs." I am certain of the distinction between Soft and Hard attention as presented in the text.

Keep going — related reads

More from Andrej Karpathy