Skip to main content

A mini supervised learning framework

Project description

NeuroSketch

A lightweight educational neural network framework built from scratch with NumPy.

Python NumPy License

NeuroSketch is an educational deep learning framework that implements neural networks from first principles. Every stage of training—from forward propagation to gradient computation and parameter updates—is written manually using NumPy.

Unlike production frameworks that rely on automatic differentiation, NeuroSketch makes every step explicit. Layers compute their own gradients, loss functions generate the initial gradient, and optimizers traverse the network during backpropagation. The goal is to understand how neural networks learn rather than simply using them.


Features

Layers

  • Fully Connected (Linear)
  • Sequential model container

Activation Functions

  • ReLU
  • LeakyReLU
  • Sigmoid
  • Tanh
  • Softmax
  • Swish
  • HeavySide

Loss Functions

  • MSELoss
  • MAELoss
  • BinaryCrossentropyLoss
  • SparseCategoricalCrossentropyLoss

Optimizers

  • SGD
  • MOMENTUM
  • ADAM

Utilities

  • Mini-batch DataLoader
  • Dataset shuffling
  • Optional dropping of incomplete batches

Weight Initialization

  • He
  • Xavier
  • Zero

Installation

pip install NeuroSketch

Quick Example

import numpy as np

from NeuroSketch.engine.nn import Sequential, Linear
from NeuroSketch.engine.act import ReLU, Softmax
from NeuroSketch.losses import SparseCategoricalCrossentropyLoss
from NeuroSketch.optims import ADAM
from NeuroSketch.utils import DataLoader

x = np.random.randn(500, 20)
y = np.random.randint(5, size=500)

loader = DataLoader(x, y, batch_size=32, shuffle=True)

model = Sequential(
    Linear(20, 64, init_type="he"),
    ReLU(),
    Linear(64, 5, init_type="xavier"),
    Softmax()
)

criterion = SparseCategoricalCrossentropyLoss(model.layers[-1])
optimizer = ADAM(model, lr=1e-3)

for epoch in range(20):
    total_loss = 0

    for xb, yb in loader:
        pred = model(xb)
        loss = criterion(pred, yb)

        criterion.backward()
        optimizer.step()

        total_loss += loss

    print(f"Epoch {epoch+1}: {total_loss:.4f}")

Model Summary

print(model.summary())

Project Structure

src/
└── NeuroSketch/
    ├── engine/
    │   ├── _module.py
    │   ├── act.py
    │   └── nn.py
    ├── losses.py
    ├── optims.py
    ├── utils.py
    └── LICENSE

README.md

Framework Design

Forward Pass

Input
 │
Linear
 │
Activation
 │
Linear
 │
Activation
 │
Prediction
 │
Loss

Backward Pass

Loss
 │
criterion.backward()
 │
optimizer.step()
 │
Activation.backward()
 │
Linear.backward()
 │
Activation.backward()
 │
Linear.backward()
 │
Parameter Update

NeuroSketch follows a modular object-oriented design.

  • Layers perform forward and backward propagation.
  • Activations compute their own derivatives.
  • Losses compute the initial gradient.
  • Optimizers drive the complete backpropagation process and update parameters.

No automatic differentiation or computational graph is used.


Training Flow

prediction = model(x_batch)
loss = criterion(prediction, y_batch)

criterion.backward()
optimizer.step()

Internally:

  1. Forward propagation through every layer.
  2. Loss computation.
  3. Initial gradient generation.
  4. Optimizer walks backward through the model by calling each layer's backward().
  5. Parameters are updated.

Components

Linear Layer

Caches:

  • Input
  • Weights
  • Biases

Computes:

  • dW
  • dB
  • Gradient for the previous layer

using vectorized NumPy operations.

Activation Functions

Each activation caches its forward output and computes its derivative during backpropagation.

Softmax implements the Jacobian-vector product for efficient gradient propagation.

Loss Functions

Every loss object stores predictions and labels during the forward pass.

Calling

criterion.backward()

computes the gradient of the loss with respect to the model output and passes it to the final activation layer.

Optimizers

Optimizers not only update parameters but also perform the complete backward traversal of the network.

Implemented:

  • SGD
  • MOMENTUM
  • ADAM

DataLoader

Supports:

  • Mini-batching
  • Full-batch training
  • Dataset shuffling
  • Dropping incomplete batches

Design Philosophy

NeuroSketch intentionally avoids hidden abstractions.

Instead of relying on automatic differentiation, every layer implements its own forward and backward computations. This exposes every mathematical operation involved in neural network training, making the framework suitable for education and experimentation.


Comparison

Feature NeuroSketch PyTorch
Built with NumPy
Manual gradient computation
Automatic differentiation
Explicit backpropagation
Educational focus ⚠️
Production ready

Roadmap

Completed

  • ✅ Sequential models
  • ✅ Linear layers
  • ✅ Weight initialization
  • ✅ Multiple activations
  • ✅ Multiple loss functions
  • ✅ SGD
  • ✅ MOMENTUM
  • ✅ ADAM
  • ✅ DataLoader

Planned

  • Dropout
  • Batch Normalization
  • Learning-rate schedulers
  • CNN layers
  • Pooling layers
  • RNN / LSTM
  • Model save/load
  • Documentation website

Requirements

  • Python 3.7+
  • NumPy

License

MIT License.


Why NeuroSketch?

NeuroSketch was created with one goal:

Understand neural networks by building them—not by treating them as black boxes.

Every gradient, parameter update, and layer operation is implemented manually using NumPy, making the framework a practical resource for students, educators, and anyone interested in learning deep learning from first principles.

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

neurosketch-0.1.4.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

neurosketch-0.1.4-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file neurosketch-0.1.4.tar.gz.

File metadata

  • Download URL: neurosketch-0.1.4.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for neurosketch-0.1.4.tar.gz
Algorithm Hash digest
SHA256 48639f9dfa10466e8dcb1e5cca50fce22ff0f8cfe3d66a9766387f04d253f2f2
MD5 7e59d36a79db714d0d23d401d6f74575
BLAKE2b-256 f41112332376910ff9396a1af3fc77666b104e4aeb9f006d7bcefd34b3ce0800

See more details on using hashes here.

File details

Details for the file neurosketch-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: neurosketch-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for neurosketch-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 41c8fd4f62b22b7d2ee0935dfdadbfd5068b96f3d13c13e89882a8cf1658fc41
MD5 32660fe8248c39b629d89fa628709a51
BLAKE2b-256 933990647715a77a57652dac29c173a37ea5ed31fd533fe51c6510b46e35438c

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