Skip to main content

A clean and educational implementation of a Multilayer Perceptron from scratch using NumPy

Project description

Multilayer Perceptron (MLP) from Scratch 🧠

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

🚀 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

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

multilayer_perceptron_from_scratch-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

File details

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

File metadata

File hashes

Hashes for multilayer_perceptron_from_scratch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0112cda5379f8b0cd964c53e09ca9378c35249ec31c74da907463558a3ec97d7
MD5 282bf1fd1592771aa08e80fd278b6dd0
BLAKE2b-256 78f476821938e264013d5336038e977fc2dc03360dfcb70a113e0174ae3e374c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for multilayer_perceptron_from_scratch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f457c624a88aac52a6390e48d447e9bf8987c008304860d055af63d4eb61b6f
MD5 db18ef2a4505e30164a9b6982976f0ca
BLAKE2b-256 15f5642606fa93a5ea9c9113481127039700a05448dc0c7d1186264a137090bb

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