MiniNN
A minimalist deep learnig crate for rust.
✏️ Usage
For this example we will resolve the classic XOR problem
use ;
use *;
Output
Epoch 1/1000, error: 0.5241278261886898, time: 0.000293329 sec
Epoch 2/1000, error: 0.42558144595907677, time: 0.000232393 sec
Epoch 3/1000, error: 0.3776874679368199, time: 0.000237112 sec
...
Epoch 998/1000, error: 0.0017618690075552517, time: 0.000231691 sec
Epoch 999/1000, error: 0.001949346158027843, time: 0.00022584 sec
Epoch 1000/1000, error: 0.0022311549699578458, time: 0.000225159 sec
Training completed!
Training Error: 0.027032078040398873 , time: 0.23521075 sec
[0, 0] --> 0
[0, 1] --> 1
[1, 0] --> 1
[1, 1] --> 0
[[2, 0],
[0, 2]]
Accuracy: 1
Recall: 1
Precision: 1
F1: 1
Metrics
You can also calculate metrics for your models using ClassMetrics:
let metrics = new;
println!;
println!;
Confusion matrix:
[[2, 0],
[0, 2]]
Accuracy: 1
Recall: 1
Precision: 1
F1: 1
Default Layers
For now, the crate only offers two types of layers:
| Layer | Description |
|---|---|
Dense |
Fully connected layer where each neuron connects to every neuron in the previous layer. It computes the weighted sum of inputs, adds a bias term, and applies an optional activation function (e.g., ReLU, Sigmoid). This layer is fundamental for transforming input data in deep learning models. |
Activation |
Applies a non-linear transformation (activation function) to its inputs. Common activation functions include ReLU, Sigmoid, Tanh, and Softmax. These functions introduce non-linearity to the model, allowing it to learn complex patterns. |
[!NOTE] More layers in the future.
Save and load models
When you already have a trained model you can save it into a HDF5 file:
nn.save.unwrap;
let mut nn = NNload.unwrap;
Custom layers
All the layers that are in the network needs to implement the Layer trait, so is possible for users to create their own custom layers.
The only rule is that all the layers must implements the following traits (instead of the Layer trait):
Debug: Standars traits.Clone: Standars traits.Serialize: Fromserdecrate.DeserializeFromserdecrate.
If you want to save your model with your new custom Layer, you need to add it into the LayerRegister, this is a data structure that stored all the types of layers that the NN struct is going to accept.
Here is a little example about how to create custom layers:
use *;
use ;
use serde_json;
use Array1;
;
📖 Add the library to your project
You can add the crate with cargo
cargo add mininn
Alternatively, you can manually add it to your project's Cargo.toml like this:
[]
= "*" # Change the `*` to the current version
Examples
There is a multitude of examples if you want to learn how to use the library, just run these commands.
cargo run --example xor
cargo run --example xor_load_nn
cargo run --example mnist
cargo run --example mnist_load_nn
cargo run --example custom_layer
📑 Libraries used
- rand - For Random stuffs.
- ndarray - For manage N-Dimensional Arrays.
- ndarray-rand - For manage Random N-Dimensional Arrays.
- serde - For serialization.
- serde_json - For JSON serialization.
- hdf5 - For model storage.
TODOs 🏁
- Add Conv2D (try Conv3D) layer
- Add optimizers
🔑 License
MIT - Created by Paco Algar.