|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Jachym Hudlicky <hudlijac@fit.cvut.cz> |
| 4 | + * @brief LightGBM wrapper interface |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: BSD-3-Clause |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "wif/storage/clfResult.hpp" |
| 12 | +#include "wif/storage/flowFeatures.hpp" |
| 13 | + |
| 14 | +#include <LightGBM/c_api.h> |
| 15 | +#include <fstream> |
| 16 | +#include <iostream> |
| 17 | +#include <iterator> |
| 18 | +#include <map> |
| 19 | +#include <memory> |
| 20 | +#include <sstream> |
| 21 | +#include <stdexcept> |
| 22 | +#include <string> |
| 23 | +#include <utility> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace WIF { |
| 27 | + |
| 28 | +/** |
| 29 | + * @brief Wrapper class which provides a bridge to LightGBM library |
| 30 | + */ |
| 31 | +class LightGBMWrapper { |
| 32 | +public: |
| 33 | + /** |
| 34 | + * @brief Construct a new LightGBM wrapper object |
| 35 | + */ |
| 36 | + LightGBMWrapper(); |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Construct a new LightGBM wrapper object |
| 40 | + * |
| 41 | + * @param modelPath contains path to the model file |
| 42 | + */ |
| 43 | + LightGBMWrapper(const std::string& modelPath); |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Destruct the LightGBM wrapper object |
| 47 | + */ |
| 48 | + ~LightGBMWrapper(); |
| 49 | + |
| 50 | + /** |
| 51 | + * @brief Set feature IDs which will be used for classification |
| 52 | + * |
| 53 | + * @param sourceFeatureIDs |
| 54 | + */ |
| 55 | + void setFeatureSourceIDs(const std::vector<FeatureID>& sourceFeatureIDs); |
| 56 | + |
| 57 | + /** |
| 58 | + * @brief Getter for path of the used ML model |
| 59 | + * @return const std::string& |
| 60 | + */ |
| 61 | + const std::string& getModelPath() const; |
| 62 | + |
| 63 | + /** |
| 64 | + * @brief Load the model from the file |
| 65 | + * |
| 66 | + * @param modelPath contains path to the model file. |
| 67 | + * @return bool true, if model was succesfully loaded. False if not. |
| 68 | + */ |
| 69 | + bool loadModel(const std::string& modelPath); |
| 70 | + |
| 71 | + /** |
| 72 | + * @brief Classify single flowFeature object |
| 73 | + * See std::vector<ClfResult> classify(const std::vector<FlowFeatures>&) for more details |
| 74 | + * |
| 75 | + * @param flowFeatures flow features to classify |
| 76 | + * @return ClfResult result of the classification, which contains |
| 77 | + * vector<double> with probabilities for each class |
| 78 | + */ |
| 79 | + ClfResult classify(const FlowFeatures& flowFeatures); |
| 80 | + |
| 81 | + /** |
| 82 | + * @brief Classify a burst of flow features |
| 83 | + * |
| 84 | + * @param burstOfFlowsFeatures the burst of flow features to classify |
| 85 | + * @return std::vector<ClfResult> the results of the classification. Each ClfResult contains |
| 86 | + * result of the classification, which contains vector<double> with |
| 87 | + * probabilities for each class |
| 88 | + */ |
| 89 | + std::vector<ClfResult> classify(const std::vector<FlowFeatures>& burstOfFeatures); |
| 90 | + |
| 91 | + /** |
| 92 | + * @brief Return information about if ML model is loaded or not |
| 93 | + * |
| 94 | + * @return bool true, if ML model is loaded. False, if not |
| 95 | + */ |
| 96 | + bool isLoaded() const; |
| 97 | + |
| 98 | + /** |
| 99 | + * @brief Train LightGBM model |
| 100 | + * |
| 101 | + * @param datasetFileName is the name of the file with training data |
| 102 | + * @param datasetParams are the additional parameters of the training dataset |
| 103 | + * @param numOfIterations is the number of training iterations (how many |
| 104 | + * LGBM_BoosterUpdateOneIter function is called, see |
| 105 | + * https://lightgbm.readthedocs.io/en/stable/C-API.html) |
| 106 | + * @param params are parameters in format ‘key1=value1 key2=value2’ (see |
| 107 | + * https://lightgbm.readthedocs.io/en/stable/C-API.html) |
| 108 | + * @param modelFileName name of the file where the trained model will be saved |
| 109 | + */ |
| 110 | + void train( |
| 111 | + const std::string& datasetFileName, |
| 112 | + const char* datasetParams = "header=true label=name:label", |
| 113 | + const unsigned numOfIterations = 100, |
| 114 | + const char* params |
| 115 | + = "boosting_type=gbdt objective=binary metric=auc num_leaves=30 learning_rate=0.05 " |
| 116 | + "feature_fraction=0.9", |
| 117 | + const std::string modelFileName = "model.txt"); |
| 118 | + |
| 119 | +private: |
| 120 | + /** |
| 121 | + * @brief LightGBM model |
| 122 | + */ |
| 123 | + BoosterHandle m_booster = nullptr; |
| 124 | + |
| 125 | + /** |
| 126 | + * @brief Number of iterations of m_booster |
| 127 | + */ |
| 128 | + int m_outNumIterations = 0; |
| 129 | + |
| 130 | + /** |
| 131 | + * @brief Bool value is true, if any ML model is correctly loaded. Otherwise it contains false |
| 132 | + */ |
| 133 | + bool m_isLoaded = false; |
| 134 | + |
| 135 | + /** |
| 136 | + * @brief The path to currently loaded ML model path |
| 137 | + */ |
| 138 | + std::string m_modelPath; |
| 139 | + |
| 140 | + /** |
| 141 | + * @brief Vector of feature IDs, which were set in setFeatureIDs method |
| 142 | + */ |
| 143 | + std::vector<FeatureID> m_featureIDs; |
| 144 | +}; |
| 145 | + |
| 146 | +} // namespace WIF |
0 commit comments