A neural network framework built from scratch using NumPy.
Project description
scratch3dnn
A lightweight deep learning framework built from scratch using only NumPy.
Designed for learning how neural networks really work — no PyTorch, no TensorFlow, just math and Python.
Features
- Modular architecture — every component extends a common
Modulebase class - Dense (fully-connected) layers with small-weight initialisation
- Activation functions — ReLU and Sigmoid
- Loss functions — Mean Squared Error (MSE)
- Optimiser — Stochastic Gradient Descent (SGD)
- Sequential network (
NeuralNet) that chains layers and handles forward + backward passes automatically
Installation
pip install scratch3dnn
Requires Python >= 3.9 and NumPy >= 1.24.
Quick Start
import numpy as np
from scratch3dnn import NeuralNet, Layer, Relu, Sigmoid, MSELoss, SGDOptimizer
# Build a simple 2-layer network
model = NeuralNet(
Layer(4, 8), # 4 inputs -> 8 hidden units
Relu(),
Layer(8, 1), # 8 hidden -> 1 output
Sigmoid(),
)
loss_fn = MSELoss()
optimizer = SGDOptimizer(model.get_params(), learning_rate=0.01)
# Dummy dataset
X = np.random.randn(32, 4) # 32 samples, 4 features
y = np.random.randint(0, 2, (32, 1)).astype(float)
# Training loop
for epoch in range(100):
# Forward pass
predictions = model.forward(X)
loss = loss_fn.forward(predictions, y)
# Backward pass
grad = loss_fn.backward()
model.backward(grad)
# Update weights
optimizer.step()
optimizer.zero_grad()
if (epoch + 1) % 10 == 0:
print(f"Epoch {epoch + 1:3d} | Loss: {loss:.6f}")
API Reference
Module (base class)
All components inherit from Module.
| Method | Description |
|---|---|
forward(input_data) |
Compute the forward pass |
backward(gradient) |
Compute the backward pass and return the upstream gradient |
get_params() |
Return a list of (param, grad) tuples (default: []) |
Layer(input_size, output_size)
A fully-connected linear layer: y = x W + b.
- Weights initialised with
N(0, 0.001), biases initialised to zero. - Accumulates gradients in
w_gradandb_gradduring backward pass.
Activations
| Class | Formula |
|---|---|
Relu() |
max(0, x) |
Sigmoid() |
1 / (1 + exp(-x)) |
MSELoss()
Mean Squared Error loss: L = 0.5 * mean((y_hat - y)^2)
loss = loss_fn.forward(predictions, targets) # scalar
grad = loss_fn.backward() # gradient w.r.t. predictions
NeuralNet(*layers)
Sequential container — passes data through each layer in order during forward,
and in reverse during backward.
model = NeuralNet(Layer(4, 8), Relu(), Layer(8, 1))
out = model.forward(X)
model.backward(grad)
params = model.get_params() # flat list of (param, grad) pairs
SGDOptimizer(parameters, learning_rate=0.001)
Vanilla Stochastic Gradient Descent.
optimizer = SGDOptimizer(model.get_params(), learning_rate=0.01)
optimizer.step() # param -= lr * grad
optimizer.zero_grad() # reset all gradients to 0
Project Structure
src/scratch3dnn/
├── __init__.py # Public API exports
├── module.py # Abstract Module base class
├── layers.py # Dense Layer
├── activations.py # Relu, Sigmoid
├── losses.py # MSELoss
├── network.py # NeuralNet (sequential container)
└── optimizers.py # SGDOptimizer
License
MIT (c) Tridibesh Sarkar
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scratch3dnn-0.1.0.tar.gz.
File metadata
- Download URL: scratch3dnn-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
007e3654b6cc130fbd7e776b765013cb5a2038c45748654dbeb2a9edaca7f0ac
|
|
| MD5 |
5e1126f8e535a39d32c6937f825ae462
|
|
| BLAKE2b-256 |
38917f67f621ca680cf797d79b51c1a08ab65d1453695668754903aaef1ff31c
|
File details
Details for the file scratch3dnn-0.1.0-py3-none-any.whl.
File metadata
- Download URL: scratch3dnn-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d9e558dc3bd3076e31b764ebce514c61d5ec47a60b9f56d4d5bb6b2f5582aacf
|
|
| MD5 |
5fbd2cbefbc2f1c420410b576af8ece1
|
|
| BLAKE2b-256 |
ffbdd0fde2841240a2b2a1c210a61639962c4a149725be98700921ed40af0990
|