A minimal, pedagogical implementation of an autograd engine and neural network library in pure Python. Built to understand from first principles how frameworks like PyTorch implement reverse-mode automatic differentiation (backpropagation) under the hood.
Thanks to Andrej Karpathy for micrograd, which served as the primary reference for this project.
pip install pocketgradEach operation dynamically adds a node to the computation graph, forming a DAG. Calling .backward() traverses this graph in reverse topological order, accumulating gradients at each node via the chain rule.
The example below demonstrates this by building a simple graph, running backpropagation, and visualizing the result:
from pocketgrad.engine import Scalar
from pocketgrad.visualize import draw_graph
a = Scalar(3.0, label="a")
b = Scalar(5.5, label="b")
c = a + b; c.label = "c"
d = c / 2; d.label = "d"
e = d.relu(); e.label = "e"
e.backward()
draw_graph(e)The notebook demo_mlp.ipynb provides an end-to-end example of training a simple 2-layer feed-forward MLP with the pocketgrad.nn module on the classic two-moons dataset, achieving 100% accuracy. The plot below visualizes the decision boundary learned by the model:
pocketgrad/
├── .github/
│ └── workflows/
│ └── ci.yml # CI workflow
├── docs/
│ ├── decision_boundary.png
│ └── graph.svg
├── pocketgrad/
│ ├── __init__.py # Package exports
│ ├── engine.py # Core autograd engine
│ ├── nn.py # Neural network modules
│ └── visualize.py # Graph rendering utilities
├── test/
│ └── test_engine.py # Unit tests
├── demo_graph.ipynb # Graph demo notebook
├── demo_mlp.ipynb # MLP demo notebook
└── pyproject.toml # Build configuration
- Scalar-valued by design: Each node in the computation graph is represented as a
Scalarrather than a tensor. This makes the chain rule traceable at every step, with each gradient reduced to a single number you can verify by hand. - Batteries included for learning: Features a small neural-network library
nn.pyfor building and training models on top of the core engine, andvisualize.pyfor rendering computation graphs and tracing gradients step by step. - Readability over performance:
pocketgradavoids abstraction that reduces its educational value without offering the benefits of a production-grade framework.
As a pedagogical tool, pocketgrad does not cover:
- Vectorization or tensor abstractions
- GPU acceleration, CUDA, or low-level kernel optimizations
- PyTorch-level API breadth
If you are using uv, you can sync the dependencies and run the test suite with:
uv sync
uv run -m pytestOr from an active Python environment:
python -m pytestMIT
