micrograd

A hundred-line scalar autograd engine and tiny neural net library written to teach backpropagation.

Open SourceSelf HostedOffline Capable
0.0 (0)

About

Andrej Karpathy wrote micrograd as a teaching artifact: an automatic differentiation engine where every number is a Value object that remembers how it was produced, so calling backward() walks the resulting graph in reverse and fills in gradients with the chain rule. The engine file is roughly a hundred lines and the companion nn module, which defines neurons, layers and a multilayer perceptron, is about half that. Because it works on single scalars rather than tensors it is far too slow for real workloads, yet small enough to read in one sitting and check by hand. The repository includes a notebook that trains a two-layer classifier on the moons toy dataset with a max-margin loss and plain SGD, another that draws the computation graph with Graphviz, and unit tests that compare gradients against PyTorch. Install with pip install micrograd or copy the two files. The code has been essentially frozen since 2020 by design; a 2026 update points readers to microgpt, a single-file GPT built on a faster take on the idea. MIT licensed. Ideal for students who want to understand backpropagation before relying on a full framework.

Should you use micrograd?

Pick it when

Pick this when you want to understand backpropagation by reading and stepping through an entire autograd engine in one sitting, for teaching a class, interview prep, or following along with Karpathy's video walkthrough.

Look elsewhere when

It works on scalars rather than tensors and has no GPU path, so it cannot train anything beyond toy classifiers. Once the ideas click, move to tinygrad for a small but real framework or to PyTorch for actual work.

Alternatives to micrograd

  • tinygrad

    Tensor-based with GPU backends and kernel fusion, so it runs real models; far more code to read than micrograd's roughly hundred lines.

  • PyTorch

    The same autograd ideas at production scale with GPU support; far too large to read end to end, which is exactly what micrograd allows.

  • nanoGPT

    A natural next step: about 600 lines of PyTorch GPT training code that teaches transformers rather than autograd internals.

Reviews (0)

Leave a Review

No reviews yet. Be the first to review!

Details

Price
Free
Platform
Local/Desktop
Difficulty
Beginner (1/5)
License
MIT
Added
Apr 3, 2026

Tags