|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Jachym Hudlicky <[email protected]> |
| 4 | + * @brief Mlpack AdaBoost model class |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: BSD-3-Clause |
| 7 | + */ |
| 8 | + |
| 9 | +#include "wif/ml/mlpackModels/mlpackModel.hpp" |
| 10 | +#include "wif/storage/clfResult.hpp" |
| 11 | +#include "wif/storage/flowFeatures.hpp" |
| 12 | + |
| 13 | +#include <armadillo> |
| 14 | +#include <mlpack.hpp> |
| 15 | +#include <stdexcept> |
| 16 | +#include <string> |
| 17 | +#include <utility> |
| 18 | +#include <variant> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +namespace WIF::MlpackModels { |
| 22 | + |
| 23 | +/** |
| 24 | + * @brief Class which provides AdaBoost model weak learner from Mlpack library |
| 25 | + * @tparam WeakLearnerType weak learner used by AdaBoost |
| 26 | + */ |
| 27 | +template<typename WeakLearnerType> |
| 28 | +class AdaBoostModel : public MlpackModel { |
| 29 | +public: |
| 30 | + /** |
| 31 | + * @brief Construct a new AdaBoost wrapper object with no loaded model |
| 32 | + */ |
| 33 | + AdaBoostModel() = default; |
| 34 | + |
| 35 | + /** |
| 36 | + * @brief Construct a new AdaBoost wrapper object |
| 37 | + * |
| 38 | + * @param modelPath contains path to trained model file |
| 39 | + * @param logicalName contains the logical name of the trained model. |
| 40 | + */ |
| 41 | + AdaBoostModel(const std::string& modelPath, const std::string& logicalName = "trained_data") |
| 42 | + { |
| 43 | + m_loaded = mlpack::data::Load(modelPath, logicalName, m_ab, true); |
| 44 | + if (m_loaded) { |
| 45 | + m_modelPath = modelPath; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Classify single flowFeature object |
| 51 | + * |
| 52 | + * @param flowFeatures flow features to classify |
| 53 | + * @return ClfResult result of the classification, which contains vector<double> with |
| 54 | + * probabilities for each class. |
| 55 | + */ |
| 56 | + ClfResult classify(const FlowFeatures& flowFeatures) override |
| 57 | + { |
| 58 | + arma::mat testDataset(m_featureIDs.size(), 1); |
| 59 | + arma::Row<size_t> predictions; |
| 60 | + arma::mat probaMatrix; |
| 61 | + std::vector<ClfResult> burstResults; |
| 62 | + std::vector<FlowFeatures> burstOfFeatures = {flowFeatures}; |
| 63 | + |
| 64 | + burstResults.reserve(1); |
| 65 | + |
| 66 | + MlpackModel::convertBurstOfFeaturesToMatrix(burstOfFeatures, testDataset); |
| 67 | + m_ab.Classify(testDataset, predictions, probaMatrix); |
| 68 | + for (unsigned i = 0; i < predictions.n_elem; ++i) { |
| 69 | + std::vector<double> probabilities(probaMatrix.col(i).begin(), probaMatrix.col(i).end()); |
| 70 | + burstResults.emplace_back(probabilities); |
| 71 | + } |
| 72 | + |
| 73 | + return burstResults[0]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @brief Classify a burst of flow features |
| 78 | + * |
| 79 | + * @param burstOfFlowsFeatures the burst of flow features to classify |
| 80 | + * @return std::vector<ClfResult> the results of the classification. Each ClfResult contains |
| 81 | + * result of the classification, which contains vector<double> with probabilities for each |
| 82 | + * class |
| 83 | + */ |
| 84 | + std::vector<ClfResult> classify(const std::vector<FlowFeatures>& burstOfFeatures) override |
| 85 | + { |
| 86 | + arma::mat testDataset(m_featureIDs.size(), burstOfFeatures.size()); |
| 87 | + arma::Row<size_t> predictions; |
| 88 | + arma::mat probaMatrix; |
| 89 | + std::vector<ClfResult> burstResults; |
| 90 | + |
| 91 | + burstResults.reserve(burstOfFeatures.size()); |
| 92 | + |
| 93 | + MlpackModel::convertBurstOfFeaturesToMatrix(burstOfFeatures, testDataset); |
| 94 | + m_ab.Classify(testDataset, predictions, probaMatrix); |
| 95 | + for (unsigned i = 0; i < predictions.n_elem; ++i) { |
| 96 | + std::vector<double> probabilities(probaMatrix.col(i).begin(), probaMatrix.col(i).end()); |
| 97 | + burstResults.emplace_back(probabilities); |
| 98 | + } |
| 99 | + |
| 100 | + return burstResults; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @brief Load AdaBoost model from file |
| 105 | + * |
| 106 | + * @param modelPath contains path to the model file. |
| 107 | + * @param logicalName contains the logical name of the trained model. |
| 108 | + * @return Bool value true, if model was successfully loaded. False if not. |
| 109 | + */ |
| 110 | + bool |
| 111 | + load(const std::string& modelPath, const std::string& logicalName = "trained_data") override |
| 112 | + { |
| 113 | + m_loaded = mlpack::data::Load(modelPath, logicalName, m_ab); |
| 114 | + if (m_loaded) { |
| 115 | + m_modelPath = modelPath; |
| 116 | + } |
| 117 | + return m_loaded; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief Save AdaBoost model to file |
| 122 | + * |
| 123 | + * @param modelPath contains file path, where the model will be saved. |
| 124 | + * @param logicalName contains the logical name of the trained model. |
| 125 | + * @return Bool value true, if model was successfully saved. False if not. |
| 126 | + */ |
| 127 | + bool save(const std::string& modelPath, const std::string& logicalName = "trained_data") |
| 128 | + const override |
| 129 | + { |
| 130 | + return mlpack::data::Save(modelPath, logicalName, m_ab); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @brief Train AdaBoost model |
| 135 | + * |
| 136 | + * @param data contains training vector of flow features. |
| 137 | + * @param labels contains training labels, between 0 and numClasses - 1 (inclusive). Should have |
| 138 | + * length data.length(). |
| 139 | + * @param path contains path, where file will be saved. |
| 140 | + * @param numClasses contains number of classes in the dataset. |
| 141 | + * @param maxIterations contains maximum number of iterations of AdaBoost.MH to use. This is the |
| 142 | + * maximum number of weak learners to train. (0 means no limit, and weak learners will be |
| 143 | + * trained until the tolerance is met.) |
| 144 | + * @param tolerance when the weighted residual (r_t) of the model goes below tolerance, training |
| 145 | + * will terminate and no more weak learners will be added. |
| 146 | + * @param weakLearnerParams optional weak learner hyperparameters. |
| 147 | + */ |
| 148 | + template<typename... WeakLearnerParams> |
| 149 | + void train( |
| 150 | + const std::vector<FlowFeatures>& data, |
| 151 | + const std::vector<size_t>& labels, |
| 152 | + const std::string& path, |
| 153 | + size_t numClasses = 2, |
| 154 | + size_t maxIterations = 100, |
| 155 | + double tolerance = 1e-6, |
| 156 | + WeakLearnerParams&&... weakLearnerParams) |
| 157 | + { |
| 158 | + arma::mat dataset(m_featureIDs.size(), data.size()); |
| 159 | + arma::Row<size_t> armaLabels(labels); |
| 160 | + |
| 161 | + MlpackModel::convertBurstOfFeaturesToMatrix(data, dataset); |
| 162 | + m_ab.Train( |
| 163 | + dataset, |
| 164 | + armaLabels, |
| 165 | + numClasses, |
| 166 | + maxIterations, |
| 167 | + tolerance, |
| 168 | + std::forward<WeakLearnerParams>(weakLearnerParams)...); |
| 169 | + |
| 170 | + this->save(path); |
| 171 | + } |
| 172 | + |
| 173 | +private: |
| 174 | + /** |
| 175 | + * @brief AdaBoost model |
| 176 | + */ |
| 177 | + mlpack::AdaBoost<WeakLearnerType, arma::mat> m_ab; |
| 178 | +}; |
| 179 | + |
| 180 | +} // namespace WIF::MlpackModels |
0 commit comments