This tutorial will help you create a sample machine learning app using mlpack/C++. Although this app does not cover all the mlpack capabilities, it will walkthrough several APIs to understand how everything connects. This Windows sample app is created using Visual Studio, but you can easily adapt it to a different platform by following the provided source code.
- Before starting, make sure you have built mlpack for Windows following this Windows guide
- Open Visual Studio and create a new project (Windows Console Application)
- For this sample, the project is named “sample-ml-app”
There are different ways in which you can configure your project to link with dependencies. This configuration is for x64 Debug Mode. If you need Release Mode, please change the paths accordingly (assuming you have built mlpack and dependencies in Release Mode).
- Right click on the project and select Properties, select the x64 Debug profile
- Under C/C++ > General > Additional Include Directories add: - C:\boost\boost_1_66_0- C:\mlpack\armadillo-8.500.1\include- C:\mlpack\mlpack-3.0.3\build\include
- Under Linker > Input > Additional Dependencies add: - C:\mlpack\mlpack-3.0.3\build\Debug\mlpack.lib- C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_serialization-vc141-mt-gd-x64-1_66.lib- C:\boost\boost_1_66_0\lib64-msvc-14.1\libboost_program_options-vc141-mt-gd-x64-1_66.lib
- Under Build Events > Post-Build Event > Command Line add: - xcopy /y "C:\mlpack\mlpack-3.0.3\build\Debug\mlpack.dll" $(OutDir)- xcopy /y "C:\mlpack\mlpack-3.0.3\packages\OpenBLAS.0.2.14.1\lib\native\bin\x64\*.dll" $(OutDir)
- Recent versions of Visual Studio set "Conformance Mode" enabled by default. This causes some issues with the armadillo library. If you encounter this issue, disable "Conformance Mode" under C/C++ > Language.
This app aims to exercise an end-to-end machine learning workflow. We will cover:
- Loading and preparing a dataset
- Training (using Random Forest as example)
- Computing the training accuracy
- Cross-Validation using K-Fold
- Metrics gathering (accuracy, precision, recall, F1)
- Saving the trained model to disk
- Loading the model
- Classifying a new sample
For this app, we will need to include the following headers (i.e. add into stdafx.h):
Also, we will use the following namespaces:
First step is about loading the dataset. Different dataset file formats are supported, but here we load a CSV dataset, and we assume the labels don't require normalization.
- Make sure you update the path to your dataset file. For this sample, you can simply copy "mlpack/tests/data/german.csv" and paste into a new "data" folder in your project directory.
Then we need to extract the labels from the last dimension of the dataset and remove the labels from the training set:
We now have our dataset ready for training.
This app will use a Random Forest classifier. At first we define the classifier parameters and then we create the classifier to train it.
Now that the training is completed, we quickly compute the training accuracy:
Instead of training the Random Forest directly, we could also use K-fold cross-validation for training, which will give us a measure of performance on a held-out test set. This can give us a better estimate of how the model will perform when given new data. We also define which metric to use in order to assess the quality of the trained model.
To compute other relevant metrics, such as Precision, Recall and F1:
Now that our model is trained and validated, we save it to a file so we can use it later. Here we save the model that was trained using the entire dataset. Alternatively, we could extract the model from the cross-validation stage by using
We can also save the model in
bin format ("mymodel.bin") which would result in a smaller file.
In a real-life application, you may want to load a previously trained model to classify new samples. We load the model from a file using:
Finally, the ultimate goal is to classify a new sample using the previously trained model. Since the Random Forest classifier provides both predictions and probabilities, we obtain both.
Building real-life applications and services using machine learning can be challenging. Hopefully, this tutorial provides a good starting point that covers the basic workflow you may need to follow while developing it. You can take a look at the entire source code in the provided sample project located here: "doc/examples/sample-ml-app".
Generated by 1.8.13