BayesNet 1.0.7.
Bayesian Network and basic classifiers Library.
Loading...
Searching...
No Matches
BoostAODE.cc
1// ***************************************************************
2// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
3// SPDX-FileType: SOURCE
4// SPDX-License-Identifier: MIT
5// ***************************************************************
6
7#include <random>
8#include <set>
9#include <limits.h>
10#include <tuple>
11#include "BoostAODE.h"
12#include "bayesnet/classifiers/SPODE.h"
13#include <loguru.hpp>
14#include <loguru.cpp>
15
16namespace bayesnet {
17
18 BoostAODE::BoostAODE(bool predict_voting) : Boost(predict_voting)
19 {
20 }
21 std::vector<int> BoostAODE::initializeModels(const Smoothing_t smoothing)
22 {
23 torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
24 std::vector<int> featuresSelected = featureSelection(weights_);
25 for (const int& feature : featuresSelected) {
26 std::unique_ptr<Classifier> model = std::make_unique<SPODE>(feature);
27 model->fit(dataset, features, className, states, weights_, smoothing);
28 models.push_back(std::move(model));
29 significanceModels.push_back(1.0); // They will be updated later in trainModel
30 n_models++;
31 }
32 notes.push_back("Used features in initialization: " + std::to_string(featuresSelected.size()) + " of " + std::to_string(features.size()) + " with " + select_features_algorithm);
33 return featuresSelected;
34 }
35 void BoostAODE::trainModel(const torch::Tensor& weights, const Smoothing_t smoothing)
36 {
37 //
38 // Logging setup
39 //
40 // loguru::set_thread_name("BoostAODE");
41 // loguru::g_stderr_verbosity = loguru::Verbosity_OFF;
42 // loguru::add_file("boostAODE.log", loguru::Truncate, loguru::Verbosity_MAX);
43
44 // Algorithm based on the adaboost algorithm for classification
45 // as explained in Ensemble methods (Zhi-Hua Zhou, 2012)
46 fitted = true;
47 double alpha_t = 0;
48 torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
49 bool finished = false;
50 std::vector<int> featuresUsed;
51 n_models = 0;
52 if (selectFeatures) {
53 featuresUsed = initializeModels(smoothing);
54 auto ypred = predict(X_train);
55 std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
56 // Update significance of the models
57 for (int i = 0; i < n_models; ++i) {
58 significanceModels.push_back(alpha_t);
59 }
60 // VLOG_SCOPE_F(1, "SelectFeatures. alpha_t: %f n_models: %d", alpha_t, n_models);
61 if (finished) {
62 return;
63 }
64 }
65 int numItemsPack = 0; // The counter of the models inserted in the current pack
66 // Variables to control the accuracy finish condition
67 double priorAccuracy = 0.0;
68 double improvement = 1.0;
69 double convergence_threshold = 1e-4;
70 int tolerance = 0; // number of times the accuracy is lower than the convergence_threshold
71 // Step 0: Set the finish condition
72 // epsilon sub t > 0.5 => inverse the weights policy
73 // validation error is not decreasing
74 // run out of features
75 bool ascending = order_algorithm == Orders.ASC;
76 std::mt19937 g{ 173 };
77 while (!finished) {
78 // Step 1: Build ranking with mutual information
79 auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted
80 if (order_algorithm == Orders.RAND) {
81 std::shuffle(featureSelection.begin(), featureSelection.end(), g);
82 }
83 // Remove used features
84 featureSelection.erase(remove_if(begin(featureSelection), end(featureSelection), [&](auto x)
85 { return std::find(begin(featuresUsed), end(featuresUsed), x) != end(featuresUsed);}),
86 end(featureSelection)
87 );
88 int k = bisection ? pow(2, tolerance) : 1;
89 int counter = 0; // The model counter of the current pack
90 // VLOG_SCOPE_F(1, "counter=%d k=%d featureSelection.size: %zu", counter, k, featureSelection.size());
91 while (counter++ < k && featureSelection.size() > 0) {
92 auto feature = featureSelection[0];
93 featureSelection.erase(featureSelection.begin());
94 std::unique_ptr<Classifier> model;
95 model = std::make_unique<SPODE>(feature);
96 model->fit(dataset, features, className, states, weights_, smoothing);
97 alpha_t = 0.0;
98 if (!block_update) {
99 torch::Tensor ypred;
100 if (alpha_block) {
101 //
102 // Compute the prediction with the current ensemble + model
103 //
104 // Add the model to the ensemble
105 n_models++;
106 models.push_back(std::move(model));
107 significanceModels.push_back(1);
108 // Compute the prediction
109 ypred = predict(X_train);
110 // Remove the model from the ensemble
111 model = std::move(models.back());
112 models.pop_back();
113 significanceModels.pop_back();
114 n_models--;
115 } else {
116 ypred = model->predict(X_train);
117 }
118 // Step 3.1: Compute the classifier amout of say
119 std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
120 }
121 // Step 3.4: Store classifier and its accuracy to weigh its future vote
122 numItemsPack++;
123 featuresUsed.push_back(feature);
124 models.push_back(std::move(model));
125 significanceModels.push_back(alpha_t);
126 n_models++;
127 // VLOG_SCOPE_F(2, "finished: %d numItemsPack: %d n_models: %d featuresUsed: %zu", finished, numItemsPack, n_models, featuresUsed.size());
128 }
129 if (block_update) {
130 std::tie(weights_, alpha_t, finished) = update_weights_block(k, y_train, weights_);
131 }
132 if (convergence && !finished) {
133 auto y_val_predict = predict(X_test);
134 double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0);
135 if (priorAccuracy == 0) {
136 priorAccuracy = accuracy;
137 } else {
138 improvement = accuracy - priorAccuracy;
139 }
140 if (improvement < convergence_threshold) {
141 // VLOG_SCOPE_F(3, " (improvement<threshold) tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy);
142 tolerance++;
143 } else {
144 // VLOG_SCOPE_F(3, "* (improvement>=threshold) Reset. tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy);
145 tolerance = 0; // Reset the counter if the model performs better
146 numItemsPack = 0;
147 }
148 if (convergence_best) {
149 // Keep the best accuracy until now as the prior accuracy
150 priorAccuracy = std::max(accuracy, priorAccuracy);
151 } else {
152 // Keep the last accuray obtained as the prior accuracy
153 priorAccuracy = accuracy;
154 }
155 }
156 // VLOG_SCOPE_F(1, "tolerance: %d featuresUsed.size: %zu features.size: %zu", tolerance, featuresUsed.size(), features.size());
157 finished = finished || tolerance > maxTolerance || featuresUsed.size() == features.size();
158 }
159 if (tolerance > maxTolerance) {
160 if (numItemsPack < n_models) {
161 notes.push_back("Convergence threshold reached & " + std::to_string(numItemsPack) + " models eliminated");
162 // VLOG_SCOPE_F(4, "Convergence threshold reached & %d models eliminated of %d", numItemsPack, n_models);
163 for (int i = 0; i < numItemsPack; ++i) {
164 significanceModels.pop_back();
165 models.pop_back();
166 n_models--;
167 }
168 } else {
169 notes.push_back("Convergence threshold reached & 0 models eliminated");
170 // VLG_SCOPE_F(4, "Convergence threshold reached & 0 models eliminated n_models=%d numItemsPack=%d", n_models, numItemsPack);
171 }
172 }
173 if (featuresUsed.size() != features.size()) {
174 notes.push_back("Used features in train: " + std::to_string(featuresUsed.size()) + " of " + std::to_string(features.size()));
175 status = WARNING;
176 }
177 notes.push_back("Number of models: " + std::to_string(n_models));
178 }
179 std::vector<std::string> BoostAODE::graph(const std::string& title) const
180 {
181 return Ensemble::graph(title);
182 }
183}