12#include "bayesnet/classifiers/SPODE.h"
18 BoostAODE::BoostAODE(
bool predict_voting) : Boost(predict_voting)
21 std::vector<int> BoostAODE::initializeModels(
const Smoothing_t smoothing)
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);
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;
35 void BoostAODE::trainModel(
const torch::Tensor& weights,
const Smoothing_t smoothing)
48 torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
49 bool finished =
false;
50 std::vector<int> featuresUsed;
53 featuresUsed = initializeModels(smoothing);
54 auto ypred = predict(X_train);
55 std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
57 for (
int i = 0; i < n_models; ++i) {
58 significanceModels.push_back(alpha_t);
67 double priorAccuracy = 0.0;
68 double improvement = 1.0;
69 double convergence_threshold = 1e-4;
75 bool ascending = order_algorithm == Orders.ASC;
76 std::mt19937 g{ 173 };
79 auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n);
80 if (order_algorithm == Orders.RAND) {
81 std::shuffle(featureSelection.begin(), featureSelection.end(), g);
84 featureSelection.erase(remove_if(begin(featureSelection), end(featureSelection), [&](
auto x)
85 {
return std::find(begin(featuresUsed), end(featuresUsed), x) != end(featuresUsed);}),
88 int k = bisection ? pow(2, tolerance) : 1;
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);
106 models.push_back(std::move(model));
107 significanceModels.push_back(1);
109 ypred = predict(X_train);
111 model = std::move(models.back());
113 significanceModels.pop_back();
116 ypred = model->predict(X_train);
119 std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
123 featuresUsed.push_back(feature);
124 models.push_back(std::move(model));
125 significanceModels.push_back(alpha_t);
130 std::tie(weights_, alpha_t, finished) = update_weights_block(k, y_train, weights_);
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;
138 improvement = accuracy - priorAccuracy;
140 if (improvement < convergence_threshold) {
148 if (convergence_best) {
150 priorAccuracy = std::max(accuracy, priorAccuracy);
153 priorAccuracy = accuracy;
157 finished = finished || tolerance > maxTolerance || featuresUsed.size() == features.size();
159 if (tolerance > maxTolerance) {
160 if (numItemsPack < n_models) {
161 notes.push_back(
"Convergence threshold reached & " + std::to_string(numItemsPack) +
" models eliminated");
163 for (
int i = 0; i < numItemsPack; ++i) {
164 significanceModels.pop_back();
169 notes.push_back(
"Convergence threshold reached & 0 models eliminated");
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()));
177 notes.push_back(
"Number of models: " + std::to_string(n_models));
179 std::vector<std::string> BoostAODE::graph(
const std::string& title)
const
181 return Ensemble::graph(title);