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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ps_neuralnetwork-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 cc30bc2d152724cd89fda9efcbcf0ba59e4cbe44b8737d7f902d5f7e7ef619ae
MD5 f2745696751762ffa4dc0b7019ca95f7
BLAKE2b-256 50747d3cffb9c76ed2b526865a2f91b0955dc0f51f741508f7d2a27a0cc7c994

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ps_neuralnetwork-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8611bae04171e7479b7fe1b062794dbb7cc3ff6a6cb9f982f151e3608cb6bee1
MD5 11cb5765261c41c2de8220b1642fc01c
BLAKE2b-256 1a2d2b21d9c48424f39edebad8fc7c9cbc08d76b1adb322dddc34547c481562d

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