An educational deep learning library built from scratch in Python โ learn neural networks by reading the code.
Project description
๐ง PyNNet
An Educational Deep Learning Library โ Built from Scratch
PyNNet is a beginner-friendly neural network library that helps you learn and understand deep learning from the ground up. Built entirely in Python and NumPy, every line of code is designed to be read, understood, and learned from.
๐ก Not a replacement for PyTorch or TensorFlow โ PyNNet is purpose-built for education. Read the source code. Understand backpropagation. Build intuition.
โจ What's New in v1.0.1
- ๐๏ธ CNN Support โ
conv2d,maxpool2d, andflattenlayers - ๐ RNN Support โ Full
lstmlayer with BPTT - ๐๏ธ Regularization โ
dropoutandbatchnormlayers - ๐ง 4 Optimizers โ SGD, Momentum, RMSprop, Adam
- ๐ก๏ธ Numerical Stability โ Stable sigmoid, softplus, and loss functions
- ๐
model.summary()โ View architecture and parameter counts - โ 36 unit tests โ Full test coverage with pytest
๐ฆ Installation
pip install pynnet
Requirements: Python 3.8+ and NumPy (installed automatically).
๐ Quick Start
Build an MLP (Multi-Layer Perceptron)
import numpy as np
from pynnet.network import sequential
from pynnet.layers import dense
from pynnet.activation import activation, relu, relu_derivative, sigmoid, sigmoid_derivative
from pynnet.optimizer import Adam
from pynnet.loss import mse, mse_derivative
# XOR dataset
X = np.array([[[0,0]], [[0,1]], [[1,0]], [[1,1]]], dtype=float)
y = np.array([[[0]], [[1]], [[1]], [[0]]], dtype=float)
# Build model
model = sequential()
model.add(dense(input_size=2, output_size=8, weight_init='he'))
model.add(activation(relu, relu_derivative))
model.add(dense(input_size=8, output_size=1, weight_init='xavier'))
model.add(activation(sigmoid, sigmoid_derivative))
# Compile & train
model.compile(loss=mse, loss_derivative=mse_derivative, optimizer=Adam(learning_rate=0.01))
model.fit(X, y, epochs=1000, verbose=True, print_every=200)
# Predict
print(model.predict(X))
Build a CNN
from pynnet.network import sequential
from pynnet.layers import conv2d, maxpool2d, flatten, dense
from pynnet.activation import activation, relu, relu_derivative, softmax
from pynnet.optimizer import Adam
from pynnet.loss import categorical_cross_entropy, cce_derivative_with_softmax
model = sequential()
# Convolutional block
model.add(conv2d(num_filters=8, kernel_size=3, input_channels=1)) # (1,28,28) โ (8,26,26)
model.add(activation(relu, relu_derivative))
model.add(maxpool2d(pool_size=2)) # (8,26,26) โ (8,13,13)
# Classifier
model.add(flatten()) # (8,13,13) โ (1,1352)
model.add(dense(input_size=1352, output_size=10, weight_init='he'))
model.add(softmax())
model.compile(
loss=categorical_cross_entropy,
loss_derivative=cce_derivative_with_softmax,
optimizer=Adam(learning_rate=0.001)
)
Build with LSTM
from pynnet.layers import lstm, dense
from pynnet.optimizer import Adam
rnn = lstm(input_size=10, hidden_size=32, return_sequences=False)
fc = dense(input_size=32, output_size=5)
# Process a sequence: (seq_len=20, features=10) โ (1, 5)
๐ ๏ธ Feature Reference
Layers
| Layer | Description | Import |
|---|---|---|
dense |
Fully connected layer | from pynnet.layers import dense |
conv2d |
2D convolution with padding & stride | from pynnet.layers import conv2d |
maxpool2d |
2D max pooling | from pynnet.layers import maxpool2d |
flatten |
Reshape (C,H,W) โ (1, CรHรW) | from pynnet.layers import flatten |
dropout |
Inverted dropout regularization | from pynnet.layers import dropout |
batchnorm |
Batch normalization | from pynnet.layers import batchnorm |
lstm |
LSTM recurrent layer | from pynnet.layers import lstm |
Activation Functions
| Function | Best For | Derivative |
|---|---|---|
relu |
Hidden layers (default) | relu_derivative |
sigmoid |
Binary output | sigmoid_derivative |
tanh |
Hidden layers (RNN) | tanh_derivative |
linear |
Regression output | linear_derivative |
leaky_relu |
Avoiding dead neurons | leaky_relu_derivative |
elu |
Smooth alternative to ReLU | elu_derivative |
swish |
Modern networks | swish_derivative |
softmax |
Multi-class output | (special layer) |
Loss Functions
| Function | Use Case |
|---|---|
mse / mse_derivative |
Regression |
mae / mae_derivative |
Robust regression |
huber_loss / huber_loss_derivative |
Outlier-resistant regression |
binary_cross_entropy / binary_cross_entropy_derivative |
Binary classification |
categorical_cross_entropy / cce_derivative_with_softmax |
Multi-class classification |
Optimizers
| Optimizer | Description |
|---|---|
SGD(lr) |
Vanilla gradient descent |
Momentum(lr, momentum) |
SGD with momentum |
RMSprop(lr, rho) |
Adaptive learning rates |
Adam(lr, beta1, beta2) |
Adaptive moments (recommended) |
Weight Initialization
| Method | Best With |
|---|---|
'he' |
ReLU / Leaky ReLU (default) |
'xavier' |
Sigmoid / Tanh |
'lecun' |
SELU / normalized inputs |
'orthogonal' |
Deep networks / RNNs |
๐ Project Structure
pynnet/
โโโ layers/
โ โโโ base.py # Abstract base layer
โ โโโ dense.py # Fully connected layer
โ โโโ conv2d.py # 2D convolution
โ โโโ maxpool2d.py # Max pooling
โ โโโ flatten.py # Flatten layer
โ โโโ dropout.py # Dropout regularization
โ โโโ batchnorm.py # Batch normalization
โ โโโ lstm.py # LSTM recurrent layer
โโโ activation.py # Activation functions & layers
โโโ loss.py # Loss functions & derivatives
โโโ optimizer.py # SGD, Momentum, RMSprop, Adam
โโโ network.py # Sequential model class
โโโ __init__.py # Public API exports
tests/
โโโ test_pynnet.py # 36 unit tests
examples/
โโโ 01_xor_example.py
โโโ 02_binary_classification.py
โโโ 03_regression.py
๐งช Running Tests
pip install pytest
python -m pytest tests/ -v
๐พ Save & Load Models
# Save weights
model.save_weights('my_model.npz')
# Load into an identical architecture
model.load_weights('my_model.npz')
๐ Requirements
- Python โฅ 3.8
- NumPy โฅ 1.20.0
๐ License
Creative Commons BY-NC-SA 4.0 โ free for non-commercial and educational use.
๐ค Author
Zain Qamar โ GitHub ยท Email
๐ Citation
@software{pynnet2025,
author = {Qamar, Zain},
title = {PyNNet: An Educational Deep Learning Library},
year = {2025},
url = {https://github.com/prime-programmer-ar/pynnet_project}
}
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 pynnet-1.0.1.tar.gz.
File metadata
- Download URL: pynnet-1.0.1.tar.gz
- Upload date:
- Size: 29.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1810a276275ed26924d9a5defd7a8cd6682dd26693629b3b8f380c58aab5b4f
|
|
| MD5 |
ab4afdb03be203c67d7915df89b3d16e
|
|
| BLAKE2b-256 |
5022484c3130f30b503c26dbcd22c6ef8cf3ad931886c84d829a3c9b1e21904a
|
File details
Details for the file pynnet-1.0.1-py3-none-any.whl.
File metadata
- Download URL: pynnet-1.0.1-py3-none-any.whl
- Upload date:
- Size: 29.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e27bc67d931b92bfbf2e88ef38d466173f8cbaac4a6302c6e3ce902c1ed8fa9
|
|
| MD5 |
a25e1dfa4c8d21d321a83585ad27d042
|
|
| BLAKE2b-256 |
962a44e0983c6eb4feac1dfdbc49c3d3ec908f4f0f09efa48a05f3f403474e18
|