From da2a9696860756974ed128e2e983a87e3677583a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Mon, 8 Apr 2024 23:36:05 +0200 Subject: [PATCH] Create hyperparameter block_update --- bayesnet/ensembles/BoostAODE.cc | 6 +++++- bayesnet/ensembles/BoostAODE.h | 7 ++++--- docs/BoostAODE.md | 8 +++++--- tests/TestBoostAODE.cc | 24 ++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/bayesnet/ensembles/BoostAODE.cc b/bayesnet/ensembles/BoostAODE.cc index 9e4a856..b30b17b 100644 --- a/bayesnet/ensembles/BoostAODE.cc +++ b/bayesnet/ensembles/BoostAODE.cc @@ -16,7 +16,7 @@ namespace bayesnet { { validHyperparameters = { "maxModels", "bisection", "order", "convergence", "threshold", - "select_features", "maxTolerance", "predict_voting" + "select_features", "maxTolerance", "predict_voting", "block_update" }; } @@ -94,6 +94,10 @@ namespace bayesnet { } hyperparameters.erase("select_features"); } + if (hyperparameters.contains("block_update")) { + block_update = hyperparameters["block_update"]; + hyperparameters.erase("block_update"); + } Classifier::setHyperparameters(hyperparameters); } std::tuple update_weights(torch::Tensor& ytrain, torch::Tensor& ypred, torch::Tensor& weights) diff --git a/bayesnet/ensembles/BoostAODE.h b/bayesnet/ensembles/BoostAODE.h index f4091df..3a64296 100644 --- a/bayesnet/ensembles/BoostAODE.h +++ b/bayesnet/ensembles/BoostAODE.h @@ -28,14 +28,15 @@ namespace bayesnet { std::vector initializeModels(); torch::Tensor X_train, y_train, X_test, y_test; // Hyperparameters - bool bisection = false; // if true, use bisection stratety to add k models at once to the ensemble - int maxTolerance = 1; + bool bisection = true; // if true, use bisection stratety to add k models at once to the ensemble + int maxTolerance = 3; std::string order_algorithm; // order to process the KBest features asc, desc, rand - bool convergence = false; //if true, stop when the model does not improve + bool convergence = true; //if true, stop when the model does not improve bool selectFeatures = false; // if true, use feature selection std::string select_features_algorithm = Orders.DESC; // Selected feature selection algorithm FeatureSelect* featureSelector = nullptr; double threshold = -1; + bool block_update = true; }; } #endif \ No newline at end of file diff --git a/docs/BoostAODE.md b/docs/BoostAODE.md index 2ed66bf..557de8b 100644 --- a/docs/BoostAODE.md +++ b/docs/BoostAODE.md @@ -4,13 +4,15 @@ The hyperparameters defined in the algorithm are: -- ***bisection*** (*boolean*): If set to true allows the algorithm to add *k* models at once (as specified in the algorithm) to the ensemble. Default value: *false*. +- ***bisection*** (*boolean*): If set to true allows the algorithm to add *k* models at once (as specified in the algorithm) to the ensemble. Default value: *true*. - ***order*** (*{"asc", "desc", "rand"}*): Sets the order (ascending/descending/random) in which dataset variables will be processed to choose the parents of the *SPODEs*. Default value: *"desc"*. -- ***convergence*** (*boolean*): Sets whether the convergence of the result will be used as a termination condition. If this hyperparameter is set to true, the training dataset passed to the model is divided into two sets, one serving as training data and the other as a test set (so the original test partition will become a validation partition in this case). The partition is made by taking the first partition generated by a process of generating a 5 fold partition with stratification using a predetermined seed. The exit condition used in this *convergence* is that the difference between the accuracy obtained by the current model and that obtained by the previous model is greater than *1e-4*; otherwise, one will be added to the number of models that worsen the result (see next hyperparameter). Default value: *false*. +- ***block_update*** (*boolean*): Sets whether the algorithm will update the weights of the models in blocks. If set to false, the algorithm will update the weights of the models one by one. Default value: *true*. -- ***maxTolerance*** (*int*): Sets the maximum number of models that can worsen the result without constituting a termination condition. Default value: *1*. if ***bisection*** is set to *true*, the value of this hyperparameter will be exponent of base 2 to compute the number of models to insert at once. +- ***convergence*** (*boolean*): Sets whether the convergence of the result will be used as a termination condition. If this hyperparameter is set to true, the training dataset passed to the model is divided into two sets, one serving as training data and the other as a test set (so the original test partition will become a validation partition in this case). The partition is made by taking the first partition generated by a process of generating a 5 fold partition with stratification using a predetermined seed. The exit condition used in this *convergence* is that the difference between the accuracy obtained by the current model and that obtained by the previous model is greater than *1e-4*; otherwise, one will be added to the number of models that worsen the result (see next hyperparameter). Default value: *true*. + +- ***maxTolerance*** (*int*): Sets the maximum number of models that can worsen the result without constituting a termination condition. if ***bisection*** is set to *true*, the value of this hyperparameter will be exponent of base 2 to compute the number of models to insert at once. Default value: *3* - ***select_features*** (*{"IWSS", "FCBF", "CFS", ""}*): Selects the variable selection method to be used to build initial models for the ensemble that will be included without considering any of the other exit conditions. Once the models of the selected variables are built, the algorithm will update the weights using the ensemble and set the significance of all the models built with the same αt. Default value: *""*. diff --git a/tests/TestBoostAODE.cc b/tests/TestBoostAODE.cc index 58c32cd..b3281c7 100644 --- a/tests/TestBoostAODE.cc +++ b/tests/TestBoostAODE.cc @@ -136,6 +136,30 @@ TEST_CASE("Bisection", "[BoostAODE]") {"bisection", true}, {"maxTolerance", 3}, {"convergence", true}, + {"block_update", false}, + }); + clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv); + REQUIRE(clf.getNumberOfNodes() == 217); + REQUIRE(clf.getNumberOfEdges() == 431); + REQUIRE(clf.getNotes().size() == 3); + REQUIRE(clf.getNotes()[0] == "Convergence threshold reached & 15 models eliminated"); + REQUIRE(clf.getNotes()[1] == "Used features in train: 16 of 216"); + REQUIRE(clf.getNotes()[2] == "Number of models: 1"); + auto score = clf.score(raw.Xv, raw.yv); + auto scoret = clf.score(raw.Xt, raw.yt); + REQUIRE(score == Catch::Approx(1.0f).epsilon(raw.epsilon)); + REQUIRE(scoret == Catch::Approx(1.0f).epsilon(raw.epsilon)); +} + +TEST_CASE("Block Update", "[BoostAODE]") +{ + auto clf = bayesnet::BoostAODE(); + auto raw = RawDatasets("mfeat-factors", true); + clf.setHyperparameters({ + {"bisection", true}, + {"block_update", true}, + {"maxTolerance", 3}, + {"convergence", true}, }); clf.fit(raw.Xv, raw.yv, raw.featuresv, raw.classNamev, raw.statesv); REQUIRE(clf.getNumberOfNodes() == 217);