nanograd
A minimal autodiffentiation engine and neural network library built from scratch.Numpy only, no PyTorch.
Supports forward and backward passes through a dynamic computation graph, a neural network API modelled after PyTorch, and computation graph visualization. Trained on MNIST.
pip install nanograd
Quick start
from nanograd import Tensor
from nanograd.nn import Sequential, Linear, ReLU, CrossEntropyLoss, SGD
# define a network
model = Sequential(Linear(784, 128),ReLU(),Linear(128, 64),ReLU(),Linear(64, 10))
loss_fn = CrossEntropyLoss()
optimizer = SGD(model.parameters(), lr=0.01)
# training step
x = Tensor(X_batch)
out = model(x)
loss = loss_fn(out, y_batch)
loss.backward()
optimizer.step()
optimizer.zero_grad()
Autograd
import numpy as np
from nanograd import Tensor
x = Tensor(np.array([[1.0, 2.0], [3.0, 4.0]]))
y = Tensor(np.array([[0.5], [1.5]]))
z = (x @ y).sum()
z.backward()
print(x.grad) # dz/dx
print(y.grad) # dz/dy
Computation graph visualization
from nanograd.viz import draw_dot
x = Tensor(np.array([2.0]))
y = Tensor(np.array([3.0]))
z = (x * y + x).sum()
z.backward()
dot = draw_dot(z)
dot.render("graph", view=True)
What's implemented
Tensor ops — +, -, *, /, **, @ (matmul), sum, mean, reshape, transpose, exp, log
Activations — relu, tanh, sigmoid, softmax
Layers — Linear (Xavier init), ReLU
Loss — CrossEntropyLoss, MSELoss
Optimizer — SGD
Viz — draw_dot, trace (graphviz-based, local use)
MNIST results
Trained a 784 → 128 → 64 → 10 network with ReLU activations, SGD lr=0.01, batch size 32, 10 epochs.
Run the tests
git clone https://github.com/duaasiraj/nanograd
cd nanograd
pip install -e ".[dev]"
pytest tests/ -v
Project structure
nanograd/
├── nanograd/
│ ├── engine/
│ │ ├── tensor.py # Tensor class, all ops + backward rules
│ │ └── utils.py # topo sort, unbroadcast, gradient_check
│ ├── nn/
│ │ ├── layers.py # Linear, ReLU
│ │ ├── loss.py # CrossEntropyLoss, MSELoss
│ │ ├── optim.py # SGD
│ │ ├── sequential.py # Sequential container
│ │ ├── modules.py # base Module class
│ │ ├── accuracy.py # multiclass accuracy
│ │ └── metrics.py # precision, recall, F1
│ └── viz/
│ └── graph_viz.py # draw_dot, trace
├── tests/
│ └── test_gradients.py # 17 numerical gradient checks
├── demos/
│ ├── mnist_demo.py
│ └── xor_demo.py
└── pyproject.toml
License
MIT — see LICENSE
Disclaimer: This project was developed as a learning project and may contain mistakes, inefficiencies, or incomplete implementations. If you spot an issue or have an improvement, feel free to open an issue or submit a pull request. Check out contribution.md for further details :)
Metadata
Release files for nanograd-lite 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nanograd_lite-0.1.1.tar.gz | 173.4 kB | Without provenance | Details for nanograd_lite-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nanograd_lite-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for nanograd_lite-0.1.1-py3-none-any.whl |
Total release size: 184.7 kB
Release files / nanograd_lite-0.1.1.tar.gz
| Download URL | nanograd_lite-0.1.1.tar.gz |
|---|---|
| Size | 173.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1f6c8fed1994fd50206cc313f9776f61c926c39594a86ce17c0b362544f03354
|
|
BLAKE2b-256 checksum How to use checksums |
689bb50a253bff1b9e4ad64925f80611affa8e9832a7bba933424fee090449ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Release files / nanograd_lite-0.1.1-py3-none-any.whl
| Download URL | nanograd_lite-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
de572ea239357254dc203784bf8b1689da791c9a7ede9e1b44b53b8d3303fc69
|
|
BLAKE2b-256 checksum How to use checksums |
0e22e409d38f20f387f8800f9a1254c0bfe542a2530f8be31f7b0b88c6433868
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Security for nanograd-lite 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.