Skip to main content

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

PyPI Python License Status Tests


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, and flatten layers
  • ๐Ÿ” RNN Support โ€” Full lstm layer with BPTT
  • ๐ŸŽ›๏ธ Regularization โ€” dropout and batchnorm layers
  • ๐Ÿ”ง 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynnet-1.0.1.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

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

pynnet-1.0.1-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

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

Hashes for pynnet-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e1810a276275ed26924d9a5defd7a8cd6682dd26693629b3b8f380c58aab5b4f
MD5 ab4afdb03be203c67d7915df89b3d16e
BLAKE2b-256 5022484c3130f30b503c26dbcd22c6ef8cf3ad931886c84d829a3c9b1e21904a

See more details on using hashes here.

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

Hashes for pynnet-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e27bc67d931b92bfbf2e88ef38d466173f8cbaac4a6302c6e3ce902c1ed8fa9
MD5 a25e1dfa4c8d21d321a83585ad27d042
BLAKE2b-256 962a44e0983c6eb4feac1dfdbc49c3d3ec908f4f0f09efa48a05f3f403474e18

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