A tiny scalar-valued autograd engine and a PyTorch-like neural-network library built on top of it. The DAG operates over scalars only — each neuron is chopped into its individual adds and multiplies — yet this is enough to build and train entire deep neural nets.
This project bypasses high-level abstractions to construct the core mathematical building blocks of deep learning from the ground up.
- Forward Pass Graph Construction: Stacking basic algebraic operations (
+,*,**,relu,tanh) while tracking parental node linkages. - The Chain Rule in Code: Storing a local
_backwardclosure for each operation and calling it via topological sorting to guarantee correct gradient ordering. - Reverse-Mode Automatic Differentiation: Recursive backward pass across a dynamically-built DAG.
- Neural Network Modules:
Neuron→Layer→MLPcomposition with a PyTorch-likeparameters()API. - Optimization Loop: Initializing a model, computing loss (MSE or hinge), zeroing gradients, executing manual SGD step updates.
- Gradient Verification: Cross-validating custom gradient outputs against PyTorch's
autogradengine to ensure numerical precision.
| # | Notebook | Topic | Status |
|---|---|---|---|
| 1 | docs/lecture.md |
Full from-scratch build of the autograd engine + MLP training. | Complete |
| 2 | docs/demo.md |
Packaged micrograd on make_moons; decision-boundary viz. |
Complete |
| 3 | docs/exercises_micrograd.md |
Derivatives + softmax/NLL autograd exercises. | Complete (all solved) |
| 4 | docs/practice_book1.md |
From-memory re-implementation scratchpad. | In-progress |
| 5 | docs/trace_graph.md |
Graphviz trace/draw_dot utilities. | Complete (unexecuted) |
The codebase runs on Python 3.x and relies on a focused stack:
torch— Used strictly as a ground-truth baseline to verify custom gradient calculations.graphviz— Used to generate and render visual representations of the computation graphs (Python package + system binary).numpy— For vectorized evaluation and data structures.matplotlib— For plotting training loss curves and decision boundaries.
cd micrograd
pip install torch numpy graphviz matplotlib
python -m pytest test/test_engine.py -vcd micrograd
jupyter notebook lecture.ipynb
# or headless:
jupyter nbconvert --to notebook --execute lecture.ipynb --output lecture.ipynbfrom micrograd.engine import Value
from micrograd.nn import MLP
model = MLP(2, [16, 16, 1])
# ...forward, loss, backward, step...Run from the micrograd/ directory or pip install -e . to make the package importable globally.