backprop-lib
A tiny scalar-valued autograd engine and a neural net library on top of it with a PyTorch-like API — in the spirit of Karpathy's micrograd, but with a slightly different approach (see Differences).
Implements backpropagation (reverse-mode autodiff) over a dynamically built DAG, plus Neuron / Layer / MLP on top. Extracted from manual_backprop.ipynb into an installable package:
backprop/
__init__.py # package entry: Value + nn
engine.py # Value autograd engine (+ - * / ** tanh exp relu)
nn.py # Module, Neuron, Layer, MLP (tanh-based)
viz.py # trace() + draw_dot() graphviz helpers
test/
test_engine.py
manual_backprop.ipynb # derivation / demo notebook
Installation
pip install backprop-lib
From source:
pip install -e .
pytest
Example usage
from backprop.engine import Value
from backprop import nn
a = Value(2.0, label='a')
b = Value(-3.0, label='b')
c = Value(10.0, label='c')
e = a * b; e.label = 'e'
d = e + c; d.label = 'd'
f = Value(-2.0, label='f')
L = d * f; L.label = 'L'
L.backward()
print(a.grad, b.grad) # 6.0 -4.0
Training a small MLP (same as the notebook):
from backprop.nn import MLP
xs = [[2.0, 3.0, -1.0], [3.0, -1.0, 0.5], [0.5, 1.0, 1.0], [1.0, 1.0, -1.0]]
ys = [1.0, -1.0, -1.0, 1.0]
model = MLP(3, [4, 4, 1])
for k in range(20):
ypred = [model(x) for x in xs]
loss = sum((y - ygt) ** 2 for y, ygt in zip(ypred, ys))
model.zero_grad()
loss.backward()
for p in model.parameters():
p.data += -0.1 * p.grad
print(k, loss.data)
Graphviz visualisation (needs the Graphviz system binary — the graphviz pip package installs automatically with the library):
from backprop.viz import draw_dot
draw_dot(L)
Note: on Windows, install the binary from graphviz.org with Add to PATH
checked (or winget install graphviz), otherwise add C:\Program Files\Graphviz\bin
to PATH manually, restart the terminal, and verify with dot -V.
Differences from micrograd
Value(..., label='...')— every node carries a debug label used bydraw_dot.- Activations:
tanh+expare core (matching the notebook's manual neuron derivation);reluis also included for convenience. Micrograd'snndefaults to ReLU; thisnnusestanheverywhere and randomises the bias. Value.zero_grad()clears the whole subgraph (iterative, sharing-safe);Module.zero_grad()clears parameters.__pow__unwraps aValueexponent to its.dataso notebook code likeother**Value(-1.0)keeps working; plain int/float exponents are preferred.- Explicit
__sub__backward (+1/-1) instead of going through__neg__.
Running tests
pytest -v
License
MIT
Metadata
Release files for backprop-lib 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| backprop_lib-0.1.0.tar.gz | 139.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| backprop_lib-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 146.1 kB
Release files / backprop_lib-0.1.0.tar.gz
| Download URL | backprop_lib-0.1.0.tar.gz |
|---|---|
| Size | 139.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd767811ef7ce13e432c9b23d6a2dcb757a6c27911a55645d6b2781523b098d4
|
|
BLAKE2b-256 checksum How to use checksums |
673df2a267b3f293e56c52d5e79176ba514aa64e00117a37261b7aadeab2d570
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|
Release files / backprop_lib-0.1.0-py3-none-any.whl
| Download URL | backprop_lib-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a3774a0f994a716f752eea65eb0ada7a2ef2f80e55a3d17bac5426c5d0eccae
|
|
BLAKE2b-256 checksum How to use checksums |
4fd53dda6afa19ddc9cbacd9f5e086bf08e863318e1bc233027266225e244c28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|