Training recurrent neural networks is not just optimization over functions, but optimization over programs, allowing them to learn arbitrary sequential logic and structures.
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.
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.
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.
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."
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.
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).
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.
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.
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.
To build an RNN, you must implement this update equation:
To train a text generator:
To control the "creativity" of generated text:
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.
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.
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).
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.
This week
Ongoing
Reflection questions
Flashcards Q: What is the fundamental difference between training vanilla NNs and RNNs? A: Vanilla nets are optimization over functions; recurrent nets are optimization over programs. Q: Why do some RNNs fail to close LaTeX environments correctly? A: The dependency distance is "too long-term" for the model's memory horizon. Q: What is the computational cost of doubling a hidden state vector? A: It quadruples the FLOPS per step. Q: Why does Hard Attention require Reinforcement Learning (REINFORCE)? A: Because sampling a specific chunk of memory is a non-differentiable interaction. Q: What was the observed order of learning in the War and Peace experiment? A: The model first learns general word-space structure (spaces, short words) before learning longer words/names.
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.
Deep LearningIan Goodfellow, Yoshua Bengio, Aaron Courville
#333 – Andrej Karpathy Tesla AI, Self-Driving, Optimus, Aliens, and AGILex Fridman PodcastA from-scratch tour of Bitcoin in PythonAndrej KarpathyA Recipe for Training Neural NetworksAndrej KarpathyA Survival Guide to a PhDAndrej KarpathyBiohacking LiteAndrej KarpathyDeep Neural Nets 33 Years Ago and 33 Years From NowAndrej KarpathyDeep Reinforcement Learning Pong from PixelsAndrej KarpathymicrogptAndrej Karpathy
No Priors Ep. 80 With Andrej Karpathy from OpenAI and TeslaNo PriorsShort Story on AI A Cognitive DiscontinuityAndrej KarpathyShort Story on AI Forward PassAndrej Karpathy
Skill Issue Andrej Karpathy on Code Agents, AutoResearch, and the Loopy Era of AINo Priors