A lightweight autograd engine and neural network library
Project description
pocketgrad
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 (aka autograd / backpropagation) under the hood.
Thanks to Andrej Karpathy for micrograd, which served as the primary reference for this project.
Installation
pip install pocketgrad
Example with Computational Graph
In pocketgrad, each 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)
Training a Neural Network
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:
Architecture
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
├── .gitignore
├── .python-version
├── LICENSE
├── README.md
├── demo_graph.ipynb # Graph demo notebook
├── demo_mlp.ipynb # MLP demo notebook
└── pyproject.toml # Build configuration
Key Design Decisions
- Faithful to the educational goal:
pocketgradstays scalar-valued by design. This keeps the computation graph easier to reason about and makes the chain rule visible at every step. - Batteries included for learning: Features a small neural network library built on top of the core engine, along with graph visualization utilities for inspecting gradient flow.
- Clarity over complexity: It preserves the transparency that makes micrograd valuable for learning, without introducing extra complexity that does not bring PyTorch-level performance.
Not in Scope
As a pedagogical tool, the following are not planned:
-
Vectorization.
-
PyTorch-level abstractions for tensors.
-
GPU acceleration, CUDA support, or low-level kernel optimizations.
Tests
If you are using uv, you can sync the dependencies and run the test suite with:
uv sync
uv run -m pytest
Or from an active Python environment:
python -m pytest
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pocketgrad-0.1.1.tar.gz.
File metadata
- Download URL: pocketgrad-0.1.1.tar.gz
- Upload date:
- Size: 105.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61fac1cc9463fa0251b9665494d9f181fb59f8f129cbeb9aab1b285e7a0dcfb9
|
|
| MD5 |
6498c8a98db84d388486b87c6f18bc01
|
|
| BLAKE2b-256 |
a82d9935fd911a5e3a51cda0fee4c22fa0f82b3ded01cba373c9b9475067528d
|
File details
Details for the file pocketgrad-0.1.1-py3-none-any.whl.
File metadata
- Download URL: pocketgrad-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d0574200be2c95f7745b7071d8c1c89b16bf669772556f34e75e5df20c8fd49
|
|
| MD5 |
f2d327c192c6415470b975953c770eda
|
|
| BLAKE2b-256 |
a8c9ef4493e4e6e1fbd2dd74c91eb7cb0f2ccdb55594f95ecbd6c124061d1a59
|