Skip to main content

A minimal neural network framework with automatic differentiation built from scratch

Project description

ps-neuralnetwork

A minimal neural network framework built from scratch in pure Python, featuring a custom automatic-differentiation Value class, multi-layer perceptron (Neural_Network), training loop, and decision boundary visualization. Perfect for educational purposes and quick experimentation with 2D classification tasks.


Features

  • Automatic Differentiation via a custom Value class
  • Multi-Layer Perceptron with configurable hidden layers and tanh activations
  • Training Utility with Mean Squared Error loss and manual gradient descent
  • Decision Boundary Visualization for 2D datasets using Matplotlib

Installation

git clone https://github.com/yourusername/ps-neuralnetwork.git
cd ps-neuralnetwork
pip install -e .

This installs the package in “editable” mode so you can modify the source and immediately test changes.


Quick Start

# 1. Import training helper and network
from ps_neuralnetwork.training import training
from ps_neuralnetwork.nn_scratch import Neural_Network

# 2. Generate 2D classification data
from sklearn.datasets import make_moons
import matplotlib.pyplot as plt

X, y = make_moons(n_samples=100, noise=0.1)
y = y * 2 - 1  # Convert labels from {0,1} to {-1,+1}

# 3. Visualize raw data
plt.figure(figsize=(5,5))
plt.scatter(X[:,0], X[:,1], c=y, s=20, cmap='jet')
plt.title("Make Moons Dataset")
plt.show()

# 4. Create and configure Neural Network
net = Neural_Network(input_size=2, layer_sizes=[4, 3, 5, 2, 1])

# 5. Train the network
learning_rate = 0.001
num_epochs     = 500
trainer = training(net, X, y, learning_rate, num_epochs)

# Run training
trainer()

# 6. Plot training progress and decision boundary
trainer.plot(smoothness=0.05)

API Reference

ps_neuralnetwork.nn_scratch.Neural_Network(input_size, layer_sizes)

  • input_size: Number of input features (e.g., 2 for 2D data)
  • layer_sizes: List of neuron counts for each hidden and output layer (e.g., [4,3,1])

Creates a feed-forward network with tanh activations.


ps_neuralnetwork.training.training(net, X, y, learning_rate, num_epochs)

  • net: Instance of Neural_Network
  • X: Feature array of shape (n_samples, input_size)
  • y: Label array of shape (n_samples,) with values -1 or +1
  • learning_rate: Step size for gradient descent (e.g., 0.001)
  • num_epochs: Number of training epochs (e.g., 500)

Returns a trainer object with:

  • Callable: trainer() to start training
  • trainer.plot(smoothness): Visualizes decision boundary and loss over epochs
    • smoothness controls contour resolution

Package Structure

ps-neuralnetwork/
├── src/
│   └── ps_neuralnetwork/
│       ├── __init__.py
│       ├── value.py        # Automatic differentiation engine
│       ├── network.py      # Multi-layer perceptron
│       └── training.py     # Training loop and visualization
├── README.md              # This file
├── LICENSE                # MIT License
└── pyproject.toml         # Build and metadata

License

This project is released under the MIT License. See the LICENSE file for details.


Acknowledgements

  • Built with NumPy, Matplotlib, and scikit-learn
  • Inspired by “neural networks from scratch” tutorials and micro-autograd implementations

The Value class was adapted from Andrej Karpathy’s micrograd project (https://github.com/karpathy/micrograd), MIT License.

Happy experimenting! 🎉

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

ps_neuralnetwork-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

ps_neuralnetwork-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file ps_neuralnetwork-0.1.0.tar.gz.

File metadata

  • Download URL: ps_neuralnetwork-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for ps_neuralnetwork-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bdc44e7744a4d8eb2e845da71e914d01d93cd6c40ac0bd1a30e7f2f8b0d09e1f
MD5 aeb92742aa57a1ba430323ace88c8dd3
BLAKE2b-256 5e7653bf497f457a8c28d9c1a75e6fd32351466d120959d23a1ed7342a3166a6

See more details on using hashes here.

File details

Details for the file ps_neuralnetwork-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ps_neuralnetwork-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43ed7ef690084cfb1900b0f1b1a525a04f1cb472a049a5384b812c562711151f
MD5 a44545911201bc916810fe263ae2e9eb
BLAKE2b-256 86584aa80ec8985215b78ab0ddbc736a7dc055d99517f1325e771899694cbf40

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