Compare commits

...

8 Commits

22 changed files with 208 additions and 153 deletions

4
.vscode/launch.json vendored
View File

@@ -31,7 +31,9 @@
"--discretize", "--discretize",
"--stratified", "--stratified",
"-d", "-d",
"iris" "glass",
"--hyperparameters",
"{\"repeatSparent\": true, \"maxModels\": 12}"
], ],
"cwd": "/Users/rmontanana/Code/discretizbench", "cwd": "/Users/rmontanana/Code/discretizbench",
}, },

View File

@@ -11,6 +11,16 @@ setup: ## Install dependencies for tests and coverage
pip install gcovr; \ pip install gcovr; \
fi fi
dest ?= ../discretizbench
copy: ## Copy binary files to selected folder
@echo "Destination folder: $(dest)"
make build
@echo ">>> Copying files to $(dest)"
@cp build/src/Platform/main $(dest)
@cp build/src/Platform/list $(dest)
@cp build/src/Platform/manage $(dest)
@echo ">>> Done"
dependency: ## Create a dependency graph diagram of the project (build/dependency.png) dependency: ## Create a dependency graph diagram of the project (build/dependency.png)
cd build && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png cd build && cmake .. --graphviz=dependency.dot && dot -Tpng dependency.dot -o dependency.png

View File

@@ -3,5 +3,6 @@ include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet)
include_directories(${BayesNet_SOURCE_DIR}/lib/Files) include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp) include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include) include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include)
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
add_executable(BayesNetSample sample.cc ${BayesNet_SOURCE_DIR}/src/Platform/Folding.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc) add_executable(BayesNetSample sample.cc ${BayesNet_SOURCE_DIR}/src/Platform/Folding.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
target_link_libraries(BayesNetSample BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}") target_link_libraries(BayesNetSample BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")

View File

