nnetflow — lightweight neural networks for learning
nnetflow is a small, opinionated deep learning library implemented with NumPy for education and experimentation. It focuses on readability and a small, correct autodiff core so you can learn how deep learning frameworks work under the hood.
Key design goals:
- Minimal API surface: easy to read and reason about
- Correct reverse-mode autodiff (dynamic graphs)
- Small, focused feature set for learning (Linear layer, losses, optimizers)
- Well-tested: unit tests exercise core pieces (Tensor, Linear, losses, optimizers)
This repository represents the v2.0.5 release — featuring GPU support via CuPy, Multi-Head Attention, and comprehensive device abstraction.
Highlights
- Tensor: NumPy-backed tensor with reverse-mode autodiff and many activations
- Linear layer: fully-connected layer with sensible initialization
- Losses: MSE, RMSE, Cross-Entropy, Binary Cross-Entropy (logits and probs)
- Optimizers: SGD (+momentum), Adam
- Examples: runnable scripts under
examples/ - CI: GitHub Actions runs full test matrix on push and PRs
- Local checks:
pre-commitconfigured to run tests before pushing changes
Install
Install from PyPI (when published):
pip install nnetflow
Or install editable from source (recommended for contributors):
git clone https://github.com/lewisnjue/nnetflow.git
cd nnetflow
pip install -e .
pip install -r requirements.dev
pre-commit install --install-hooks
Note: pre-commit install sets up git hooks locally. This repo includes a pre-push hook that runs pytest to help prevent regressions before pushing.
Examples
Examples are included in the examples/ folder and are runnable directly:
python examples/simple_regression.py
python examples/binary_classification.py
python examples/gpt2.py
They demonstrate model definition, training loops, loss computation and parameter updates using the library primitives.
Quick usage
import numpy as np
from nnetflow import Tensor, Linear, mse_loss, Adam
X = np.random.randn(128, 3)
y = np.random.randn(128, 1)
layer = Linear(3, 1)
opt = Adam(layer.parameters(), lr=1e-2)
X_t = Tensor(X, requires_grad=False)
y_t = Tensor(y, requires_grad=False)
for epoch in range(100):
preds = layer(X_t)
loss = mse_loss(preds, y_t)
opt.zero_grad()
loss.backward()
opt.step()
if (epoch + 1) % 10 == 0:
print(f"epoch {epoch+1}: loss={loss.item():.4f}")
You can also import components individually:
from nnetflow.engine import Tensor
from nnetflow.layers import Linear
from nnetflow.losses import mse_loss, cross_entropy_loss
from nnetflow.optim import SGD, Adam
Testing
Run unit tests locally with:
pytest tests/ -q
CI runs tests automatically on push and pull requests.
Pre-commit
This project uses pre-commit to run basic checks and to run pytest before pushing. After cloning run:
pip install pre-commit
pre-commit install --install-hooks
To run the hooks locally (including the pytest hook configured for pre-push):
pre-commit run --all-files
API Reference
Tensor Operations
The Tensor class is the core of nnetflow, providing automatic differentiation:
from nnetflow import Tensor
# Create tensors
x = Tensor([1.0, 2.0, 3.0], requires_grad=True)
y = Tensor([4.0, 5.0, 6.0], requires_grad=True)
# Operations
z = x + y # Addition
z = x * y # Multiplication
z = x / y # Division
z = x @ y # Matrix multiplication (if compatible shapes)
z = x.sum() # Sum reduction
z = x.mean() # Mean reduction
# Activations
z = x.relu() # ReLU
z = x.sigmoid() # Sigmoid
z = x.tanh() # Tanh
z = x.softmax() # Softmax
# Backward pass
z.backward() # Compute gradients
Layers
from nnetflow import Linear
# Linear layer
layer = Linear(in_features=10, out_features=5, bias=True)
output = layer(input_tensor)
params = layer.parameters() # Get trainable parameters
Loss Functions
from nnetflow import (
mse_loss,
rmse_loss,
cross_entropy_loss,
binary_cross_entropy_loss,
logits_binary_cross_entropy_loss
)
# Regression losses
loss = mse_loss(predictions, targets)
loss = rmse_loss(predictions, targets)
# Classification losses
loss = cross_entropy_loss(logits, one_hot_targets)
loss = binary_cross_entropy_loss(probabilities, targets)
loss = logits_binary_cross_entropy_loss(logits, targets)
Optimizers
from nnetflow import SGD, Adam
# SGD with optional momentum
optimizer = SGD(params, lr=0.01, momentum=0.9)
# Adam optimizer
optimizer = Adam(params, lr=0.001, beta1=0.9, beta2=0.999)
# Training step
optimizer.zero_grad() # Clear gradients
loss.backward() # Compute gradients
optimizer.step() # Update parameters
Project structure
nnetflow/ # package source
engine.py # Tensor & autodiff engine
layers.py # Linear, BatchNorm1d, LayerNorm, etc.
losses.py # loss functions
optim.py # optimizers
examples/ # runnable examples
tests/ # unit tests
Contributing
Contributions are welcome. Please follow these steps:
- Fork the repository and create a feature branch
- Write tests for your change
- Run
pytestandpre-commitlocally - Open a pull request with a clear description
See CONTRIBUTING.md for more details.
Changelog
See CHANGELOG.md for details on releases. The repository is now at v2.0.4.
License
MIT — see LICENSE.
Maintained by Lewis Njue — aimed at learners and educators building intuition about how neural networks work.
Metadata
Release files for nnetflow 2.0.5
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 |
|---|---|---|---|---|
| nnetflow-2.0.5.tar.gz | 51.7 kB | Without provenance | Details for nnetflow-2.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nnetflow-2.0.5-py3-none-any.whl | Python 3 | none | any | Details for nnetflow-2.0.5-py3-none-any.whl |
Total release size: 101.9 kB
Release files / nnetflow-2.0.5.tar.gz
| Download URL | nnetflow-2.0.5.tar.gz |
|---|---|
| Size | 51.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d204a7d5067dcc71fc7d3c7e01efbc3b834fbb2ff9f776092c0c8ce78c7da70
|
|
BLAKE2b-256 checksum How to use checksums |
146f9c86336dec078b04ce37d1ff995ce748037cf7b9b83a90af409a92654f10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.19
|
Release files / nnetflow-2.0.5-py3-none-any.whl
| Download URL | nnetflow-2.0.5-py3-none-any.whl |
|---|---|
| Size | 50.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6929a37ff88dae606a47c405f2ccf01531f31c49e57d9d95fa87038ad5b9129e
|
|
BLAKE2b-256 checksum How to use checksums |
179b82a8617d89b2109a8e0cdff98bf253fdc8ad4f24739b33c1e5fc4e1f67d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.19
|
Security for nnetflow 2.0.5
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.