A from-scratch scalar autograd engine and a small neural-net library on top of it, with tools to draw the gradients and watch a network train. My version of Karpathy's micrograd.
| file | what it does |
|---|---|
micrograd/engine.py |
the Value autograd engine |
micrograd/nn.py |
Neuron, Layer, MLP |
micrograd/viz.py |
draw the graph, animate the backward pass |
micrograd/learn.py |
fit a 1-D function, with a training dashboard |
micrograd/classify.py |
2-D classifier with an animated boundary |
micrograd/frameworks.py |
the same training in PyTorch and JAX |
micrograd/cli.py |
command-line runner |
Ops: + - * / **, tanh, exp, log, relu, abs, each with its own backward.
from micrograd import Value
a = Value(-4.0)
b = Value(2.0)
c = a * b + b**3 # build an expression
c.backward() # backprop
print(a.grad, b.grad) # 2.0 8.0Gradients match PyTorch and JAX to 1e-6 (tests/).
viz.py colors each node by its gradient and replays the backward pass one node at a time.
learn.py trains an MLP and records the fit, the per-neuron contributions, the residual, the loss, and the gradient/weight sizes.
| sin | abs | step |
|---|---|---|
![]() |
![]() |
![]() |
| moons | spiral |
|---|---|
![]() |
![]() |
python -m micrograd.cli --list
python -m micrograd.cli --task sin --gif out.gif
python -m micrograd.cli --task moons --loss svm
python -m micrograd.cli --task spiral --framework pytorchFlags: --task, --framework (micrograd/pytorch/jax), --loss, --lr, --epochs, --hidden, --alpha, --seed.
pytest # gradients vs PyTorch and JAX
python benchmark.py # writes BENCHMARKS.mdpip install -e .
pip install -r requirements.txt # matplotlib, pillow, graphviz, torch, jax, pytest
brew install graphviz # system binary for graph drawingBased on Karpathy's micrograd and his neural networks: zero to hero series.