@@ -3,6 +3,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include <nlohmann/json.hpp>
#include "ArffFiles.h" #include "ArffFiles.h"
#include "BayesMetrics.h" #include "BayesMetrics.h"
#include "CPPFImdlp.h" #include "CPPFImdlp.h"
@@ -141,111 +142,96 @@ int main(int argc, char** argv)
/* /*
* Begin Processing * Begin Processing
*/ */
auto ypred = torch::tensor({ 1,2,3,2,2,3,4,5,2,1 }); auto handler = ArffFiles();
auto y = torch::tensor({ 0,0,0,0,2,3,4,0,0,0 }); handler.load(complete_file_name, class_last);
auto weights = torch::ones({ 10 }, kDouble); // Get Dataset X, y
auto mask = ypred == y; vector<mdlp::samples_t>& X = handler.getX();
cout << "ypred:" << ypred << endl; mdlp::labels_t& y = handler.getY();
cout << "y:" << y << endl; // Get className & Features
cout << "weights:" << weights << endl; auto className = handler.getClassName();
cout << "mask:" << mask << endl; vector<string> features;
double value_to_add = 0.5; auto attributes = handler.getAttributes();
weights += mask.to(torch::kDouble) * value_to_add; transform(attributes.begin(), attributes.end(), back_inserter(features),
cout << "New weights:" << weights << endl; [](const pair<string, string>& item) { return item.first; });
auto masked_weights = weights * mask.to(weights.dtype()); // Discretize Dataset
double sum_of_weights = masked_weights.sum().item<double>(); auto [Xd, maxes] = discretize(X, y, features);
cout << "Sum of weights: " << sum_of_weights << endl; maxes[className] = *max_element(y.begin(), y.end()) + 1;
//weights.index_put_({ mask }, weights + 10); map<string, vector<int>> states;
// auto handler = ArffFiles(); for (auto feature : features) {
// handler.load(complete_file_name, class_last); states[feature] = vector<int>(maxes[feature]);
// // Get Dataset X, y }
// vector<mdlp::samples_t>& X = handler.getX(); states[className] = vector<int>(maxes[className]);
// mdlp::labels_t& y = handler.getY(); auto clf = platform::Models::instance()->create(model_name);
// // Get className & Features clf->fit(Xd, y, features, className, states);
// auto className = handler.getClassName(); if (dump_cpt) {
// vector<string> features; cout << "--- CPT Tables ---" << endl;
// auto attributes = handler.getAttributes(); clf->dump_cpt();
// transform(attributes.begin(), attributes.end(), back_inserter(features), }
// [](const pair<string, string>& item) { return item.first; }); auto lines = clf->show();
// // Discretize Dataset for (auto line : lines) {
// auto [Xd, maxes] = discretize(X, y, features); cout << line << endl;
// maxes[className] = *max_element(y.begin(), y.end()) + 1; }
// map<string, vector<int>> states; cout << "--- Topological Order ---" << endl;
// for (auto feature : features) { auto order = clf->topological_order();
// states[feature] = vector<int>(maxes[feature]); for (auto name : order) {
// } cout << name << ", ";
// states[className] = vector<int>(maxes[className]); }
// auto clf = platform::Models::instance()->create(model_name); cout << "end." << endl;
// clf->fit(Xd, y, features, className, states); auto score = clf->score(Xd, y);
// if (dump_cpt) { cout << "Score: " << score << endl;
// cout << "--- CPT Tables ---" << endl; auto graph = clf->graph();
// clf->dump_cpt(); auto dot_file = model_name + "_" + file_name;
// } ofstream file(dot_file + ".dot");
// auto lines = clf->show(); file << graph;
// for (auto line : lines) { file.close();
// cout << line << endl; cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << endl;
// } cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << endl;
// cout << "--- Topological Order ---" << endl; string stratified_string = stratified ? " Stratified" : "";
// auto order = clf->topological_order(); cout << nFolds << " Folds" << stratified_string << " Cross validation" << endl;
// for (auto name : order) { cout << "==========================================" << endl;
// cout << name << ", "; torch::Tensor Xt = torch::zeros({ static_cast<int>(Xd.size()), static_cast<int>(Xd[0].size()) }, torch::kInt32);
// } torch::Tensor yt = torch::tensor(y, torch::kInt32);
// cout << "end." << endl; for (int i = 0; i < features.size(); ++i) {
// auto score = clf->score(Xd, y); Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32));
// cout << "Score: " << score << endl; }
// auto graph = clf->graph(); float total_score = 0, total_score_train = 0, score_train, score_test;
// auto dot_file = model_name + "_" + file_name; Fold* fold;
// ofstream file(dot_file + ".dot"); if (stratified)
// file << graph; fold = new StratifiedKFold(nFolds, y, seed);
// file.close(); else
// cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << endl; fold = new KFold(nFolds, y.size(), seed);
// cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << endl; for (auto i = 0; i < nFolds; ++i) {
// string stratified_string = stratified ? " Stratified" : ""; auto [train, test] = fold->getFold(i);
// cout << nFolds << " Folds" << stratified_string << " Cross validation" << endl; cout << "Fold: " << i + 1 << endl;
// cout << "==========================================" << endl; if (tensors) {
// torch::Tensor Xt = torch::zeros({ static_cast<int>(Xd.size()), static_cast<int>(Xd[0].size()) }, torch::kInt32); auto ttrain = torch::tensor(train, torch::kInt64);
// torch::Tensor yt = torch::tensor(y, torch::kInt32); auto ttest = torch::tensor(test, torch::kInt64);
// for (int i = 0; i < features.size(); ++i) { torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain);
// Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32)); torch::Tensor ytraint = yt.index({ ttrain });
// } torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest);
// float total_score = 0, total_score_train = 0, score_train, score_test; torch::Tensor ytestt = yt.index({ ttest });
// Fold* fold; clf->fit(Xtraint, ytraint, features, className, states);
// if (stratified) auto temp = clf->predict(Xtraint);
// fold = new StratifiedKFold(nFolds, y, seed); score_train = clf->score(Xtraint, ytraint);
// else score_test = clf->score(Xtestt, ytestt);
// fold = new KFold(nFolds, y.size(), seed); } else {
// for (auto i = 0; i < nFolds; ++i) { auto [Xtrain, ytrain] = extract_indices(train, Xd, y);
// auto [train, test] = fold->getFold(i); auto [Xtest, ytest] = extract_indices(test, Xd, y);
// cout << "Fold: " << i + 1 << endl; clf->fit(Xtrain, ytrain, features, className, states);
// if (tensors) { score_train = clf->score(Xtrain, ytrain);
// auto ttrain = torch::tensor(train, torch::kInt64); score_test = clf->score(Xtest, ytest);
// auto ttest = torch::tensor(test, torch::kInt64); }
// torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain); if (dump_cpt) {
// torch::Tensor ytraint = yt.index({ ttrain }); cout << "--- CPT Tables ---" << endl;
// torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest); clf->dump_cpt();
// torch::Tensor ytestt = yt.index({ ttest }); }
// clf->fit(Xtraint, ytraint, features, className, states); total_score_train += score_train;
// auto temp = clf->predict(Xtraint); total_score += score_test;
// score_train = clf->score(Xtraint, ytraint); cout << "Score Train: " << score_train << endl;
// score_test = clf->score(Xtestt, ytestt); cout << "Score Test : " << score_test << endl;
// } else { cout << "-------------------------------------------------------------------------------" << endl;
// auto [Xtrain, ytrain] = extract_indices(train, Xd, y); }
// auto [Xtest, ytest] = extract_indices(test, Xd, y); cout << "**********************************************************************************" << endl;
// clf->fit(Xtrain, ytrain, features, className, states); cout << "Average Score Train: " << total_score_train / nFolds << endl;
// score_train = clf->score(Xtrain, ytrain); cout << "Average Score Test : " << total_score / nFolds << endl;return 0;
// score_test = clf->score(Xtest, ytest);
// }
// if (dump_cpt) {
// cout << "--- CPT Tables ---" << endl;
// clf->dump_cpt();
// }
// total_score_train += score_train;
// total_score += score_test;
// cout << "Score Train: " << score_train << endl;
// cout << "Score Test : " << score_test << endl;
// cout << "-------------------------------------------------------------------------------" << endl;
// }
// cout << "**********************************************************************************" << endl;
// cout << "Average Score Train: " << total_score_train / nFolds << endl;
// cout << "Average Score Test : " << total_score / nFolds << endl;return 0;
} }

