rust-mlp
A small, from-scratch multi-layer perceptron (MLP) library for Rust.
This crate is intentionally "small-core": dense layers, common activations/losses, a predictable training loop, and a performance model you can reason about.
Highlights:
- Allocation-free hot path: reuse
Scratch/Gradients. - Batched compute:
forward_batch/backward_batchwith an optional faster GEMM backend. - Determinism: seeded init + deterministic shuffling.
- Practical API:
fit,evaluate,predict,predict_into. - Optional JSON model I/O (feature:
serde).
Install
[]
= "0.1"
Optional features:
[]
= { = "0.1", = ["matrixmultiply", "serde"] }
Quick start (train + evaluate)
use ;
Allocation-free inference
Mlp::forward is allocation-free if you reuse Scratch. For shape-checked inference (returns Result), use predict_into.
use ;
Feature flags
matrixmultiply: use thematrixmultiplycrate as a faster GEMM backend for batched compute.serde: enable JSON model serialization viaserde+serde_json.
Data layout and shapes
- All scalars are
f32. Dataset/Inputsstore samples contiguously in row-major layout.- inputs shape:
(len, input_dim)stored aslen * input_dimflat - targets shape:
(len, target_dim)stored aslen * target_dimflat
- inputs shape:
Performance
fitallocates its buffers once and reuses them across steps.- When
batch_size > 1,fituses a batched GEMM-based path for full-size batches. - For meaningful batched performance, enable the
matrixmultiplyfeature (otherwise a simple scalar GEMM is used). - Benchmarks:
Examples
MSRV
MSRV is specified in Cargo.toml.
License
MIT. See LICENSE.