Zing Forum

Reading

Implementing a Multilayer Perceptron from Scratch: A Complete Practice with Pure NumPy Neural Networks

A project that implements a Multilayer Perceptron (MLP) from scratch using pure NumPy, including a complete neural network architecture, forward/backward propagation, SGD and Adam optimizers, and a practical case of breast cancer classification.

神经网络多层感知机NumPy机器学习反向传播Adam优化器SGD乳腺癌分类从零实现深度学习
Published 2026-06-05 21:12Recent activity 2026-06-05 21:19Estimated read 4 min
Implementing a Multilayer Perceptron from Scratch: A Complete Practice with Pure NumPy Neural Networks
1

Section 01

Introduction / Main Floor: Implementing a Multilayer Perceptron from Scratch: A Complete Practice with Pure NumPy Neural Networks

A project that implements a Multilayer Perceptron (MLP) from scratch using pure NumPy, including a complete neural network architecture, forward/backward propagation, SGD and Adam optimizers, and a practical case of breast cancer classification.

2

Section 02

Original Author and Source

3

Section 03

Project Overview

This project is a teaching-oriented neural network implementation aimed at helping learners understand the basic principles of artificial neural networks. Built entirely from scratch using NumPy without relying on any deep learning frameworks, it implements a complete MLP architecture and applies it to the Wisconsin Breast Cancer Dataset for a binary classification task (malignant vs. benign tumors).

The dataset contains 569 samples and 30 features, making it a classic small-scale machine learning dataset ideal for verifying the correctness of neural network implementations.

4

Section 04

Project Architecture and Core Components

The project uses a modular design with clear responsibilities for each component:

5

Section 05

1. Data Preprocessing Module (split.py)

Responsible for splitting raw data into training and validation sets in an 80/20 ratio, and supports fixing random seeds to ensure reproducible results. This step is crucial for preventing data leakage and obtaining reliable model evaluations.

6

Section 06

2. Network Core (network.py)

Implements the forward and backward propagation algorithms of the neural network. It supports configurable hidden layer structures, with a default configuration of three hidden layers each containing 24 neurons. Weight initialization supports two strategies: He Uniform and Xavier Uniform, which are suitable for ReLU and Sigmoid activation functions respectively.

7

Section 07

3. Activation Functions and Utility Functions (utils.py)

Contains implementations of activation functions such as Sigmoid, ReLU, and Softmax, as well as cross-entropy loss function and one-hot encoding tools. These basic components are the cornerstones of neural network operations.

8

Section 08

4. Optimizers (optimizer.py)

Implements two classic optimization algorithms:

SGD (Stochastic Gradient Descent) : The most basic optimization method, which updates all parameters using a single global learning rate.

Adam (Adaptive Moment Estimation) : Combines the advantages of Momentum and RMSProp, maintains first-moment (gradient mean) and second-moment (mean of squared gradients) estimates for each parameter, and achieves adaptive learning rate adjustment through bias correction.