MiniNN
A minimalist deep learnig crate for rust.
[!NOTE] This crate is still in development and is not ready for production use.
🔧 Setup
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
✏️ Quick Start: Solving XOR
For this example we will resolve the classic XOR problem
use ;
use *;
Output
Epoch 1/200 - Loss: 0.2636616, Time: 0.000482592 sec
Epoch 2/200 - Loss: 0.265602, Time: 0.000444258 sec
Epoch 3/200 - Loss: 0.26768285, Time: 0.000398091 sec
...
Epoch 198/200 - Loss: 0.0010192227, Time: 0.000600476 sec
Epoch 199/200 - Loss: 0.0009878413, Time: 0.000510074 sec
Epoch 200/200 - Loss: 0.0009578406, Time: 0.000512518 sec
Training Completed!
Total Training Time: 0.11 sec
Predictions:
[0, 0] --> 0
[0, 1] --> 1
[1, 0] --> 1
[1, 1] --> 0
Confusion matrix:
[[2, 0],
[0, 2]]
Accuracy: 1
Recall: 1
Precision: 1
F1: 1
Loss: 0.0009578406
Model saved successfully!
📊 Train and evaluation
Train the model
In order to train the model, you need to provide the training data, the labels and the training configuration. The training configuration is a struct that contains all the parameters that are used during the training process, such as the number of epochs, the cost function, the learning rate, the batch size, the optimizer, and whether to print the training process or not.
let train_data = array!;
let labels = array!;
let loss = nn.train?;
Predict the model
Once the model is trained, you can use it to make predictions on new data. To do this, you need to provide the input data to the predict method.
let input = array!;
let output = nn.predict?;
Metrics
You can also calculate metrics for your models using MetricsCalculator:
let metrics = new;
println!;
println!;
This is the output of the iris example
Confusion matrix:
[[26, 0, 0],
[0, 28, 1],
[0, 2, 18]]
Accuracy: 0.96
Recall: 0.9551724137931035
Precision: 0.960233918128655
F1: 0.9574098218166016
Save and load models
When you already have a trained model you can save it into a HDF5 file:
nn.save.unwrap;
let nn = NNload.unwrap;
🧰 Built-in Components
The crate defines some default layers, activations and costs that can be used in your model:
Default Layers
For now, the crate only offers these 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. |
Flatten |
Flattens the input into a 1D array. This layer is useful when the input is a 2D array, but you want to treat it as a 1D array. |
Dropout |
Applies dropout, a regularization technique where randomly selected neurons are ignored during training. This helps prevent overfitting by reducing reliance on specific neurons and forces the network to learn more robust features. Dropout is typically used in the training phase and is deactivated during inference. |
[!NOTE] More layers in the future.
Cost functions
The crate also provides a set of cost functions that can be used in the training process, these are represented by the Cost enum::
-
Cost::MSE: Mean Squared Error. This cost function measures the average squared difference between the predicted and actual values.\text{MSE}(y_p, y) = \frac{1}{n} \sum_{i=1}^{n} (y_p - y)^2 -
Cost::MAE: maps the input to a value between 0 and 1, which is the probability of the input being 1.\text{MAE}(y_p, y) = \frac{1}{n} \sum_{i=1}^{n} |y_p - y| -
Cost::BCE: maps the input to 0 if it is negative, and the input itself if it is positive.\text{BCE}(y_p, y) = -\frac{1}{n} \sum_{i=1}^{n} y_p \log(y) + (1 - y_p) \log(1 - y) -
Cost::CCE: maps the input to a value between -1 and 1, which is the ratio of the input to the hyperbolic tangent of the input.\text{CCE}(y_p, y) = -\frac{1}{n} \sum_{i=1}^{n} y_p \log(y)
Activation functions
The crate provides a set of activation functions that can be used in the Activation layer, these are represented by the Act enum:
-
Act::Step: maps the input to 0 if it is negative, and 1 if it is positive.\text{step}(x) = \begin{cases} 0 & \text{if } x < 0 \\ 1 & \text{if } x \geq 0 \end{cases} -
Act::Sigmoid: maps the input to a value between 0 and 1, which is the probability of the input being 1.\text{sigmoid}(x) = \frac{1}{1 + e^{-x}} -
Act::ReLU: maps the input to 0 if it is negative, and the input itself if it is positive.\text{ReLU}(x) = \max(0, x) -
Act::Tanh: maps the input to a value between -1 and 1, which is the ratio of the input to the hyperbolic tangent of the input.\text{tanh}(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} -
Act::Softmax: maps the input to a probability distribution over the possible values of the input.\text{softmax}(x) = \frac{e^x}{e^x + \sum_{i=1}^{n} e^{x_i}}
🛠️ Customization
One of the main goals of the mininn crate is to provide a flexible and customizable framework for building and training neural networks. This section will cover how to create your own layers, activations and costs and how to register them with the framework.
Custom layers
All layers in the network are required to implement the Layer trait. This ensures that users can define their own custom layers while maintaining compatibility with the framework.
To fulfill this requirement, every layer must also implement the following traits in addition to Layer:
Debug: For inspecting print layers information.Clone: To enable copying of layer instances.SerializeandDeserialize: For seamless serialization and deserialization, typically usingserde.
Here is a little example about how to create custom layers:
use *;
use ;
use ArrayViewD;
// The implementation of the custom layer
;
Custom Activation Functions
You can also create your own activation functions by implementing the ActivationFunction and Debug traits.
use *;
use ;
;
Custom Cost Functions
You can also create your own cost functions by implementing the CostFunction and Debug traits.
use *;
use ;
;
Register layers, activations and costs
For use your custom layers, activation functions, or cost functions in the load method, you need to register them first:
The register! macro can be used to register your layers, activations, costs or all of them at once.
register!;
register!;
register!;
register!;
register!;
📋 Examples
There is a multitude of examples resolving classics ML problems, if you want to see the results just run these commands.
📑 Libraries used
- ndarray - For manage N-Dimensional Arrays.
- ndarray-rand - For manage Random N-Dimensional Arrays.
- serde - For serialization.
- rmp_serde - For MSGPack serialization.
- hdf5 - For model storage.
- dyn-clone - For cloning trait objects.
💻 Contributing
Contributions are welcome! Feel free to open issues or submit pull requests, see CONTRIBUTING.md for more information.
🔑 License
MIT - Created by Paco Algar.