First approach to BoostA2DE::trainModel

This commit is contained in:
Ricardo Montañana Gómez 2024-05-16 14:32:59 +02:00
parent 677ec5613d
commit 80043d5181
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE

View File

@ -81,89 +81,84 @@ namespace bayesnet {
// run out of features // run out of features
bool ascending = order_algorithm == Orders.ASC; bool ascending = order_algorithm == Orders.ASC;
std::mt19937 g{ 173 }; std::mt19937 g{ 173 };
std::vector<std::pair<int, int>> pairSelection;
while (!finished) { while (!finished) {
// Step 1: Build ranking with mutual information // Step 1: Build ranking with mutual information
auto pairSelection = metrics.SelectKPairs(weights_, featuresUsed, ascending, 0); // Get all the pairs sorted pairSelection = metrics.SelectKPairs(weights_, featuresUsed, ascending, 0); // Get all the pairs sorted
if (order_algorithm == Orders.RAND) { if (order_algorithm == Orders.RAND) {
std::shuffle(pairSelection.begin(), pairSelection.end(), g); std::shuffle(pairSelection.begin(), pairSelection.end(), g);
} }
// // Remove used features int k = bisection ? pow(2, tolerance) : 1;
// featureSelection.erase(remove_if(begin(featureSelection), end(featureSelection), [&](auto x) int counter = 0; // The model counter of the current pack
// { return std::find(begin(featuresUsed), end(featuresUsed), x) != end(featuresUsed);}), // VLOG_SCOPE_F(1, "counter=%d k=%d featureSelection.size: %zu", counter, k, featureSelection.size());
// end(featureSelection) while (counter++ < k && pairSelection.size() > 0) {
// ); auto feature_pair = pairSelection[0];
// int k = bisection ? pow(2, tolerance) : 1; pairSelection.erase(pairSelection.begin());
// int counter = 0; // The model counter of the current pack std::unique_ptr<Classifier> model;
// VLOG_SCOPE_F(1, "counter=%d k=%d featureSelection.size: %zu", counter, k, featureSelection.size()); model = std::make_unique<SPnDE>(std::vector<int>({ feature_pair.first, feature_pair.second }));
// while (counter++ < k && featureSelection.size() > 0) { model->fit(dataset, features, className, states, weights_);
// auto feature = featureSelection[0]; alpha_t = 0.0;
// featureSelection.erase(featureSelection.begin()); if (!block_update) {
// std::unique_ptr<Classifier> model; auto ypred = model->predict(X_train);
// model = std::make_unique<SPODE>(feature); // Step 3.1: Compute the classifier amout of say
// model->fit(dataset, features, className, states, weights_); std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
// alpha_t = 0.0; }
// if (!block_update) { // Step 3.4: Store classifier and its accuracy to weigh its future vote
// auto ypred = model->predict(X_train); numItemsPack++;
// // Step 3.1: Compute the classifier amout of say models.push_back(std::move(model));
// std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_); significanceModels.push_back(alpha_t);
// } n_models++;
// // Step 3.4: Store classifier and its accuracy to weigh its future vote // VLOG_SCOPE_F(2, "numItemsPack: %d n_models: %d featuresUsed: %zu", numItemsPack, n_models, featuresUsed.size());
// numItemsPack++; }
// featuresUsed.push_back(feature); if (block_update) {
// models.push_back(std::move(model)); std::tie(weights_, alpha_t, finished) = update_weights_block(k, y_train, weights_);
// significanceModels.push_back(alpha_t); }
// n_models++; if (convergence && !finished) {
// VLOG_SCOPE_F(2, "numItemsPack: %d n_models: %d featuresUsed: %zu", numItemsPack, n_models, featuresUsed.size()); auto y_val_predict = predict(X_test);
// } double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0);
// if (block_update) { if (priorAccuracy == 0) {
// std::tie(weights_, alpha_t, finished) = update_weights_block(k, y_train, weights_); priorAccuracy = accuracy;
// } } else {
// if (convergence && !finished) { improvement = accuracy - priorAccuracy;
// auto y_val_predict = predict(X_test); }
// double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0); if (improvement < convergence_threshold) {
// if (priorAccuracy == 0) { // VLOG_SCOPE_F(3, " (improvement<threshold) tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy);
// priorAccuracy = accuracy; tolerance++;
// } else { } else {
// improvement = accuracy - priorAccuracy; // VLOG_SCOPE_F(3, "* (improvement>=threshold) Reset. tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy);
// } tolerance = 0; // Reset the counter if the model performs better
// if (improvement < convergence_threshold) { numItemsPack = 0;
// VLOG_SCOPE_F(3, " (improvement<threshold) tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy); }
// tolerance++; if (convergence_best) {
// } else { // Keep the best accuracy until now as the prior accuracy
// VLOG_SCOPE_F(3, "* (improvement>=threshold) Reset. tolerance: %d numItemsPack: %d improvement: %f prior: %f current: %f", tolerance, numItemsPack, improvement, priorAccuracy, accuracy); priorAccuracy = std::max(accuracy, priorAccuracy);
// tolerance = 0; // Reset the counter if the model performs better } else {
// numItemsPack = 0; // Keep the last accuray obtained as the prior accuracy
// } priorAccuracy = accuracy;
// if (convergence_best) { }
// // Keep the best accuracy until now as the prior accuracy }
// priorAccuracy = std::max(accuracy, priorAccuracy); // VLOG_SCOPE_F(1, "tolerance: %d featuresUsed.size: %zu features.size: %zu", tolerance, featuresUsed.size(), features.size());
// } else { finished = finished || tolerance > maxTolerance || pairSelection.size() == 0;
// // Keep the last accuray obtained as the prior accuracy
// priorAccuracy = accuracy;
// }
// }
// VLOG_SCOPE_F(1, "tolerance: %d featuresUsed.size: %zu features.size: %zu", tolerance, featuresUsed.size(), features.size());
// finished = finished || tolerance > maxTolerance || featuresUsed.size() == features.size();
} }
// if (tolerance > maxTolerance) { if (tolerance > maxTolerance) {
// if (numItemsPack < n_models) { if (numItemsPack < n_models) {
// notes.push_back("Convergence threshold reached & " + std::to_string(numItemsPack) + " models eliminated"); notes.push_back("Convergence threshold reached & " + std::to_string(numItemsPack) + " models eliminated");
// VLOG_SCOPE_F(4, "Convergence threshold reached & %d models eliminated of %d", numItemsPack, n_models); // VLOG_SCOPE_F(4, "Convergence threshold reached & %d models eliminated of %d", numItemsPack, n_models);
// for (int i = 0; i < numItemsPack; ++i) { for (int i = 0; i < numItemsPack; ++i) {
// significanceModels.pop_back(); significanceModels.pop_back();
// models.pop_back(); models.pop_back();
// n_models--; n_models--;
// } }
// } else { } else {
// notes.push_back("Convergence threshold reached & 0 models eliminated"); notes.push_back("Convergence threshold reached & 0 models eliminated");
// VLOG_SCOPE_F(4, "Convergence threshold reached & 0 models eliminated n_models=%d numItemsPack=%d", n_models, numItemsPack); // VLOG_SCOPE_F(4, "Convergence threshold reached & 0 models eliminated n_models=%d numItemsPack=%d", n_models, numItemsPack);
// } }
// } }
// if (featuresUsed.size() != features.size()) { if (pairSelection.size() > 0) {
// notes.push_back("Used features in train: " + std::to_string(featuresUsed.size()) + " of " + std::to_string(features.size())); notes.push_back("Used pairs not used in train: " + std::to_string(pairSelection.size()));
// status = WARNING; status = WARNING;
// } }
// notes.push_back("Number of models: " + std::to_string(n_models)); notes.push_back("Number of models: " + std::to_string(n_models));
} }
std::vector<std::string> BoostA2DE::graph(const std::string& title) const std::vector<std::string> BoostA2DE::graph(const std::string& title) const
{ {