← LibraryBlog · AI

A Recipe for Training Neural Networks

Andrej Karpathy
✓ Source-grounded

Success in deep learning requires a paranoid, methodical process of building from simple to complex while verifying every hypothesis with rigorous visualization and testing.

Read the original

In one sentence

Neural network training is a "leaky abstraction" that fails silently; therefore, you must succeed by being thorough, defensive, and obsessed with visualizations of every possible thing.

Why read this (and who it's for)

This guide solves the "large gap" between understanding how a convolutional layer works and actually achieving state-of-the-art results. It is for ML engineers and researchers who find themselves frustrated by models that train but don't perform, or those who struggle to debug why a specific architecture isn't converging. After reading this, you will have a systematic "recipe" to build robust, debuggable pipelines that eliminate silent logical errors.

The big idea

Karpathy argues that neural network training is fundamentally different from standard software engineering. In standard software (like the requests library), clean APIs hide complexity. In neural networks, these abstractions are "leaky." Backpropagation and Stochastic Gradient Descent (SGD) do not magically make a network work; they are tools that require deep understanding of how they interact with your specific data.

Because neural networks can "fail silently," the standard "fast and furious" approach to development leads to suffering. A model might run without throwing an exception but could be learning from flipped labels, off-by-one bugs in autoregressive models, or incorrect weight initializations. To succeed, you must replace speed with patience and attention to detail, building your system from simple to complex while verifying every hypothesis with a concrete experiment.

Key ideas

Leaky Abstractions

What it is — The false impression that deep learning libraries make training "plug and play." Unlike standard software where APIs hide complexity, NN components (like Batch Norm or RNNs) do not magically solve problems without understanding the underlying mechanics. Why it matters — If you treat ML like standard software, you will fail to diagnose why a model isn't converging when you deviate slightly from a standard ImageNet classifier. In the book — Karpathy compares NN training to the requests library: while requests hides query strings and HTTP connections behind a clean API, NN training requires you to understand the "query strings" of backpropagation.

Silent Failures

What it is — Errors in neural networks are often logical rather than syntactic. The code may run perfectly but the model might be learning from corrupted data or buggy logic. Why it matters — These bugs are hard to unit test and can cause a model to "train but silently work a bit worse," making them incredibly difficult to find without a methodical process. In the book — He cites examples like an autoregressive model taking the thing it's trying to predict as an input due to an off-by-one bug, or a network learning to "internally learn to detect flipped images and then it left-right flips its predictions."

Become One with the Data

What it is — The first step of training is not touching neural net code but instead spending hours manually inspecting thousands of examples. Why it matters — Your brain is better at identifying patterns, biases, and noise than an initial network; understanding these nuances informs the architecture you will eventually need. In the book — Karpathy describes scanning data to find duplicates, corrupted labels, and imbalances, asking questions like: "Are very local features enough or do we need global context?"

The "Don't be a Hero" Rule

What it is — Resist the temptation to build exotic, creative architectures in the early stages of a project. Why it matters — Unverified complexity introduces bugs that take forever to find; you should start with a proven baseline first. In the book — He advises: "simply find the most related paper and copy paste their simplest architecture." For example, if classifying images, just copy-paste a ResNet-50 for your first run.

Input-Independent Baselines

What it is — Training a model where all inputs are set to zero. Why it matters — This verifies that the model is actually extracting information from the input data rather than learning something else (or nothing at all). In the book — If your model performs worse when you plug in real data than when you use zeroed-out inputs, then your model isn't learning anything from the input.

Overfitting as a Diagnostic

What it is — Ensuring that a model can reach near-zero loss on a single batch of only a few examples. Why it matters — If you cannot overfit a tiny bit of data, there is a bug in your training pipeline and you cannot proceed to the next stage. In the book — He suggests increasing the capacity of the model (adding layers/filters) for this specific test and visualizing that the labels and predictions align perfectly.

Visualization as Source of Truth

What it is — Visualizing the data immediately before the y_hat = model(x) call. Why it matters — This is the only "source of truth" to reveal problems in preprocessing, augmentation, or vectorization bugs. In the book — Karpathy notes this has saved him countless times from discovering problems that were hidden by the abstraction of the data loader.

Frameworks & tools

The Training Recipe

Follow these steps in order to build a successful model:

  1. Data Inspection: Manually scan thousands of examples for biases, noise, and distribution patterns. Write simple code to filter/sort outliers.
  2. Skeleton Setup:
    • Fix the random seed (to ensure reproducibility).
    • Disable all data augmentation (remove "unnecessary fanciness").
    • Verify loss @ init (e.g., check if softmax starts at -log(1/n_classes))).
    • Initialize final layer weights correctly (e.g., set bias to the mean of your target values).
    • Establish a human baseline (compare model accuracy to your own manual accuracy).
  3. Overfit Stage:
    • Pick a standard architecture from a related paper ("Don't be a hero").
    • Ensure training loss goes down as you increase capacity slightly.
  4. Regularization Stage:
    • Add more real data (the "by far best" way to regularize).
    • Apply aggressive data augmentation or creative simulations.
    • Use pretraining if available.
    • Decrease model size/input dimensionality to remove spurious signals.
    • Use Dropout2d, Weight Decay, and Early Stopping.
  5. Tuning:
    • Use Random Search (not Grid Search) for hyperparameters.
  6. Squeeze the Juice:
    • Use ensembles (guaranteed ~2% accuracy gain).
    • "Leave it training" longer than expected (sometimes SOTA results appear after much longer training).

Memorable examples & stories

The Flipped Label Bug — A network might learn to detect flipped images and then flip its own predictions, hiding a bug in your data augmentation. Lesson: Logical errors are often hidden by the model's ability to adapt.

The Winter Break SOTA — Karpathy left a model training during winter break; when he returned in January, it had reached state-of-the-art results. Lesson: Networks often train for unintuitively long times; don't stop just because validation loss levels off.

The "Requests" Analogy — Standard software libraries like requests hide the complexity of HTTP connections, but NN training is a leaky abstraction where you must understand the underlying mechanics. Lesson: Don't trust the library to do everything for you.

Key quotes

"Neural net training is a leaky abstraction." Why it matters: Warns against treating ML like standard software development.

"A 'fast and furious' approach... does not work and only leads to suffering." Why it matters: Success requires patience and attention to detail.

"Don’t be a hero." Why it matters: Avoid unnecessary complexity in early stages; copy-paste proven architectures first.

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 is strictly grounded in the provided text. I am certain about the "Recipe" steps and the core warnings regarding leaky abstractions and silent failures. The reader should verify specific initialization values (like the -log(1/n_classes)) formula) against the original blog post to ensure they match their specific loss function (e.g., L2 vs Huber).

Keep going — related reads

More from Andrej Karpathy