Skip to main content

A minimal neural network framework with autodiff and NumPy

Project description

nnetflow

A minimal neural network framework with autodiff, inspired by micrograd and pytorch.

Installation

import numpy as np
from nnetflow.nn import Conv2D, MaxPool2D, Linear, MLP, CrossEntropyLoss, Module
from nnetflow.engine import Tensor
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import time

# ----------- DataLoader -----------
def numpy_dataloader(batch_size=32, train=True):
    tf = transforms.Compose([
        transforms.ToTensor(),  # (0,1)
        transforms.Lambda(lambda x: x.numpy()),
    ])
    cifar = datasets.CIFAR10(root='./data', train=train, download=True, transform=tf)
    loader = DataLoader(cifar, batch_size=batch_size, shuffle=True)
    for imgs, labels in loader:
        imgs = imgs.numpy()
        labels = np.eye(10)[labels.numpy()]  # One-hot
        yield Tensor(imgs), Tensor(labels)

# ----------- Model Definition -----------
class SimpleCNN(Module):
    def __init__(self):
        super().__init__()
        self.conv1 = Conv2D(3, 8, kernel_size=3, stride=1, padding=1)
        self.pool = MaxPool2D(kernel_size=2)
        self.conv2 = Conv2D(8, 16, kernel_size=3, stride=1, padding=1)
        self.fc1 = Linear(16 * 8 * 8, 64, activation='relu')
        self.fc2 = Linear(64, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = self.pool(x)
        B, C, H, W = x.data.shape
        x = x.reshape(B, C * H * W)
        x = self.fc1(x)
        x = self.fc2(x)
        return x

# ----------- Training Function -----------
def train(model, epochs=5, lr=0.01, batch_size=32):
    loss_fn = CrossEntropyLoss()

    for epoch in range(epochs):
        total_loss = 0.0
        num_batches = 0
        start = time.time()

        for x, y in numpy_dataloader(batch_size=batch_size, train=True):
            out = model(x)
            loss = loss_fn(out, y)

            # Backward pass
            for p in model.parameters():
                p.grad = np.zeros_like(p.data)
            loss.backward()

            # SGD step
            for p in model.parameters():
                p.data -= lr * p.grad

            total_loss += loss.data.item() if hasattr(loss.data, 'item') else loss.data
            num_batches += 1

        print(f"[Epoch {epoch+1}] Loss: {total_loss/num_batches:.4f} Time: {time.time()-start:.2f}s")

# ----------- Accuracy Evaluation -----------
def evaluate(model):
    correct = 0
    total = 0
    for x, y in numpy_dataloader(train=False):
        out = model(x)
        preds = np.argmax(out.data, axis=-1)
        labels = np.argmax(y.data, axis=-1)
        correct += np.sum(preds == labels)
        total += x.data.shape[0]
    print(f"Accuracy: {(correct / total) * 100:.2f}%")

# ----------- Run Training -----------
if __name__ == "__main__":
    model = SimpleCNN()
    train(model, epochs=5, lr=0.01, batch_size=64)
    evaluate(model)

...

See the docs/ folder for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nnetflow-1.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nnetflow-1.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file nnetflow-1.0.0.tar.gz.

File metadata

  • Download URL: nnetflow-1.0.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for nnetflow-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7a2f72b9e7a9728df928a5f2b5ac482fc4c0862433cc9a9339ac613134a16280
MD5 a4e78360a0be6855cabe4b9c9c5bdd47
BLAKE2b-256 37f1e2286e328058888cd3ca841f8c42069de4443d21d7c645accfa0235b2c6c

See more details on using hashes here.

File details

Details for the file nnetflow-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: nnetflow-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for nnetflow-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dcb524e8b2688e7d19f244ad4dd47d4e7003d9b3c497613c0b3d4e74478fb3d8
MD5 0d4995afd7e9da4388269080e2f50838
BLAKE2b-256 33ca301a6dea6e407e620cbac1cfe5e77e7a5bf71595f48f753967c2cfc7ffb4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page