From 86ffdfd6f3c4416a9276a70dd27d4c5b9d54b2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Wed, 23 Aug 2023 23:15:39 +0200 Subject: [PATCH] Add const feature and className to fit models --- src/BayesNet/AODELd.cc | 2 +- src/BayesNet/AODELd.h | 2 +- src/BayesNet/BaseClassifier.h | 8 ++++---- src/BayesNet/BayesMetrics.cc | 1 - src/BayesNet/BoostAODE.cc | 1 - src/BayesNet/Classifier.cc | 10 +++++----- src/BayesNet/Classifier.h | 10 +++++----- src/BayesNet/Ensemble.cc | 2 +- src/BayesNet/KDBLd.cc | 2 +- src/BayesNet/KDBLd.h | 2 +- src/BayesNet/Network.cc | 5 ++--- src/BayesNet/SPODELd.cc | 4 ++-- src/BayesNet/SPODELd.h | 4 ++-- src/BayesNet/TANLd.cc | 2 +- src/BayesNet/TANLd.h | 2 +- src/Platform/ReportExcel.cc | 4 ++-- src/Platform/Results.cc | 8 ++++---- 17 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/BayesNet/AODELd.cc b/src/BayesNet/AODELd.cc index cc842be..439f2d4 100644 --- a/src/BayesNet/AODELd.cc +++ b/src/BayesNet/AODELd.cc @@ -4,7 +4,7 @@ namespace bayesnet { using namespace std; AODELd::AODELd() : Ensemble(), Proposal(dataset, features, className) {} - AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, vector& features_, string className_, map>& states_) + AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector& features_, const string& className_, map>& states_) { // This first part should go in a Classifier method called fit_local_discretization o fit_float... features = features_; diff --git a/src/BayesNet/AODELd.h b/src/BayesNet/AODELd.h index e85c2f6..125d819 100644 --- a/src/BayesNet/AODELd.h +++ b/src/BayesNet/AODELd.h @@ -12,7 +12,7 @@ namespace bayesnet { void buildModel(const torch::Tensor& weights) override; public: AODELd(); - AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, vector& features_, string className_, map>& states_) override; + AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, const vector& features_, const string& className_, map>& states_) override; virtual ~AODELd() = default; vector graph(const string& name = "AODE") const override; static inline string version() { return "0.0.1"; }; diff --git a/src/BayesNet/BaseClassifier.h b/src/BayesNet/BaseClassifier.h index 130276a..5744f62 100644 --- a/src/BayesNet/BaseClassifier.h +++ b/src/BayesNet/BaseClassifier.h @@ -10,11 +10,11 @@ namespace bayesnet { virtual void trainModel(const torch::Tensor& weights) = 0; public: // X is nxm vector, y is nx1 vector - virtual BaseClassifier& fit(vector>& X, vector& y, vector& features, string className, map>& states) = 0; + virtual BaseClassifier& fit(vector>& X, vector& y, const vector& features, const string& className, map>& states) = 0; // X is nxm tensor, y is nx1 tensor - virtual BaseClassifier& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) = 0; - virtual BaseClassifier& fit(torch::Tensor& dataset, vector& features, string className, map>& states) = 0; - virtual BaseClassifier& fit(torch::Tensor& dataset, vector& features, string className, map>& states, const torch::Tensor& weights) = 0; + virtual BaseClassifier& fit(torch::Tensor& X, torch::Tensor& y, const vector& features, const string& className, map>& states) = 0; + virtual BaseClassifier& fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states) = 0; + virtual BaseClassifier& fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states, const torch::Tensor& weights) = 0; virtual ~BaseClassifier() = default; torch::Tensor virtual predict(torch::Tensor& X) = 0; vector virtual predict(vector>& X) = 0; diff --git a/src/BayesNet/BayesMetrics.cc b/src/BayesNet/BayesMetrics.cc index 1ce46e1..0306d44 100644 --- a/src/BayesNet/BayesMetrics.cc +++ b/src/BayesNet/BayesMetrics.cc @@ -77,7 +77,6 @@ namespace bayesnet { auto source = vector(features); source.push_back(className); auto combinations = doCombinations(source); - double totalWeight = weights.sum().item(); // Compute class prior auto margin = torch::zeros({ classNumStates }, torch::kFloat); for (int value = 0; value < classNumStates; ++value) { diff --git a/src/BayesNet/BoostAODE.cc b/src/BayesNet/BoostAODE.cc index a7d5e5c..25e7602 100644 --- a/src/BayesNet/BoostAODE.cc +++ b/src/BayesNet/BoostAODE.cc @@ -37,7 +37,6 @@ namespace bayesnet { // Step 0: Set the finish condition // if not repeatSparent a finish condition is run out of features // n_models == maxModels - int numClasses = states[className].size(); while (!exitCondition) { // Step 1: Build ranking with mutual information auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted diff --git a/src/BayesNet/Classifier.cc b/src/BayesNet/Classifier.cc index db4a63f..1100288 100644 --- a/src/BayesNet/Classifier.cc +++ b/src/BayesNet/Classifier.cc @@ -5,7 +5,7 @@ namespace bayesnet { using namespace torch; Classifier::Classifier(Network model) : model(model), m(0), n(0), metrics(Metrics()), fitted(false) {} - Classifier& Classifier::build(vector& features, string className, map>& states, const torch::Tensor& weights) + Classifier& Classifier::build(const vector& features, const string& className, map>& states, const torch::Tensor& weights) { this->features = features; this->className = className; @@ -39,7 +39,7 @@ namespace bayesnet { model.fit(dataset, weights, features, className, states); } // X is nxm where n is the number of features and m the number of samples - Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) + Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, const vector& features, const string& className, map>& states) { dataset = X; buildDataset(y); @@ -47,7 +47,7 @@ namespace bayesnet { return build(features, className, states, weights); } // X is nxm where n is the number of features and m the number of samples - Classifier& Classifier::fit(vector>& X, vector& y, vector& features, string className, map>& states) + Classifier& Classifier::fit(vector>& X, vector& y, const vector& features, const string& className, map>& states) { dataset = torch::zeros({ static_cast(X.size()), static_cast(X[0].size()) }, kInt32); for (int i = 0; i < X.size(); ++i) { @@ -58,13 +58,13 @@ namespace bayesnet { const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble); return build(features, className, states, weights); } - Classifier& Classifier::fit(torch::Tensor& dataset, vector& features, string className, map>& states) + Classifier& Classifier::fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states) { this->dataset = dataset; const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble); return build(features, className, states, weights); } - Classifier& Classifier::fit(torch::Tensor& dataset, vector& features, string className, map>& states, const torch::Tensor& weights) + Classifier& Classifier::fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states, const torch::Tensor& weights) { this->dataset = dataset; return build(features, className, states, weights); diff --git a/src/BayesNet/Classifier.h b/src/BayesNet/Classifier.h index d27e486..ba5dcbc 100644 --- a/src/BayesNet/Classifier.h +++ b/src/BayesNet/Classifier.h @@ -11,7 +11,7 @@ namespace bayesnet { class Classifier : public BaseClassifier { private: void buildDataset(torch::Tensor& y); - Classifier& build(vector& features, string className, map>& states, const torch::Tensor& weights); + Classifier& build(const vector& features, const string& className, map>& states, const torch::Tensor& weights); protected: bool fitted; int m, n; // m: number of samples, n: number of features @@ -28,10 +28,10 @@ namespace bayesnet { public: Classifier(Network model); virtual ~Classifier() = default; - Classifier& fit(vector>& X, vector& y, vector& features, string className, map>& states) override; - Classifier& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) override; - Classifier& fit(torch::Tensor& dataset, vector& features, string className, map>& states) override; - Classifier& fit(torch::Tensor& dataset, vector& features, string className, map>& states, const torch::Tensor& weights) override; + Classifier& fit(vector>& X, vector& y, const vector& features, const string& className, map>& states) override; + Classifier& fit(torch::Tensor& X, torch::Tensor& y, const vector& features, const string& className, map>& states) override; + Classifier& fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states) override; + Classifier& fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states, const torch::Tensor& weights) override; void addNodes(); int getNumberOfNodes() const override; int getNumberOfEdges() const override; diff --git a/src/BayesNet/Ensemble.cc b/src/BayesNet/Ensemble.cc index 33a11a2..d7ee5b9 100644 --- a/src/BayesNet/Ensemble.cc +++ b/src/BayesNet/Ensemble.cc @@ -3,7 +3,7 @@ namespace bayesnet { using namespace torch; - Ensemble::Ensemble() : Classifier(Network()) {} + Ensemble::Ensemble() : Classifier(Network()), n_models(0) {} void Ensemble::trainModel(const torch::Tensor& weights) { diff --git a/src/BayesNet/KDBLd.cc b/src/BayesNet/KDBLd.cc index c236a1c..172045a 100644 --- a/src/BayesNet/KDBLd.cc +++ b/src/BayesNet/KDBLd.cc @@ -3,7 +3,7 @@ namespace bayesnet { using namespace std; KDBLd::KDBLd(int k) : KDB(k), Proposal(dataset, features, className) {} - KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, vector& features_, string className_, map>& states_) + KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector& features_, const string& className_, map>& states_) { // This first part should go in a Classifier method called fit_local_discretization o fit_float... features = features_; diff --git a/src/BayesNet/KDBLd.h b/src/BayesNet/KDBLd.h index f463c80..c642bad 100644 --- a/src/BayesNet/KDBLd.h +++ b/src/BayesNet/KDBLd.h @@ -10,7 +10,7 @@ namespace bayesnet { public: explicit KDBLd(int k); virtual ~KDBLd() = default; - KDBLd& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) override; + KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const vector& features, const string& className, map>& states) override; vector graph(const string& name = "KDB") const override; Tensor predict(Tensor& X) override; void setHyperparameters(nlohmann::json& hyperparameters) override {}; diff --git a/src/BayesNet/Network.cc b/src/BayesNet/Network.cc index 5753eb8..15fcacc 100644 --- a/src/BayesNet/Network.cc +++ b/src/BayesNet/Network.cc @@ -3,8 +3,8 @@ #include "Network.h" #include "bayesnetUtils.h" namespace bayesnet { - Network::Network() : features(vector()), className(""), classNumStates(0), fitted(false) {} - Network::Network(float maxT) : features(vector()), className(""), classNumStates(0), maxThreads(maxT), fitted(false) {} + Network::Network() : features(vector()), className(""), classNumStates(0), fitted(false), laplaceSmoothing(0) {} + Network::Network(float maxT) : features(vector()), className(""), classNumStates(0), maxThreads(maxT), fitted(false), laplaceSmoothing(0) {} Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), features(other.features), className(other.className), classNumStates(other.getClassNumStates()), maxThreads(other. getmaxThreads()), fitted(other.fitted) { @@ -399,7 +399,6 @@ namespace bayesnet { auto result = features; result.erase(remove(result.begin(), result.end(), className), result.end()); bool ending{ false }; - int idx = 0; while (!ending) { ending = true; for (auto feature : features) { diff --git a/src/BayesNet/SPODELd.cc b/src/BayesNet/SPODELd.cc index 2711c86..eeb6e16 100644 --- a/src/BayesNet/SPODELd.cc +++ b/src/BayesNet/SPODELd.cc @@ -3,7 +3,7 @@ namespace bayesnet { using namespace std; SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {} - SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, vector& features_, string className_, map>& states_) + SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector& features_, const string& className_, map>& states_) { // This first part should go in a Classifier method called fit_local_discretization o fit_float... features = features_; @@ -18,7 +18,7 @@ namespace bayesnet { states = localDiscretizationProposal(states, model); return *this; } - SPODELd& SPODELd::fit(torch::Tensor& dataset, vector& features_, string className_, map>& states_) + SPODELd& SPODELd::fit(torch::Tensor& dataset, const vector& features_, const string& className_, map>& states_) { Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone(); y = dataset.index({ -1, "..." }).clone(); diff --git a/src/BayesNet/SPODELd.h b/src/BayesNet/SPODELd.h index 94957e6..c83a6e9 100644 --- a/src/BayesNet/SPODELd.h +++ b/src/BayesNet/SPODELd.h @@ -9,8 +9,8 @@ namespace bayesnet { public: explicit SPODELd(int root); virtual ~SPODELd() = default; - SPODELd& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) override; - SPODELd& fit(torch::Tensor& dataset, vector& features, string className, map>& states) override; + SPODELd& fit(torch::Tensor& X, torch::Tensor& y, const vector& features, const string& className, map>& states) override; + SPODELd& fit(torch::Tensor& dataset, const vector& features, const string& className, map>& states) override; vector graph(const string& name = "SPODE") const override; Tensor predict(Tensor& X) override; void setHyperparameters(nlohmann::json& hyperparameters) override {}; diff --git a/src/BayesNet/TANLd.cc b/src/BayesNet/TANLd.cc index e0fdebd..b0ceb96 100644 --- a/src/BayesNet/TANLd.cc +++ b/src/BayesNet/TANLd.cc @@ -3,7 +3,7 @@ namespace bayesnet { using namespace std; TANLd::TANLd() : TAN(), Proposal(dataset, features, className) {} - TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, vector& features_, string className_, map>& states_) + TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector& features_, const string& className_, map>& states_) { // This first part should go in a Classifier method called fit_local_discretization o fit_float... features = features_; diff --git a/src/BayesNet/TANLd.h b/src/BayesNet/TANLd.h index 7ee5eb6..205ca6a 100644 --- a/src/BayesNet/TANLd.h +++ b/src/BayesNet/TANLd.h @@ -10,7 +10,7 @@ namespace bayesnet { public: TANLd(); virtual ~TANLd() = default; - TANLd& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) override; + TANLd& fit(torch::Tensor& X, torch::Tensor& y, const vector& features, const string& className, map>& states) override; vector graph(const string& name = "TAN") const override; Tensor predict(Tensor& X) override; static inline string version() { return "0.0.1"; }; diff --git a/src/Platform/ReportExcel.cc b/src/Platform/ReportExcel.cc index bb51dfb..0ed5854 100644 --- a/src/Platform/ReportExcel.cc +++ b/src/Platform/ReportExcel.cc @@ -47,11 +47,11 @@ namespace platform { void ReportExcel::body() { - auto header = vector( + auto head = vector( { "Dataset", "Samples", "Features", "Classes", "Nodes", "Edges", "States", "Score", "Score Std.", "Time", "Time Std.", "Hyperparameters" }); int col = 1; - for (const auto& item : header) { + for (const auto& item : head) { wks.cell(8, col++).value() = item; } int row = 9; diff --git a/src/Platform/Results.cc b/src/Platform/Results.cc index 0440bc1..fc478dc 100644 --- a/src/Platform/Results.cc +++ b/src/Platform/Results.cc @@ -100,11 +100,11 @@ namespace platform { cout << Colors::YELLOW() << "Reporting " << files.at(index).getFilename() << endl; auto data = files.at(index).load(); if (excelReport) { - ReportExcel report(data); - report.show(); + ReportExcel reporter(data); + reporter.show(); } else { - ReportConsole report(data); - report.show(); + ReportConsole reporter(data); + reporter.show(); } } void Results::menu()