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.2.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.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: neurosketch-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 120c6771966812b7e7f0a34c6fbdc5293a49623214a64eba879c6eb8593f7e46
MD5 343ab805fc1b8afec569f761a7d9a932
BLAKE2b-256 55cca8dbcb3fb8bc006519f1b9237fae5b7f39e7352bc97abb255bc46d1ae6fe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: neurosketch-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a4934ef4adfce9fcc9f46ccb3af1446677f278d2905fbf26c9bde583da8e7a8e
MD5 dd231804c1e10e1f7e2a5db6c2f04719
BLAKE2b-256 2a22d17c4b4a96da17c10a50b6dbaf40dd55a4b5f49a57f2b6026733a1276aa5

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