micrograd
A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API
A tiny educational autograd engine in about 100 lines of Python plus a PyTorch-like neural net layer on top, written by Andrej Karpathy.
Micrograd is a tiny educational library that shows how the core math inside AI neural networks actually works. A neural network learns by repeatedly adjusting its internal numbers — called weights — to get better at a task. The mechanism that figures out how to adjust those weights is called backpropagation. Micrograd implements backpropagation from scratch in about 100 lines of Python code so learners can see exactly how it works rather than just trusting a larger library to handle it invisibly.
The library defines a Value object that tracks every mathematical operation performed on a number — addition, multiplication, powers, and so on. After running a calculation forward (called a forward pass), you can call .backward() on the result and the library automatically figures out how much each input number contributed to the final output. This is the core of how neural networks learn.
On top of this engine, micrograd provides a small neural network library with a PyTorch-like interface — meaning its API looks similar to PyTorch, one of the most popular AI frameworks. The README includes a demo notebook that trains a two-layer neural network to classify data points into two groups.
You would use micrograd if you want to deeply understand how modern AI learning works under the hood, rather than just using a ready-made framework. It is aimed at students and curious developers. It is written in Python and distributed as an installable package.
Where it fits
- Read the 100-line engine to learn exactly how backpropagation works
- Train a tiny two-layer net to classify points in a notebook
- Use micrograd as the basis of a teaching module on automatic differentiation
- Extend the Value class with new operations as an exercise