A multilayer perceptron (MLP) implemented in NumPy from scratch (no autograd) for binary classification of breast cell malignancy (e.g. Wisconsin Breast Cancer–style data).
- Pure NumPy MLP: vectorized forward/backward pass, ReLU or sigmoid hidden layers, configurable input/output sizes
- Optimizers: SGD and RMSprop
- Training: train/validation split, learning curves, early stopping, optional batch training
- Hyperparameter search:
--bestto run a grid and pick the best run; optional test-set ranking - Pipeline: prepare dataset → split → train → predict, plus EDA and run comparison
- Python ≥ 3.13, < 3.14
- Dependencies:
numpy,pandas,scikit-learn,matplotlib,seaborn,pydantic,fastapi,uvicorn
For this full project:
uv syncThis installs:
mlp: this project's breast-cancer training CLI, visualizer, telemetry, and dataset pipelinemlp_core: the lightweight package pulled fromhttps://github.com/maujogue/mlp-core.git
For another project that only needs the lightweight reusable MLP/DQN package:
uv add "mlp-core @ git+https://github.com/maujogue/mlp-core.git"That installs only mlp_core and its NumPy dependency.
Reusable package API:
from mlp_core import DQN, MLPClassifier, MLPNetwork
classifier = MLPClassifier(input_size=30, hidden_layers=[24, 24], output_size=2)
network = MLPNetwork(input_size=4, hidden_layers=[64, 64], output_size=2)
dqn = DQN(state_size=4, n_actions=2, hidden_layers=[64, 64])- Input: CSV with an ID column (dropped), then 30 feature columns and a label column (e.g.
M/Bfor malignant/benign). - Prepared CSV: use
mlp-prepare-datasetto fix column names and fill missing values. Scaling is applied during training (and reused at predict time).
Fix column titles and fill missing values. Output path defaults to <stem>_prepared.csv next to the input.
mlp-prepare-dataset path/to/raw_data.csv [-o path/to/prepared.csv]mlp-split path/to/prepared.csv [--test-size 0.2]Creates train.csv and test.csv in the same directory as the prepared file.
Train on the training CSV (with internal train/validation split). Model and learning curves are always written under temp/<opts>_<timestamp>/ (e.g. model.pkl, scaler.pkl, figures/).
mlp-train path/to/train.csv [options]Common options:
| Option | Default | Description |
|---|---|---|
--layers |
24 24 |
Hidden layer sizes (space-separated) |
--epochs |
70 |
Number of epochs |
--learning-rate |
0.01 |
Learning rate |
--val-ratio |
0.2 |
Fraction of train used for validation |
--optimizer |
sgd |
sgd or rmsprop |
--batch-size |
0 |
Batch size; 0 = full dataset |
--patience |
0 |
Early stopping (epochs without improvement); 0 = off |
--seed |
42 |
Random seed |
Hyperparameter search (best run):
mlp-train path/to/train.csv --best [test1.csv test2.csv ...]Runs a grid over layers/lr/optimizer and picks the best run. If one or more test CSVs are given, best models are ranked by average metrics on those sets.
Run inference with a saved model. Preprocessing (fix + scaler) is applied automatically. Use a run directory (e.g. temp/opts_xxx/) or the path to model.pkl so the same scaler used at training is found.
mlp-predict [path/to/test.csv] --model-path path/to/run_dir_or_model.pkl [--output-path predictions.csv]Generate EDA figures from a training CSV:
mlp-eda [path/to/train.csv] [--output-dir figures/eda]Compare multiple training runs (each folder must contain history.json) and plot learning curves:
mlp-compare run_folder1 run_folder2 ... [--save path_or_dir] [--test test1.csv test2.csv]Optional: --accuracy, --f1, --recall, --precision, --val, --train, --val-train.
Interactive UI for epoch-by-epoch replay of saved runs (history.json / run_config.json) and live training with per-batch and per-epoch telemetry over SSE.
API contract (endpoints and event shapes): docs/visualizer_api.md.
-
Install JS deps and build the UI (from repo root):
cd visualizer/frontend && npm install && npm run build
-
Start the server (serves API + static UI):
mlp-visualizer --static visualizer/frontend/dist
Or API only on default port
8765(use with Vite dev proxy):mlp-visualizer
-
Dev workflow: terminal A —
mlp-visualizer; terminal B —cd visualizer/frontend && npm run dev→ open the URL Vite prints (proxies/apito127.0.0.1:8765).
Replay: scans temp/ (or set “Runs root” in the UI) for **/history.json. Curves use the same series as history.json (parity with mlp-compare / saved PNGs).
Live: POST /api/live/train starts training in a background thread; connect GET /api/live/stream/{session_id} immediately after (the UI does this). Batch events include loss, gradient norm per layer, and weight-update norm per layer (when sampling allows).
From the project root:
make format # ruff format
make lint # ruff check
make type # ty check
make all # format + lint + typemlp/
├── Makefile
├── pyproject.toml
├── src/
│ └── mlp/
│ ├── cli.py # CLI entrypoints
│ ├── data/
│ │ ├── data_engineering.py # prepare, split, scale, load
│ │ └── feature_engineering.py # EDA
│ ├── model/
│ │ ├── mlp_classifier.py # MLPClassifier (NumPy)
│ │ ├── telemetry.py # optional training telemetry for visualizer
│ │ ├── schemas.py # Pydantic config & training history
│ │ ├── training.py # train command
│ │ ├── predict.py # predict command
│ │ ├── evaluation.py # metrics, evaluate on dataset
│ │ ├── compare.py # best search + run comparison
│ │ ├── plots.py # learning curves
│ │ └── serialization.py # save/load model + scaler
│ ├── visualizer/ # FastAPI server + run discovery
│ │ ├── server.py
│ │ ├── discovery.py
│ │ └── api_schemas.py
│ └── utils/
│ ├── constants.py # feature names, labels
│ ├── loader.py # dataset loading
│ └── CustomStandardScaler.py
├── docs/
│ └── visualizer_api.md # HTTP + SSE contract
├── visualizer/frontend/ # React + Vite UI (npm)
└── README.md
See repository or project rules. Author: Mathis.