A NumPy-only autograd and neural network library with a PyTorch-like API
Project description
NumpyGrad
A small autograd and neural network library with a PyTorch-like API, built on NumPy only. No GPU, no C++ extensions—just Python and NumPy. Useful for learning how backprop and frameworks like PyTorch work, or for lightweight experiments where a full framework is overkill.
Features
Autograd
- NumPy-only — Single dependency: NumPy.
- Define-by-run autograd — Builds a computation graph as ops are invoked (i.e., Torch eager).
- Familiar array API —
arraywithshape,ndim,dtypeetc. - Familiar array creation -
ones,zeros,arange,randn, etc. - Familiar torch autograd API -
.backward(),requires_grad,.grad,no_grad(), etc. - Broadcasting & batched ops — Linear algebra, reductions, transforms, and elementwise ops support batched and broadcasted shapes.
- Familiar special methods -
x @ y,mask = x > 0, etc.
Neural Nets
- Modules -
Linear,Conv2d,Embedding,LayerNorm,MultiHeadAttention,Dropout,Sequential, etc. - Optimizers -
AdamW,SGD. - Activations -
ReLU,GELU,Tanh,Sigmoid,SoftPlus. - Losses -
CrossEntropy(supports N-D logits),MSE. - Parameter init -
nn.init.kaiming_uniform_,xavier_normal_, etc.
Installation
From the project root:
pip install -e .
Requires Python ≥3.12 and NumPy ≥2.4.2.
Optional dependencies (e.g. for tests and examples):
pip install -e ".[tests]" # pytest, hypothesis, torch (for gradient checks)
pip install -e ".[examples]" # matplotlib for plotting
Quick start
import numpygrad as np # live on the edge!
import numpygrad.nn as nn
# Arrays and gradients
x = np.randn((3, 4), requires_grad=True)
y = (x ** 2).sum()
y.backward()
print(x.grad) # gradients of sum(x²) w.r.t. x
# Small MLP
net = nn.MLP(input_dim=1, hidden_sizes=[8, 8], output_dim=1)
optimizer = np.optim.SGD(net.parameters(), step_size=1e-1)
x = np.randn(32, 1)
targets = np.randn(32, 1)
out = net(x)
loss = ((out - targets) ** 2).mean()
loss.backward()
optimizer.step()
Examples
The examples/ directory includes self-contained demos. Run any of them with:
python -m examples.<name>.main # use --help for CLI options
If matplotlib is not installed, training and evaluation run as usual but no figures are generated. Install the examples extra to enable plotting:
pip install -e ".[examples]" # adds matplotlib
Scalar Regression
MLP fit to a noisy 1D function. Demonstrates MSE loss, AdamW, and train/test splits.
python -m examples.regression_1d.main
2D Classification
Classifier on a pinwheel (interleaved spirals) dataset. Demonstrates cross-entropy loss and decision-boundary visualisation.
python -m examples.classification_2d.main
MNIST
Conv-net classifier on handwritten digits. Data is downloaded automatically.
python -m examples.mnist.main
GPT-2 Character Language Model
A GPT-2-style transformer trained character-by-character on Shakespeare's complete works.
Uses LayerNorm, Embedding, Dropout, GELU, causal masking via triu + masked_fill, and ND cross-entropy — all in pure NumPy.
python -m examples.gpt2.main # ~45 min on CPU to generate proto-English
Project layout
src/numpygrad/
├── core/ # Array, autograd (Function, backward), dispatch, device
├── ops/ # Operators: elementwise, linalg, transforms, reductions, etc.
├── nn/ # Linear, MLP (and other modules)
├── optim/ # SGD and optimizer base
├── utils/ # Logging, I/O, visualizations
└── configuration.py
Tests live in tests/ and use Hypothesis plus PyTorch to check gradients against a reference.
Development
Run tests:
pytest
# or with optional deps
pip install -e ".[tests]" && pytest
License
MIT License. See LICENSE for 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 numpygrad-0.1.0.tar.gz.
File metadata
- Download URL: numpygrad-0.1.0.tar.gz
- Upload date:
- Size: 39.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a62270a44c06eb83577dfdaa50866280cb76362cfa379332c32eff806e0316d7
|
|
| MD5 |
cfc5150d764c554a50ba561853aa1e3c
|
|
| BLAKE2b-256 |
3f1d243d07b4c0fe0e044388f604604ad093a13d3f51b79c7df084326f0a9310
|
Provenance
The following attestation bundles were made for numpygrad-0.1.0.tar.gz:
Publisher:
publish.yml on njkrichardson/numpygrad
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpygrad-0.1.0.tar.gz -
Subject digest:
a62270a44c06eb83577dfdaa50866280cb76362cfa379332c32eff806e0316d7 - Sigstore transparency entry: 1046834450
- Sigstore integration time:
-
Permalink:
njkrichardson/numpygrad@2ba4afd90d592b7a19d747265bc51754ea917a31 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/njkrichardson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2ba4afd90d592b7a19d747265bc51754ea917a31 -
Trigger Event:
push
-
Statement type:
File details
Details for the file numpygrad-0.1.0-py3-none-any.whl.
File metadata
- Download URL: numpygrad-0.1.0-py3-none-any.whl
- Upload date:
- Size: 44.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a1bcb07ea2f59e28bf7dd5282cc1e369f1d93507f258b241bc864091249d13e
|
|
| MD5 |
8874f317752ebd3b279d308c66c86107
|
|
| BLAKE2b-256 |
0802b8978e9c5102c0cd06222b23adb09826f4f0410249fb988cc0edf20ffd2e
|
Provenance
The following attestation bundles were made for numpygrad-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on njkrichardson/numpygrad
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
numpygrad-0.1.0-py3-none-any.whl -
Subject digest:
1a1bcb07ea2f59e28bf7dd5282cc1e369f1d93507f258b241bc864091249d13e - Sigstore transparency entry: 1046834455
- Sigstore integration time:
-
Permalink:
njkrichardson/numpygrad@2ba4afd90d592b7a19d747265bc51754ea917a31 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/njkrichardson
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2ba4afd90d592b7a19d747265bc51754ea917a31 -
Trigger Event:
push
-
Statement type: