Skip to main content

micrograd: understanding backpropagation through a tiny implementation

A guided reading of a compact public implementation that makes reverse-mode automatic differentiation and a small neural-network library inspectable.

Original project — GitHubProgramming & Scientific ComputingIntermediate

Original source

Original project — GitHub

Andrej Karpathy's micrograd repository.

Open original source

Learning goal

Trace how local derivatives on a computation graph become parameter gradients through reverse-mode automatic differentiation.

Prerequisites

  • Basic derivatives
  • Basic neural-network terminology

Why make the implementation tiny?

Automatic differentiation can feel abstract when it is hidden inside a large framework. A small implementation exposes the data structure, the local derivative rules, and the order in which gradients are propagated.

The computation graph

Each scalar value can point to the values that produced it and retain the local operation. The forward pass builds a directed acyclic graph that can later be traversed in reverse.

Chain rule on a computation graph
y=f(g(x)),dydx=dydgdgdxy=f(g(x)),\qquad\frac{dy}{dx}=\frac{dy}{dg}\frac{dg}{dx}

Local derivatives

Every supported operation contributes a local derivative. Backpropagation does not require a new global symbolic derivative for the whole network; it composes these local rules using the chain rule.

Reverse-mode accumulation

Starting from the final scalar objective, the backward pass propagates how much the objective changes with respect to each intermediate value. Gradients from multiple downstream paths are accumulated.

From scalars to a small neural network

A compact neural-network layer can then be built from these scalar values. A neuron computes a weighted sum plus bias, followed by an activation; the autograd engine supplies gradients for the parameters.

What this project teaches

  • Backpropagation is repeated application of the chain rule on a computation graph.
  • A framework's automatic differentiation still rests on explicit local derivative rules.
  • A tiny implementation is useful for understanding, but it does not replace optimized tensor libraries for production-scale workloads.
  • Understanding the scalar mechanism helps explain why matrix and tensor implementations need careful batching, memory, and numerical design.

Connection to the site's AI learning path

This explainer sits between mathematical lessons on gradients and larger Transformer mechanics. The same habit applies throughout: start with the mathematical operation, make the data flow explicit, then inspect what the software implementation adds.

Need to make an AI or numerical mechanism understandable and inspectable?

A careful explanation can start from the mathematics and continue into a concrete implementation.