View File

@@ -10,6 +10,7 @@ namespace bayesnet {
AODE(); AODE();
virtual ~AODE() {}; virtual ~AODE() {};
vector<string> graph(const string& title = "AODE") const override; vector<string> graph(const string& title = "AODE") const override;
void setHyperparameters(nlohmann::json& hyperparameters) override {};
}; };
} }
#endif #endif

View File

@@ -16,6 +16,7 @@ namespace bayesnet {
virtual ~AODELd() = default; virtual ~AODELd() = default;
vector<string> graph(const string& name = "AODE") const override; vector<string> graph(const string& name = "AODE") const override;
static inline string version() { return "0.0.1"; }; static inline string version() { return "0.0.1"; };
void setHyperparameters(nlohmann::json& hyperparameters) override {};
}; };
} }
#endif // !AODELD_H #endif // !AODELD_H

View File

@@ -1,6 +1,7 @@
#ifndef BASE_H #ifndef BASE_H
#define BASE_H #define BASE_H
#include <torch/torch.h> #include <torch/torch.h>
#include <nlohmann/json.hpp>
#include <vector> #include <vector>
namespace bayesnet { namespace bayesnet {
using namespace std; using namespace std;
@@ -27,6 +28,7 @@ namespace bayesnet {
const string inline getVersion() const { return "0.1.0"; }; const string inline getVersion() const { return "0.1.0"; };
vector<string> virtual topological_order() = 0; vector<string> virtual topological_order() = 0;
void virtual dump_cpt()const = 0; void virtual dump_cpt()const = 0;
virtual void setHyperparameters(nlohmann::json& hyperparameters) = 0;
}; };
} }
#endif #endif

View File

@@ -21,25 +21,39 @@ namespace bayesnet {
} }
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32)); samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
} }
vector<int> Metrics::SelectKBestWeighted(const torch::Tensor& weights, unsigned k) vector<int> Metrics::SelectKBestWeighted(const torch::Tensor& weights, bool ascending, unsigned k)
{ {
// Return the K Best features
auto n = samples.size(0) - 1; auto n = samples.size(0) - 1;
if (k == 0) { if (k == 0) {
k = n; k = n;
} }
// compute scores // compute scores
scoresKBest.reserve(n); scoresKBest.clear();
featuresKBest.clear();
auto label = samples.index({ -1, "..." }); auto label = samples.index({ -1, "..." });
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
scoresKBest.push_back(mutualInformation(label, samples.index({ i, "..." }), weights)); scoresKBest.push_back(mutualInformation(label, samples.index({ i, "..." }), weights));
featuresKBest.push_back(i); featuresKBest.push_back(i);
} }
// sort & reduce scores and features // sort & reduce scores and features
if (ascending) {
sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
{ return scoresKBest[i] < scoresKBest[j]; });
sort(scoresKBest.begin(), scoresKBest.end(), std::less<double>());
if (k < n) {
for (int i = 0; i < n - k; ++i) {
featuresKBest.erase(featuresKBest.begin());
scoresKBest.erase(scoresKBest.begin());
}
}
} else {
sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j) sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
{ return scoresKBest[i] > scoresKBest[j]; }); { return scoresKBest[i] > scoresKBest[j]; });
sort(scoresKBest.begin(), scoresKBest.end(), std::greater<double>()); sort(scoresKBest.begin(), scoresKBest.end(), std::greater<double>());
featuresKBest.resize(k); featuresKBest.resize(k);
scoresKBest.resize(k); scoresKBest.resize(k);
}
return featuresKBest; return featuresKBest;
} }
vector<double> Metrics::getScoresKBest() const vector<double> Metrics::getScoresKBest() const

