Skip to main content

pocketgrad

CI

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)

Computational Graph

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:

Decision Boundary

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: pocketgrad stays 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

Metadata

Release files for pocketgrad 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pocketgrad 0.1.1
File Size Uploaded
pocketgrad-0.1.1.tar.gz 105.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pocketgrad 0.1.1
File Interpreter ABI Platform
pocketgrad-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 112.9 kB

Release files / pocketgrad-0.1.1.tar.gz

Download URL pocketgrad-0.1.1.tar.gz
Size 105.2 kB
Tags Source
SHA-256 checksum
How to use checksums
61fac1cc9463fa0251b9665494d9f181fb59f8f129cbeb9aab1b285e7a0dcfb9
BLAKE2b-256 checksum
How to use checksums
a82d9935fd911a5e3a51cda0fee4c22fa0f82b3ded01cba373c9b9475067528d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.5

Release files / pocketgrad-0.1.1-py3-none-any.whl

Download URL pocketgrad-0.1.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5d0574200be2c95f7745b7071d8c1c89b16bf669772556f34e75e5df20c8fd49
BLAKE2b-256 checksum
How to use checksums
a8c9ef4493e4e6e1fbd2dd74c91eb7cb0f2ccdb55594f95ecbd6c124061d1a59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page