A minimal NumPy-based autograd engine and neural network library, built from scratch
Project description
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 :)
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 nanograd_lite-0.1.1.tar.gz.
File metadata
- Download URL: nanograd_lite-0.1.1.tar.gz
- Upload date:
- Size: 173.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1f6c8fed1994fd50206cc313f9776f61c926c39594a86ce17c0b362544f03354
|
|
| MD5 |
41d9dbfbe3d0995cd6545741f9e75120
|
|
| BLAKE2b-256 |
689bb50a253bff1b9e4ad64925f80611affa8e9832a7bba933424fee090449ea
|
File details
Details for the file nanograd_lite-0.1.1-py3-none-any.whl.
File metadata
- Download URL: nanograd_lite-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de572ea239357254dc203784bf8b1689da791c9a7ede9e1b44b53b8d3303fc69
|
|
| MD5 |
dcc21333c2af6b74c3cf527fd900ba2e
|
|
| BLAKE2b-256 |
0e22e409d38f20f387f8800f9a1254c0bfe542a2530f8be31f7b0b88c6433868
|