Multilayer Perceptron (MLP) from Scratch 🧠
Multilayer Perceptron from Scratch is a lightweight, educational, and high-performance implementation of a feedforward artificial neural network built entirely from scratch using NumPy.
A lightweight, educational, and high-performance implementation of a Multilayer Perceptron neural network, built entirely from scratch using NumPy.
This project implements the fundamental mathematics of deep learning—including Backpropagation and Stochastic Gradient Descent (SGD) with data shuffling—without the overhead of heavy frameworks like PyTorch or TensorFlow.
✨ Key Features
-
🎯 Pure NumPy: Zero dependencies for the core engine (only
numpy). -
🔄 Stochastic Gradient Descent (SGD): Includes automatic data shuffling every epoch for better convergence.
-
🏗️ Flexible Architecture: Custom-define any number of hidden layers and neurons.
-
📉 Matrix-Based Backpropagation: Efficient implementation using the four fundamental equations of backpropagation.
-
🎓 Educational Design: Clean, commented code ideal for learning how neural networks actually work "under the hood."
📦 Installation
You can install the package directly from PyPI:
pip install multilayer-perceptron-from-scratch
📓 Interactive Jupyter Notebooks (.ipynb)
For a richer, visual, and hands-on experience, the repository includes ready-to-run Jupyter Notebooks featuring complex non-linear classification tasks. Each demo captures dynamic metrics like Loss progression and Accuracy scores, culminating in detailed decision boundary plots using matplotlib.
🌕 DEMO 1: Moons Dataset Boundary
- Description: A classic, non-linear benchmarking problem. This demo uses a hidden layer of 16 neurons to smoothly separate two intertwining mathematical half-circles.
- Key Components:
sklearn.datasets.make_moons,matplotlib.pyplotcontour plotting. - Performance: Reaches 100% accuracy in less than 1000 epochs.
- 🔗 Run in Cloud:
🌀 DEMO 2: Wide-Gap 6-Arm Spiral
- Description: A highly complex geometric distribution designed to test the network's ability to capture sharp, twisting transitions. By leveraging a deeper architecture (
[2, 10, 10, 1]) and a higher learning rate ($\eta = 0.2$), the model maps the intricate spiral arms perfectly. - Key Components: Custom synthetic spiral generator with a wide center gap.
- Performance: Successfully untangles all 6 arms, achieving 99.8%+ accuracy by epoch 2000.
- 🔗 Run in Cloud:
📊 DEMO 3: Continuous Cosine vs. Interrupted Sine
- Description: A sophisticated phase and amplitude classification task. The network is challenged to classify a completely continuous blue Cosine wave against a red Sine wave that gets intentionally interrupted at intersection points to avoid mathematical overlapping.
- Key Components: Custom hybrid mathematical wave simulation,
matplotlib.lines.Line2Dadvanced legends. - Performance: Demonstrates exceptional decision-boundary fitting with 98.6% accuracy.
- 🔗 Run in Cloud:
🚀 Quick Start (XOR Example)
The following example demonstrates how to solve the classic XOR problem using the Network class.
import numpy as np
from multilayer_perceptron import Network
# 1. Define Architecture: [Input (2), Hidden (20), Output (1)]
net = Network([2, 20, 1])
# 2. Prepare Training Data (XOR)
# Format: List of tuples (input_vector, target_vector)
training_data = [
(np.array([[0], [0]]), np.array([[0]])),
(np.array([[0], [1]]), np.array([[1]])),
(np.array([[1], [0]]), np.array([[1]])),
(np.array([[1], [1]]), np.array([[0]]))
]
# 3. Train using SGD
# Parameters: data, epochs, learning_rate
print("Starting Training...")
net.SGD(training_data, epochs=3000, learning_rate=0.2)
# 4. Test the model
print("\nResults:")
for x, y in training_data:
output, _, _ = net.feedforward(x)
prediction = 1 if output >= 0.5 else 0
print(f"Input: {x.flatten()} | Target: {int(y[0][0])} | Predicted: {prediction}")
📜 License
Distributed under the MIT License. See LICENSE for more information.
Created with ❤️ by Nehorai Yosef
Metadata
Release files for multilayer-perceptron-from-scratch 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| multilayer_perceptron_from_scratch-0.1.3.tar.gz | 7.2 kB | Without provenance | Details for multilayer_perceptron_from_scratch-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| multilayer_perceptron_from_scratch-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for multilayer_perceptron_from_scratch-0.1.3-py3-none-any.whl |
Total release size: 15.3 kB
Release files / multilayer_perceptron_from_scratch-0.1.3.tar.gz
| Download URL | multilayer_perceptron_from_scratch-0.1.3.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7162f383e0c0e05d8b7229ecd4e458f53a9b2a1c31dad5b3cfae5c3629896b6b
|
|
BLAKE2b-256 checksum How to use checksums |
998e31497e1e8249453fce118133355b9881a2abd0d071602a6a7cdfcf47b1c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Release files / multilayer_perceptron_from_scratch-0.1.3-py3-none-any.whl
| Download URL | multilayer_perceptron_from_scratch-0.1.3-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c06ef32f4c93301dca07a91af433fda785ad629a6f5734b9030f1cdc6583794
|
|
BLAKE2b-256 checksum How to use checksums |
996cfb39fcb3faf93cf0bd8ed6049a7871b6bbde5c0806985145bb986e5d16ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Security for multilayer-perceptron-from-scratch 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.