GitHub · micrograd: A tiny scalar-valued autograd engine and a neural net library on top of it with PyTorch-like API
Open the sourceThis README presents micrograd, a tiny scalar-valued autograd engine (about 100 lines) implementing backpropagation over a dynamically built DAG, plus a ~50-line neural net library with a PyTorch-like API. Karpathy shows that operating on individual scalars is enough to build and train deep neural nets, and he points to microgpt as a single-file complete GPT algorithm where everything else is just efficiency. He frames it as potentially useful for education.
- micrograd implements reverse-mode autodiff (backpropagation) over a dynamically built DAG of scalar values. - Each neuron is chopped up into its individual tiny adds and multiplies, yet this suffices to build deep nets for binary classification. - The demo trains a 2-layer MLP with an SVM max-margin loss and SGD on the moon dataset. - microgpt trains a GPT-2-like transformer in pure, dependency-free Python, and he describes it as the complete algorithm with everything else being efficiency. - Graphviz tracing shows both data and gradient in each node to make the computation visible. - Unit tests verify gradient correctness against PyTorch as a reference.
Based on Andrej Karpathy's public content. Not affiliated with or endorsed by Andrej Karpathy.
Supports these rules
Understand the backward pass
ConfirmedVerify each stage before adding complexity
ConfirmedReduce to the minimal readable core
Quotes from this video
“Both are tiny, with about 100 and 50 lines of code respectively.”
“is the complete algorithm in a single file everything else is just efficiency.”
“The DAG only operates over scalar values, so e.g. we chop up each neuron into all of its individual tiny adds and multiplies. However, this is enough to build up entire deep neural nets doing binary classification,”
“which the tests use as a reference for verifying the correctness of the calculated gradients.”
“it shows both the data (left number in each node) and the gradient (right number in each node).”