brique.rs
What is brique.rs ?
Brique.rs is a multi-perceptron layer (MLP) library developped in rust from scratch without using any other library.
Everything was made from scratch :
- The matrix data structure
- All the linear algebra logic
- All the MLP logic
- A binary encoder/decoder for saving/loading traind models
- A CSV parser for unit/integration testing
The only dependencies of the project are the rand and rand-dist libs. There is no reliable way to generate random number in the standard library of Rust, and having a solid random number generator (on a normal distribution with a pre-determined standard deviation) is crucial for the weights initializations.
Features
- Build and train a MLP model
- Activation functions : ReLu, Softmax
- Optimizers : SGD, Adam
- Easy-to-use API based on a builder pattern
- Save and load models with .brq file format
But why ?
For two main reasons : I love building things, which is why I chose coding in the first place, and most importantly, to learn.
I significantly improved my Rust skills. I now have a better comprehension of the borrowing system and the memory structure in general. Before this project, I had a good theoretical grasp of how MLPs are structured and learn. Now, by writing and testing everything myself, I have a very deep understanding of every aspect of it. Beyond that I tackled many different problems. I wrote a CSV parser to facilitate unit and end-to-end testing and created my own binary encoder/decoder with a custom file format to save trained models.
This experience as a whole made me a better engineer.
Installation
Create a Rust project
Add the library to your dependency list, in the Cargo.toml file
Usage
Basic use
Here's a simple model trained on a spiral dataset, consisting of 3 spirals
Taken from examples/spiral.rs
use *;
use ModelBuilder;
use Optimizer;
use generate_spiral_dataset;
Run the program
⚠️ Do not forget the --release flag. If you don't use it the program could be significantly slower
The MNIST example
Here's how to train the MNIST dataset and save the trained model using a checkpoint on the best validation accuracy.
Download the dataset from https://www.kaggle.com/datasets/hojjatk/mnist-dataset and copy the files in the root of the project
You can use the pre-written functions to extract the dataset
use Checkpoint;
use *;
use *;
use *;
use ModelBuilder;
use Optimizer;
use *;
use *;
Code example of how to load the model and test it
You can also directly launch the above examples with
And
The .brq binary file
| Field | Size (bytes) | Description |
|---|---|---|
| Header | ||
| Magic Number | 6 | Fixed identifier "COOKIE" |
| Version | 1 | Library version |
| Length | 8 | Total file size |
| Model Data | ||
| Start of Object | 3 | Fixed identifier "CAT" |
| Model ID | 1 | Identifier for Model |
| Learning Step | 8 | f64 value |
| Number of Layers | 8 | u64 value |
| Layers | Variable | Depends on the number of layers |
| Layer Data | (Repeated for each layer) | |
| Start of Object | 3 | Fixed identifier "CAT" |
| Layer ID | 1 | Identifier for Layer |
| Activation (ReLU) | 1 | bool as u8 |
| Weights Matrix | Variable | Depends on matrix size |
| Biases Matrix | Variable | Depends on matrix size |
| Matrix Data | (Repeated for each matrix) | |
| Start of Object | 3 | Fixed identifier "CAT" |
| Matrix ID | 1 | Identifier for Matrix |
| Transposed | 1 | bool as u8 |
| Height | 8 | u64 value |
| Width | 8 | u64 value |
| Data | Variable | Depends on the number of elements |