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

🚀 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.0.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.0.tar.gz.

File metadata

File hashes

Hashes for multilayer_perceptron_from_scratch-0.1.0.tar.gz
Algorithm Hash digest
SHA256 918a4e94503eb1b11c68a5d7b21ef7188c84f2287f961f1cc9ea7e1aae87a061
MD5 dbef62437afb0e316a1b28f0f539667e
BLAKE2b-256 8188b57b7ef56bca56e12757ae6e034d00f9765b2f34b80dd232c9a4f8b08290

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for multilayer_perceptron_from_scratch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e23160074b11937b1f2ae797c8a750fb03932352365f07ee283d9d289d5b0fe1
MD5 9c0a7b6c97a1b1c0e1666d9682f290b5
BLAKE2b-256 37853fc0f9bd494207f0ea18a9c1853fcdbae82b4e7c9c4834aa6f6ad1a8f93a

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