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

Uploaded Python 3

File details

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

File metadata

  • Download URL: neurosketch-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 2c2fe5fbbc51a3d1867e403827885ddd773ae304fc5980b6350441396c20e3ba
MD5 ff84fd46202e0bd7632d710082c58944
BLAKE2b-256 4af0b4f3a2c9944d48e7e8a386296a576a87827792f163d8e2daee9753c7f3a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: neurosketch-0.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 54c7f92b56bf8c82e40f761bb9ea58bedc5dd2b4cdde36e54c9c8f901dc81bcb
MD5 5b4ca8d3584505dff288f49c5e5e258a
BLAKE2b-256 b1b9578c107278adae0beab3e6212062d77db5e069755c1bd6b9c58c2de26123

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