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

pip install nnetflow

Usage

from nnetflow.nn import MLP, SGD, MSELoss from nnetflow.engine import Tensor

model = MLP(nin=3, nouts=[8, 2])

example

import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from nnetflow.engine import Tensor
from nnetflow.nn import Linear, mse_loss
from nnetflow.optim import SGD


X, y = fetch_california_housing(return_X_y=True)
y = y.reshape(-1, 1)  


scaler_X = StandardScaler()
scaler_y = StandardScaler()
X = scaler_X.fit_transform(X).astype(np.float32)
y = scaler_y.fit_transform(y).astype(np.float32)


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


lr = 0.01
epochs = 100
batch_size = 64

input_dim = X.shape[1]
hidden_dim = 32
output_dim = 1


model = [
    Linear(input_dim, hidden_dim),
    Linear(hidden_dim, output_dim)
]

# Collect trainable parameters
params = []
for layer in model:
    params.append(layer.weight)
    if layer.bias:
        params.append(layer.bias)
optimizer = SGD(params, lr=lr)


def forward(x_batch):
    out = Tensor(x_batch, shape=x_batch.shape)
    out = model[0](out).relu()
    out = model[1](out)
    return out


for epoch in range(1, epochs + 1):
    perm = np.random.permutation(X_train.shape[0])
    X_train, y_train = X_train[perm], y_train[perm]
    total_loss = 0.0

    for i in range(0, X_train.shape[0], batch_size):
        xb = X_train[i:i + batch_size]
        yb = y_train[i:i + batch_size]

        preds = forward(xb)
        loss = mse_loss(preds, Tensor(yb))
        total_loss += loss.data

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    avg_loss = total_loss / (X_train.shape[0] / batch_size)
    print(f"Epoch {epoch}/{epochs} - Loss: {avg_loss.item():.4f}")


preds_test = forward(X_test).data
mse = np.mean((preds_test - y_test) ** 2)
rmse = np.sqrt(mse)
print(f"Test RMSE: {rmse:.4f}")

...

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-0.5.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

nnetflow-0.5.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nnetflow-0.5.0.tar.gz
Algorithm Hash digest
SHA256 062a7c39d020c72435382718c34a444fcd842495ceb8c0eb45452912929e08c7
MD5 ac847888ae4b4803fe71af95f1485abd
BLAKE2b-256 741bb039217e8b92deaffe6d6f38dc46dc679aaa0ba29d516f953881ce0fda87

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for nnetflow-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 350439533972861a4734ff088102d668c1506ad1230e1596eb9d6450c9fbd9fb
MD5 2802b3b3b5a0dbebab0c602f23c6c8c4
BLAKE2b-256 e399908dd8e329cc4fb3f35fd0f09a3ddeeb6bb48b438d3c242aa24056af720f

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