View File

@@ -21,7 +21,7 @@ namespace bayesnet {
Metrics() = default; Metrics() = default;
Metrics(const torch::Tensor& samples, const vector<string>& features, const string& className, const int classNumStates); Metrics(const torch::Tensor& samples, const vector<string>& features, const string& className, const int classNumStates);
Metrics(const vector<vector<int>>& vsamples, const vector<int>& labels, const vector<string>& features, const string& className, const int classNumStates); Metrics(const vector<vector<int>>& vsamples, const vector<int>& labels, const vector<string>& features, const string& className, const int classNumStates);
vector<int> SelectKBestWeighted(const torch::Tensor& weights, unsigned k = 0); vector<int> SelectKBestWeighted(const torch::Tensor& weights, bool ascending=false, unsigned k = 0);
vector<double> getScoresKBest() const; vector<double> getScoresKBest() const;
double mutualInformation(const Tensor& firstFeature, const Tensor& secondFeature, const Tensor& weights); double mutualInformation(const Tensor& firstFeature, const Tensor& secondFeature, const Tensor& weights);
vector<float> conditionalEdgeWeights(vector<float>& weights); // To use in Python vector<float> conditionalEdgeWeights(vector<float>& weights); // To use in Python

View File

@@ -1,4 +1,5 @@
#include "BoostAODE.h" #include "BoostAODE.h"
#include <set>
#include "BayesMetrics.h" #include "BayesMetrics.h"
namespace bayesnet { namespace bayesnet {
@@ -7,40 +8,46 @@ namespace bayesnet {
{ {
// Models shall be built in trainModel // Models shall be built in trainModel
} }
void BoostAODE::setHyperparameters(nlohmann::json& hyperparameters)
{
if (hyperparameters.contains("repeatSparent")) {
repeatSparent = hyperparameters["repeatSparent"];
}
if (hyperparameters.contains("maxModels")) {
maxModels = hyperparameters["maxModels"];
}
if (hyperparameters.contains("ascending")) {
ascending = hyperparameters["ascending"];
}
}
void BoostAODE::trainModel(const torch::Tensor& weights) void BoostAODE::trainModel(const torch::Tensor& weights)
{ {
models.clear(); models.clear();
n_models = 0; n_models = 0;
int max_models = .1 * n > 10 ? .1 * n : n; if (maxModels == 0)
maxModels = .1 * n > 10 ? .1 * n : n;
Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64); Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
auto X_ = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }); auto X_ = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." });
auto y_ = dataset.index({ -1, "..." }); auto y_ = dataset.index({ -1, "..." });
bool exitCondition = false; bool exitCondition = false;
bool repeatSparent = false; unordered_set<int> featuresUsed;
vector<int> featuresUsed;
// Step 0: Set the finish condition // Step 0: Set the finish condition
// if not repeatSparent a finish condition is run out of features // if not repeatSparent a finish condition is run out of features
// n_models == max_models // n_models == maxModels
int numClasses = states[className].size(); int numClasses = states[className].size();
while (!exitCondition) { while (!exitCondition) {
// Step 1: Build ranking with mutual information // Step 1: Build ranking with mutual information
auto featureSelection = metrics.SelectKBestWeighted(weights_, n); // Get all the features sorted auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted
auto feature = featureSelection[0];
unique_ptr<Classifier> model; unique_ptr<Classifier> model;
if (!repeatSparent) { auto feature = featureSelection[0];
if (n_models == 0) { if (!repeatSparent || featuresUsed.size() < featureSelection.size()) {
models.resize(n); // Resize for n==nfeatures SPODEs
significanceModels.resize(n);
}
bool found = false; bool found = false;
for (int i = 0; i < featureSelection.size(); ++i) { for (auto feat : featureSelection) {
if (find(featuresUsed.begin(), featuresUsed.end(), i) != featuresUsed.end()) { if (find(featuresUsed.begin(), featuresUsed.end(), feat) != featuresUsed.end()) {
continue; continue;
} }
found = true; found = true;
feature = i; feature = feat;
featuresUsed.push_back(feature);
n_models++;
break; break;
} }
if (!found) { if (!found) {
@@ -48,7 +55,9 @@ namespace bayesnet {
continue; continue;
} }
} }
featuresUsed.insert(feature);
model = std::make_unique<SPODE>(feature); model = std::make_unique<SPODE>(feature);
n_models++;
model->fit(dataset, features, className, states, weights_); model->fit(dataset, features, className, states, weights_);
auto ypred = model->predict(X_); auto ypred = model->predict(X_);
// Step 3.1: Compute the classifier amout of say // Step 3.1: Compute the classifier amout of say
@@ -63,15 +72,12 @@ namespace bayesnet {
double totalWeights = torch::sum(weights_).item<double>(); double totalWeights = torch::sum(weights_).item<double>();
weights_ = weights_ / totalWeights; weights_ = weights_ / totalWeights;
// Step 3.4: Store classifier and its accuracy to weigh its future vote // Step 3.4: Store classifier and its accuracy to weigh its future vote
if (!repeatSparent) {
models[feature] = std::move(model);
significanceModels[feature] = significance;
} else {
models.push_back(std::move(model)); models.push_back(std::move(model));
significanceModels.push_back(significance); significanceModels.push_back(significance);
n_models++; exitCondition = n_models == maxModels;
} }
exitCondition = n_models == max_models; if (featuresUsed.size() != features.size()) {
cout << "Warning: BoostAODE did not use all the features" << endl;
} }
weights.copy_(weights_); weights.copy_(weights_);
} }

View File

@@ -4,13 +4,18 @@
#include "SPODE.h" #include "SPODE.h"
namespace bayesnet { namespace bayesnet {
class BoostAODE : public Ensemble { class BoostAODE : public Ensemble {
protected:
void buildModel(const torch::Tensor& weights) override;
void trainModel(const torch::Tensor& weights) override;
public: public:
BoostAODE(); BoostAODE();
virtual ~BoostAODE() {}; virtual ~BoostAODE() {};
vector<string> graph(const string& title = "BoostAODE") const override; vector<string> graph(const string& title = "BoostAODE") const override;
void setHyperparameters(nlohmann::json& hyperparameters) override;
protected:
void buildModel(const torch::Tensor& weights) override;
void trainModel(const torch::Tensor& weights) override;
private:
bool repeatSparent=false;
int maxModels=0;
bool ascending=false; //Process KBest features ascending or descending order
}; };
} }
#endif #endif

View File

@@ -1,5 +1,6 @@
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp) include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
include_directories(${BayesNet_SOURCE_DIR}/lib/Files) include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet) include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet)
include_directories(${BayesNet_SOURCE_DIR}/src/Platform) include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc

