Metadata-Version: 2.4
Name: nanograd-lite
Version: 0.1.1
Summary: A minimal NumPy-based autograd engine and neural network library, built from scratch
Project-URL: Homepage, https://github.com/duaasiraj/nanograd
Project-URL: Repository, https://github.com/duaasiraj/nanograd
Project-URL: Issues, https://github.com/duaasiraj/nanograd/issues
Author: Duaa Siraj
License: MIT License
        
        Copyright (c) 2026 Duaa Siraj
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: autograd,backpropagation,deep learning,neural network,numpy
Classifier: Intended Audience :: Education
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: matplotlib>=3.5
Requires-Dist: numpy>=1.24
Provides-Extra: dev
Requires-Dist: hatch; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: scikit-learn>=1.3; extra == 'dev'
Provides-Extra: viz
Requires-Dist: graphviz>=0.20; extra == 'viz'
Description-Content-Type: text/markdown

# 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

```python
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

```python
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

```python
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)   
```

![computation graph](assets/computationgraph.svg)

---

## 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.

![mnist training](assets/mnist1.png)

![mnist training](assets/mnist2.png)

---

## Run the tests

```bash
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](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 :)