View File

@@ -16,6 +16,7 @@ namespace bayesnet {
public: public:
explicit KDB(int k, float theta = 0.03); explicit KDB(int k, float theta = 0.03);
virtual ~KDB() {}; virtual ~KDB() {};
void setHyperparameters(nlohmann::json& hyperparameters) override {};
vector<string> graph(const string& name = "KDB") const override; vector<string> graph(const string& name = "KDB") const override;
}; };
} }

View File

@@ -13,6 +13,7 @@ namespace bayesnet {
KDBLd& fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) override; KDBLd& fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) override;
vector<string> graph(const string& name = "KDB") const override; vector<string> graph(const string& name = "KDB") const override;
Tensor predict(Tensor& X) override; Tensor predict(Tensor& X) override;
void setHyperparameters(nlohmann::json& hyperparameters) override {};
static inline string version() { return "0.0.1"; }; static inline string version() { return "0.0.1"; };
}; };
} }

View File

@@ -12,6 +12,7 @@ namespace bayesnet {
explicit SPODE(int root); explicit SPODE(int root);
virtual ~SPODE() {}; virtual ~SPODE() {};
vector<string> graph(const string& name = "SPODE") const override; vector<string> graph(const string& name = "SPODE") const override;
void setHyperparameters(nlohmann::json& hyperparameters) override {};
}; };
} }
#endif #endif

View File

@@ -13,6 +13,7 @@ namespace bayesnet {
SPODELd& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states) override; SPODELd& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states) override;
vector<string> graph(const string& name = "SPODE") const override; vector<string> graph(const string& name = "SPODE") const override;
Tensor predict(Tensor& X) override; Tensor predict(Tensor& X) override;
void setHyperparameters(nlohmann::json& hyperparameters) override {};
static inline string version() { return "0.0.1"; }; static inline string version() { return "0.0.1"; };
}; };
} }

View File

@@ -3,7 +3,6 @@
#include "Classifier.h" #include "Classifier.h"
namespace bayesnet { namespace bayesnet {
using namespace std; using namespace std;
using namespace torch;
class TAN : public Classifier { class TAN : public Classifier {
private: private:
protected: protected:
@@ -12,6 +11,7 @@ namespace bayesnet {
TAN(); TAN();
virtual ~TAN() {}; virtual ~TAN() {};
vector<string> graph(const string& name = "TAN") const override; vector<string> graph(const string& name = "TAN") const override;
void setHyperparameters(nlohmann::json& hyperparameters) override {};
}; };
} }
#endif #endif

View File

@@ -14,6 +14,7 @@ namespace bayesnet {
vector<string> graph(const string& name = "TAN") const override; vector<string> graph(const string& name = "TAN") const override;
Tensor predict(Tensor& X) override; Tensor predict(Tensor& X) override;
static inline string version() { return "0.0.1"; }; static inline string version() { return "0.0.1"; };
void setHyperparameters(nlohmann::json& hyperparameters) override {};
}; };
} }
#endif // !TANLD_H #endif // !TANLD_H

View File

@@ -25,6 +25,7 @@ namespace platform {
oss << std::put_time(timeinfo, "%H:%M:%S"); oss << std::put_time(timeinfo, "%H:%M:%S");
return oss.str(); return oss.str();
} }
Experiment::Experiment() : hyperparameters(json::parse("{}")) {}
string Experiment::get_file_name() string Experiment::get_file_name()
{ {
string result = "results_" + score_name + "_" + model + "_" + platform + "_" + get_date() + "_" + get_time() + "_" + (stratified ? "1" : "0") + ".json"; string result = "results_" + score_name + "_" + model + "_" + platform + "_" + get_date() + "_" + get_time() + "_" + (stratified ? "1" : "0") + ".json";
@@ -124,6 +125,8 @@ namespace platform {
auto result = Result(); auto result = Result();
auto [values, counts] = at::_unique(y); auto [values, counts] = at::_unique(y);
result.setSamples(X.size(1)).setFeatures(X.size(0)).setClasses(values.size(0)); result.setSamples(X.size(1)).setFeatures(X.size(0)).setClasses(values.size(0));
result.setHyperparameters(hyperparameters);
// Initialize results vectors
int nResults = nfolds * static_cast<int>(randomSeeds.size()); int nResults = nfolds * static_cast<int>(randomSeeds.size());
auto accuracy_test = torch::zeros({ nResults }, torch::kFloat64); auto accuracy_test = torch::zeros({ nResults }, torch::kFloat64);
auto accuracy_train = torch::zeros({ nResults }, torch::kFloat64); auto accuracy_train = torch::zeros({ nResults }, torch::kFloat64);
@@ -144,6 +147,10 @@ namespace platform {
for (int nfold = 0; nfold < nfolds; nfold++) { for (int nfold = 0; nfold < nfolds; nfold++) {
auto clf = Models::instance()->create(model); auto clf = Models::instance()->create(model);
setModelVersion(clf->getVersion()); setModelVersion(clf->getVersion());
if (hyperparameters.size() != 0) {
clf->setHyperparameters(hyperparameters);
}
// Split train - test dataset
train_timer.start(); train_timer.start();
auto [train, test] = fold->getFold(nfold); auto [train, test] = fold->getFold(nfold);
auto train_t = torch::tensor(train); auto train_t = torch::tensor(train);
@@ -153,12 +160,14 @@ namespace platform {
auto X_test = X.index({ "...", test_t }); auto X_test = X.index({ "...", test_t });
auto y_test = y.index({ test_t }); auto y_test = y.index({ test_t });
cout << nfold + 1 << ", " << flush; cout << nfold + 1 << ", " << flush;
// Train model
clf->fit(X_train, y_train, features, className, states); clf->fit(X_train, y_train, features, className, states);
nodes[item] = clf->getNumberOfNodes(); nodes[item] = clf->getNumberOfNodes();
edges[item] = clf->getNumberOfEdges(); edges[item] = clf->getNumberOfEdges();
num_states[item] = clf->getNumberOfStates(); num_states[item] = clf->getNumberOfStates();
train_time[item] = train_timer.getDuration(); train_time[item] = train_timer.getDuration();
auto accuracy_train_value = clf->score(X_train, y_train); auto accuracy_train_value = clf->score(X_train, y_train);
// Test model
test_timer.start(); test_timer.start();
auto accuracy_test_value = clf->score(X_test, y_test); auto accuracy_test_value = clf->score(X_test, y_test);
test_time[item] = test_timer.getDuration(); test_time[item] = test_timer.getDuration();

View File

@@ -29,7 +29,8 @@ namespace platform {
}; };
class Result { class Result {
private: private:
string dataset, hyperparameters, model_version; string dataset, model_version;
json hyperparameters;
int samples{ 0 }, features{ 0 }, classes{ 0 }; int samples{ 0 }, features{ 0 }, classes{ 0 };
double score_train{ 0 }, score_test{ 0 }, score_train_std{ 0 }, score_test_std{ 0 }, train_time{ 0 }, train_time_std{ 0 }, test_time{ 0 }, test_time_std{ 0 }; double score_train{ 0 }, score_test{ 0 }, score_train_std{ 0 }, score_test_std{ 0 }, train_time{ 0 }, train_time_std{ 0 }, test_time{ 0 }, test_time_std{ 0 };
float nodes{ 0 }, leaves{ 0 }, depth{ 0 }; float nodes{ 0 }, leaves{ 0 }, depth{ 0 };
@@ -37,7 +38,7 @@ namespace platform {
public: public:
Result() = default; Result() = default;
Result& setDataset(const string& dataset) { this->dataset = dataset; return *this; } Result& setDataset(const string& dataset) { this->dataset = dataset; return *this; }
Result& setHyperparameters(const string& hyperparameters) { this->hyperparameters = hyperparameters; return *this; } Result& setHyperparameters(const json& hyperparameters) { this->hyperparameters = hyperparameters; return *this; }
Result& setSamples(int samples) { this->samples = samples; return *this; } Result& setSamples(int samples) { this->samples = samples; return *this; }
Result& setFeatures(int features) { this->features = features; return *this; } Result& setFeatures(int features) { this->features = features; return *this; }
Result& setClasses(int classes) { this->classes = classes; return *this; } Result& setClasses(int classes) { this->classes = classes; return *this; }
@@ -59,7 +60,7 @@ namespace platform {
const float get_score_train() const { return score_train; } const float get_score_train() const { return score_train; }
float get_score_test() { return score_test; } float get_score_test() { return score_test; }
const string& getDataset() const { return dataset; } const string& getDataset() const { return dataset; }
const string& getHyperparameters() const { return hyperparameters; } const json& getHyperparameters() const { return hyperparameters; }
const int getSamples() const { return samples; } const int getSamples() const { return samples; }
const int getFeatures() const { return features; } const int getFeatures() const { return features; }
const int getClasses() const { return classes; } const int getClasses() const { return classes; }
@@ -85,11 +86,12 @@ namespace platform {
bool discretized{ false }, stratified{ false }; bool discretized{ false }, stratified{ false };
vector<Result> results; vector<Result> results;
vector<int> randomSeeds; vector<int> randomSeeds;
json hyperparameters = "{}";
int nfolds{ 0 }; int nfolds{ 0 };
float duration{ 0 }; float duration{ 0 };
json build_json(); json build_json();
public: public:
Experiment() = default; Experiment();
Experiment& setTitle(const string& title) { this->title = title; return *this; } Experiment& setTitle(const string& title) { this->title = title; return *this; }
Experiment& setModel(const string& model) { this->model = model; return *this; } Experiment& setModel(const string& model) { this->model = model; return *this; }
Experiment& setPlatform(const string& platform) { this->platform = platform; return *this; } Experiment& setPlatform(const string& platform) { this->platform = platform; return *this; }
@@ -103,6 +105,7 @@ namespace platform {
Experiment& addResult(Result result) { results.push_back(result); return *this; } Experiment& addResult(Result result) { results.push_back(result); return *this; }
Experiment& addRandomSeed(int randomSeed) { randomSeeds.push_back(randomSeed); return *this; } Experiment& addRandomSeed(int randomSeed) { randomSeeds.push_back(randomSeed); return *this; }
Experiment& setDuration(float duration) { this->duration = duration; return *this; } Experiment& setDuration(float duration) { this->duration = duration; return *this; }
Experiment& setHyperparameters(const json& hyperparameters) { this->hyperparameters = hyperparameters; return *this; }
string get_file_name(); string get_file_name();
void save(const string& path); void save(const string& path);
void cross_validation(const string& path, const string& fileName); void cross_validation(const string& path, const string& fileName);

View File

@@ -231,6 +231,7 @@ namespace platform {
cout << "No results found!" << endl; cout << "No results found!" << endl;
exit(0); exit(0);
} }
sortDate();
show(); show();
menu(); menu();
cout << "Done!" << endl; cout << "Done!" << endl;

View File

@@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include <argparse/argparse.hpp> #include <argparse/argparse.hpp>
#include <nlohmann/json.hpp>
#include "platformUtils.h" #include "platformUtils.h"
#include "Experiment.h" #include "Experiment.h"
#include "Datasets.h" #include "Datasets.h"
@@ -10,12 +11,14 @@
using namespace std; using namespace std;
using json = nlohmann::json;
argparse::ArgumentParser manageArguments(int argc, char** argv) argparse::ArgumentParser manageArguments(int argc, char** argv)
{ {
auto env = platform::DotEnv(); auto env = platform::DotEnv();
argparse::ArgumentParser program("main"); argparse::ArgumentParser program("main");
program.add_argument("-d", "--dataset").default_value("").help("Dataset file name"); program.add_argument("-d", "--dataset").default_value("").help("Dataset file name");
program.add_argument("--hyperparameters").default_value("{}").help("Hyperparamters passed to the model in Experiment");
program.add_argument("-p", "--path") program.add_argument("-p", "--path")
.help("folder where the data files are located, default") .help("folder where the data files are located, default")
.default_value(string{ platform::Paths::datasets() }); .default_value(string{ platform::Paths::datasets() });
@@ -31,6 +34,7 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
); );
program.add_argument("--title").default_value("").help("Experiment title"); program.add_argument("--title").default_value("").help("Experiment title");
program.add_argument("--discretize").help("Discretize input dataset").default_value((bool)stoi(env.get("discretize"))).implicit_value(true); program.add_argument("--discretize").help("Discretize input dataset").default_value((bool)stoi(env.get("discretize"))).implicit_value(true);
program.add_argument("--save").help("Save result (always save if no dataset is supplied)").default_value(false).implicit_value(true);
program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value((bool)stoi(env.get("stratified"))).implicit_value(true); program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value((bool)stoi(env.get("stratified"))).implicit_value(true);
program.add_argument("-f", "--folds").help("Number of folds").default_value(stoi(env.get("n_folds"))).scan<'i', int>().action([](const string& value) { program.add_argument("-f", "--folds").help("Number of folds").default_value(stoi(env.get("n_folds"))).scan<'i', int>().action([](const string& value) {
try { try {
@@ -59,6 +63,8 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
auto seeds = program.get<vector<int>>("seeds"); auto seeds = program.get<vector<int>>("seeds");
auto complete_file_name = path + file_name + ".arff"; auto complete_file_name = path + file_name + ".arff";
auto title = program.get<string>("title"); auto title = program.get<string>("title");
auto hyperparameters = program.get<string>("hyperparameters");
auto saveResults = program.get<bool>("save");
if (title == "" && file_name == "") { if (title == "" && file_name == "") {
throw runtime_error("title is mandatory if dataset is not provided"); throw runtime_error("title is mandatory if dataset is not provided");
} }
@@ -74,7 +80,6 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
auto program = manageArguments(argc, argv); auto program = manageArguments(argc, argv);
bool saveResults = false;
auto file_name = program.get<string>("dataset"); auto file_name = program.get<string>("dataset");
auto path = program.get<string>("path"); auto path = program.get<string>("path");
auto model_name = program.get<string>("model"); auto model_name = program.get<string>("model");
@@ -82,9 +87,11 @@ int main(int argc, char** argv)
auto stratified = program.get<bool>("stratified"); auto stratified = program.get<bool>("stratified");
auto n_folds = program.get<int>("folds"); auto n_folds = program.get<int>("folds");
auto seeds = program.get<vector<int>>("seeds"); auto seeds = program.get<vector<int>>("seeds");
auto hyperparameters =program.get<string>("hyperparameters");
vector<string> filesToTest; vector<string> filesToTest;
auto datasets = platform::Datasets(path, true, platform::ARFF); auto datasets = platform::Datasets(path, true, platform::ARFF);
auto title = program.get<string>("title"); auto title = program.get<string>("title");
auto saveResults = program.get<bool>("save");
if (file_name != "") { if (file_name != "") {
if (!datasets.isDataset(file_name)) { if (!datasets.isDataset(file_name)) {
cerr << "Dataset " << file_name << " not found" << endl; cerr << "Dataset " << file_name << " not found" << endl;
@@ -106,6 +113,7 @@ int main(int argc, char** argv)
experiment.setTitle(title).setLanguage("cpp").setLanguageVersion("14.0.3"); experiment.setTitle(title).setLanguage("cpp").setLanguageVersion("14.0.3");
experiment.setDiscretized(discretize_dataset).setModel(model_name).setPlatform(env.get("platform")); experiment.setDiscretized(discretize_dataset).setModel(model_name).setPlatform(env.get("platform"));
experiment.setStratified(stratified).setNFolds(n_folds).setScoreName("accuracy"); experiment.setStratified(stratified).setNFolds(n_folds).setScoreName("accuracy");
experiment.setHyperparameters(json::parse(hyperparameters));
for (auto seed : seeds) { for (auto seed : seeds) {
experiment.addRandomSeed(seed); experiment.addRandomSeed(seed);
} }
@@ -113,9 +121,9 @@ int main(int argc, char** argv)
timer.start(); timer.start();
experiment.go(filesToTest, path); experiment.go(filesToTest, path);
experiment.setDuration(timer.getDuration()); experiment.setDuration(timer.getDuration());
if (saveResults) if (saveResults) {
experiment.save(platform::Paths::results()); experiment.save(platform::Paths::results());
else }
experiment.report(); experiment.report();
cout << "Done!" << endl; cout << "Done!" << endl;
return 0; return 0;