Compare commits
24 Commits
xlsx
...
501ea0ab4e
Author | SHA1 | Date | |
---|---|---|---|
501ea0ab4e
|
|||
847c6761d7
|
|||
6030885fc3
|
|||
89df7f4db0
|
|||
41257ed566
|
|||
506369e46b
|
|||
d908f389f5
|
|||
5a7c8f1818
|
|||
64fc7bd9dd
|
|||
0b7beda78c
|
|||
05b670dfc0
|
|||
de62d42b74
|
|||
edb957d22e
|
|||
4de5cb4c6c | |||
c35030f137
|
|||
182b07ed90
|
|||
7806f961e2
|
|||
7c3e315ae7
|
|||
284ef6dfd1
|
|||
1c6af619b5
|
|||
86ffdfd6f3
|
|||
d82148079d
|
|||
067430fd1b
|
|||
f5d0d16365 |
31
.clang-uml
Normal file
31
.clang-uml
Normal file
@@ -0,0 +1,31 @@
|
||||
compilation_database_dir: build
|
||||
output_directory: puml
|
||||
diagrams:
|
||||
BayesNet:
|
||||
type: class
|
||||
glob:
|
||||
- src/BayesNet/*.cc
|
||||
- src/Platform/*.cc
|
||||
using_namespace: bayesnet
|
||||
include:
|
||||
namespaces:
|
||||
- bayesnet
|
||||
- platform
|
||||
plantuml:
|
||||
after:
|
||||
- "note left of {{ alias(\"MyProjectMain\") }}: Main class of myproject library."
|
||||
sequence:
|
||||
type: sequence
|
||||
glob:
|
||||
- src/Platform/main.cc
|
||||
combine_free_functions_into_file_participants: true
|
||||
using_namespace:
|
||||
- std
|
||||
- bayesnet
|
||||
- platform
|
||||
include:
|
||||
paths:
|
||||
- src/BayesNet
|
||||
- src/Platform
|
||||
start_from:
|
||||
- function: main(int,const char **)
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -35,3 +35,4 @@ build/
|
||||
*.dSYM/**
|
||||
cmake-build*/**
|
||||
.idea
|
||||
puml/**
|
||||
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -10,3 +10,6 @@
|
||||
[submodule "lib/json"]
|
||||
path = lib/json
|
||||
url = https://github.com/nlohmann/json.git
|
||||
[submodule "lib/openXLSX"]
|
||||
path = lib/openXLSX
|
||||
url = https://github.com/troldal/OpenXLSX.git
|
||||
|
10
.vscode/launch.json
vendored
10
.vscode/launch.json
vendored
@@ -10,7 +10,7 @@
|
||||
"-d",
|
||||
"iris",
|
||||
"-m",
|
||||
"KDB",
|
||||
"TANLd",
|
||||
"-s",
|
||||
"271",
|
||||
"-p",
|
||||
@@ -28,12 +28,12 @@
|
||||
"BoostAODE",
|
||||
"-p",
|
||||
"/Users/rmontanana/Code/discretizbench/datasets",
|
||||
"--discretize",
|
||||
"--stratified",
|
||||
"-d",
|
||||
"glass",
|
||||
"--hyperparameters",
|
||||
"{\"repeatSparent\": true, \"maxModels\": 12}"
|
||||
"mfeat-morphological",
|
||||
"--discretize"
|
||||
// "--hyperparameters",
|
||||
// "{\"repeatSparent\": true, \"maxModels\": 12}"
|
||||
],
|
||||
"cwd": "/Users/rmontanana/Code/discretizbench",
|
||||
},
|
||||
|
@@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(BayesNet
|
||||
VERSION 0.1.0
|
||||
VERSION 0.2.0
|
||||
DESCRIPTION "Bayesian Network and basic classifiers Library."
|
||||
HOMEPAGE_URL "https://github.com/rmontanana/bayesnet"
|
||||
LANGUAGES CXX
|
||||
@@ -30,7 +30,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
|
||||
option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF)
|
||||
option(ENABLE_TESTING "Unit testing build" OFF)
|
||||
option(CODE_COVERAGE "Collect coverage from test library" OFF)
|
||||
|
||||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||
# CMakes modules
|
||||
# --------------
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
||||
@@ -40,8 +40,7 @@ if (CODE_COVERAGE)
|
||||
enable_testing()
|
||||
include(CodeCoverage)
|
||||
MESSAGE("Code coverage enabled")
|
||||
set(CMAKE_C_FLAGS " ${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
|
||||
set(CMAKE_CXX_FLAGS " ${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O0 -g")
|
||||
SET(GCC_COVERAGE_LINK_FLAGS " ${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage")
|
||||
endif (CODE_COVERAGE)
|
||||
|
||||
@@ -75,7 +74,6 @@ file(GLOB Platform_SOURCES CONFIGURE_DEPENDS ${BayesNet_SOURCE_DIR}/src/Platform
|
||||
if (ENABLE_TESTING)
|
||||
MESSAGE("Testing enabled")
|
||||
add_git_submodule("lib/catch2")
|
||||
|
||||
include(CTest)
|
||||
add_subdirectory(tests)
|
||||
endif (ENABLE_TESTING)
|
||||
|
5
Makefile
5
Makefile
@@ -32,12 +32,15 @@ clean: ## Clean the debug info
|
||||
find . -name "*.gcda" -print0 | xargs -0 rm
|
||||
@echo ">>> Done";
|
||||
|
||||
clang-uml: ## Create uml class and sequence diagrams
|
||||
clang-uml -p --add-compile-flag -I /usr/lib/gcc/x86_64-redhat-linux/8/include/
|
||||
|
||||
debug: ## Build a debug version of the project
|
||||
@echo ">>> Building Debug BayesNet ...";
|
||||
@if [ -d ./build ]; then rm -rf ./build; fi
|
||||
@mkdir build;
|
||||
cmake -S . -B build -D CMAKE_BUILD_TYPE=Debug -D ENABLE_TESTING=ON -D CODE_COVERAGE=ON; \
|
||||
cmake --build build -j 32;
|
||||
cmake --build build -t main -t BayesNetSample -t manage -t list unit_tests -j 32;
|
||||
@echo ">>> Done";
|
||||
|
||||
release: ## Build a Release version of the project
|
||||
|
12
TAN_iris.dot
12
TAN_iris.dot
@@ -1,12 +0,0 @@
|
||||
digraph BayesNet {
|
||||
label=<BayesNet >
|
||||
fontsize=30
|
||||
fontcolor=blue
|
||||
labelloc=t
|
||||
layout=circo
|
||||
class [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ]
|
||||
class -> sepallength class -> sepalwidth class -> petallength class -> petalwidth petallength [shape=circle]
|
||||
petallength -> sepallength petalwidth [shape=circle]
|
||||
sepallength [shape=circle]
|
||||
sepallength -> sepalwidth sepalwidth [shape=circle]
|
||||
sepalwidth -> petalwidth }
|
@@ -1 +0,0 @@
|
||||
null
|
BIN
diagrams/BayesNet.pdf
Executable file
BIN
diagrams/BayesNet.pdf
Executable file
Binary file not shown.
382
sample/sample.cc
382
sample/sample.cc
@@ -10,7 +10,7 @@
|
||||
#include "Folding.h"
|
||||
#include "Models.h"
|
||||
#include "modelRegister.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -58,180 +58,226 @@ pair<vector<vector<int>>, vector<int>> extract_indices(vector<int> indices, vect
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
map<string, bool> datasets = {
|
||||
{"diabetes", true},
|
||||
{"ecoli", true},
|
||||
{"glass", true},
|
||||
{"iris", true},
|
||||
{"kdd_JapaneseVowels", false},
|
||||
{"letter", true},
|
||||
{"liver-disorders", true},
|
||||
{"mfeat-factors", true},
|
||||
};
|
||||
auto valid_datasets = vector<string>();
|
||||
transform(datasets.begin(), datasets.end(), back_inserter(valid_datasets),
|
||||
[](const pair<string, bool>& pair) { return pair.first; });
|
||||
argparse::ArgumentParser program("BayesNetSample");
|
||||
program.add_argument("-d", "--dataset")
|
||||
.help("Dataset file name")
|
||||
.action([valid_datasets](const std::string& value) {
|
||||
if (find(valid_datasets.begin(), valid_datasets.end(), value) != valid_datasets.end()) {
|
||||
return value;
|
||||
torch::Tensor weights_ = torch::full({ 10 }, 1.0 / 10, torch::kFloat64);
|
||||
torch::Tensor y_ = torch::tensor({ 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, torch::kInt32);
|
||||
torch::Tensor ypred = torch::tensor({ 1, 1, 1, 0, 0, 1, 1, 1, 1, 0 }, torch::kInt32);
|
||||
cout << "Initial weights_: " << endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cout << weights_.index({ i }).item<double>() << ", ";
|
||||
}
|
||||
throw runtime_error("file must be one of {diabetes, ecoli, glass, iris, kdd_JapaneseVowels, letter, liver-disorders, mfeat-factors}");
|
||||
cout << "end." << endl;
|
||||
cout << "y_: " << endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cout << y_.index({ i }).item<int>() << ", ";
|
||||
}
|
||||
);
|
||||
program.add_argument("-p", "--path")
|
||||
.help(" folder where the data files are located, default")
|
||||
.default_value(string{ PATH }
|
||||
);
|
||||
program.add_argument("-m", "--model")
|
||||
.help("Model to use " + platform::Models::instance()->toString())
|
||||
.action([](const std::string& value) {
|
||||
static const vector<string> choices = platform::Models::instance()->getNames();
|
||||
if (find(choices.begin(), choices.end(), value) != choices.end()) {
|
||||
return value;
|
||||
cout << "end." << endl;
|
||||
cout << "ypred: " << endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cout << ypred.index({ i }).item<int>() << ", ";
|
||||
}
|
||||
throw runtime_error("Model must be one of " + platform::Models::instance()->toString());
|
||||
cout << "end." << endl;
|
||||
auto mask_wrong = ypred != y_;
|
||||
auto mask_right = ypred == y_;
|
||||
auto masked_weights = weights_ * mask_wrong.to(weights_.dtype());
|
||||
double epsilon_t = masked_weights.sum().item<double>();
|
||||
cout << "epsilon_t: " << epsilon_t << endl;
|
||||
double wt = (1 - epsilon_t) / epsilon_t;
|
||||
cout << "wt: " << wt << endl;
|
||||
double alpha_t = epsilon_t == 0 ? 1 : 0.5 * log(wt);
|
||||
cout << "alpha_t: " << alpha_t << endl;
|
||||
// Step 3.2: Update weights for next classifier
|
||||
// Step 3.2.1: Update weights of wrong samples
|
||||
cout << "exp(alpha_t): " << exp(alpha_t) << endl;
|
||||
cout << "exp(-alpha_t): " << exp(-alpha_t) << endl;
|
||||
weights_ += mask_wrong.to(weights_.dtype()) * exp(alpha_t) * weights_;
|
||||
// Step 3.2.2: Update weights of right samples
|
||||
weights_ += mask_right.to(weights_.dtype()) * exp(-alpha_t) * weights_;
|
||||
// Step 3.3: Normalise the weights
|
||||
double totalWeights = torch::sum(weights_).item<double>();
|
||||
cout << "totalWeights: " << totalWeights << endl;
|
||||
cout << "Before normalization: " << endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cout << weights_.index({ i }).item<double>() << endl;
|
||||
}
|
||||
);
|
||||
program.add_argument("--discretize").help("Discretize input dataset").default_value(false).implicit_value(true);
|
||||
program.add_argument("--dumpcpt").help("Dump CPT Tables").default_value(false).implicit_value(true);
|
||||
program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value(false).implicit_value(true);
|
||||
program.add_argument("--tensors").help("Use tensors to store samples").default_value(false).implicit_value(true);
|
||||
program.add_argument("-f", "--folds").help("Number of folds").default_value(5).scan<'i', int>().action([](const string& value) {
|
||||
try {
|
||||
auto k = stoi(value);
|
||||
if (k < 2) {
|
||||
throw runtime_error("Number of folds must be greater than 1");
|
||||
}
|
||||
return k;
|
||||
}
|
||||
catch (const runtime_error& err) {
|
||||
throw runtime_error(err.what());
|
||||
}
|
||||
catch (...) {
|
||||
throw runtime_error("Number of folds must be an integer");
|
||||
}});
|
||||
program.add_argument("-s", "--seed").help("Random seed").default_value(-1).scan<'i', int>();
|
||||
bool class_last, stratified, tensors, dump_cpt;
|
||||
string model_name, file_name, path, complete_file_name;
|
||||
int nFolds, seed;
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
file_name = program.get<string>("dataset");
|
||||
path = program.get<string>("path");
|
||||
model_name = program.get<string>("model");
|
||||
complete_file_name = path + file_name + ".arff";
|
||||
stratified = program.get<bool>("stratified");
|
||||
tensors = program.get<bool>("tensors");
|
||||
nFolds = program.get<int>("folds");
|
||||
seed = program.get<int>("seed");
|
||||
dump_cpt = program.get<bool>("dumpcpt");
|
||||
class_last = datasets[file_name];
|
||||
if (!file_exists(complete_file_name)) {
|
||||
throw runtime_error("Data File " + path + file_name + ".arff" + " does not exist");
|
||||
}
|
||||
}
|
||||
catch (const exception& err) {
|
||||
cerr << err.what() << endl;
|
||||
cerr << program;
|
||||
exit(1);
|
||||
weights_ = weights_ / totalWeights;
|
||||
cout << "After normalization: " << endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
cout << weights_.index({ i }).item<double>() << endl;
|
||||
}
|
||||
// map<string, bool> datasets = {
|
||||
// {"diabetes", true},
|
||||
// {"ecoli", true},
|
||||
// {"glass", true},
|
||||
// {"iris", true},
|
||||
// {"kdd_JapaneseVowels", false},
|
||||
// {"letter", true},
|
||||
// {"liver-disorders", true},
|
||||
// {"mfeat-factors", true},
|
||||
// };
|
||||
// auto valid_datasets = vector<string>();
|
||||
// transform(datasets.begin(), datasets.end(), back_inserter(valid_datasets),
|
||||
// [](const pair<string, bool>& pair) { return pair.first; });
|
||||
// argparse::ArgumentParser program("BayesNetSample");
|
||||
// program.add_argument("-d", "--dataset")
|
||||
// .help("Dataset file name")
|
||||
// .action([valid_datasets](const std::string& value) {
|
||||
// if (find(valid_datasets.begin(), valid_datasets.end(), value) != valid_datasets.end()) {
|
||||
// return value;
|
||||
// }
|
||||
// throw runtime_error("file must be one of {diabetes, ecoli, glass, iris, kdd_JapaneseVowels, letter, liver-disorders, mfeat-factors}");
|
||||
// }
|
||||
// );
|
||||
// program.add_argument("-p", "--path")
|
||||
// .help(" folder where the data files are located, default")
|
||||
// .default_value(string{ PATH }
|
||||
// );
|
||||
// program.add_argument("-m", "--model")
|
||||
// .help("Model to use " + platform::Models::instance()->toString())
|
||||
// .action([](const std::string& value) {
|
||||
// static const vector<string> choices = platform::Models::instance()->getNames();
|
||||
// if (find(choices.begin(), choices.end(), value) != choices.end()) {
|
||||
// return value;
|
||||
// }
|
||||
// throw runtime_error("Model must be one of " + platform::Models::instance()->toString());
|
||||
// }
|
||||
// );
|
||||
// program.add_argument("--discretize").help("Discretize input dataset").default_value(false).implicit_value(true);
|
||||
// program.add_argument("--dumpcpt").help("Dump CPT Tables").default_value(false).implicit_value(true);
|
||||
// program.add_argument("--stratified").help("If Stratified KFold is to be done").default_value(false).implicit_value(true);
|
||||
// program.add_argument("--tensors").help("Use tensors to store samples").default_value(false).implicit_value(true);
|
||||
// program.add_argument("-f", "--folds").help("Number of folds").default_value(5).scan<'i', int>().action([](const string& value) {
|
||||
// try {
|
||||
// auto k = stoi(value);
|
||||
// if (k < 2) {
|
||||
// throw runtime_error("Number of folds must be greater than 1");
|
||||
// }
|
||||
// return k;
|
||||
// }
|
||||
// catch (const runtime_error& err) {
|
||||
// throw runtime_error(err.what());
|
||||
// }
|
||||
// catch (...) {
|
||||
// throw runtime_error("Number of folds must be an integer");
|
||||
// }});
|
||||
// program.add_argument("-s", "--seed").help("Random seed").default_value(-1).scan<'i', int>();
|
||||
// bool class_last, stratified, tensors, dump_cpt;
|
||||
// string model_name, file_name, path, complete_file_name;
|
||||
// int nFolds, seed;
|
||||
// try {
|
||||
// program.parse_args(argc, argv);
|
||||
// file_name = program.get<string>("dataset");
|
||||
// path = program.get<string>("path");
|
||||
// model_name = program.get<string>("model");
|
||||
// complete_file_name = path + file_name + ".arff";
|
||||
// stratified = program.get<bool>("stratified");
|
||||
// tensors = program.get<bool>("tensors");
|
||||
// nFolds = program.get<int>("folds");
|
||||
// seed = program.get<int>("seed");
|
||||
// dump_cpt = program.get<bool>("dumpcpt");
|
||||
// class_last = datasets[file_name];
|
||||
// if (!file_exists(complete_file_name)) {
|
||||
// throw runtime_error("Data File " + path + file_name + ".arff" + " does not exist");
|
||||
// }
|
||||
// }
|
||||
// catch (const exception& err) {
|
||||
// cerr << err.what() << endl;
|
||||
// cerr << program;
|
||||
// exit(1);
|
||||
// }
|
||||
|
||||
/*
|
||||
* Begin Processing
|
||||
*/
|
||||
auto handler = ArffFiles();
|
||||
handler.load(complete_file_name, class_last);
|
||||
// Get Dataset X, y
|
||||
vector<mdlp::samples_t>& X = handler.getX();
|
||||
mdlp::labels_t& y = handler.getY();
|
||||
// Get className & Features
|
||||
auto className = handler.getClassName();
|
||||
vector<string> features;
|
||||
auto attributes = handler.getAttributes();
|
||||
transform(attributes.begin(), attributes.end(), back_inserter(features),
|
||||
[](const pair<string, string>& item) { return item.first; });
|
||||
// Discretize Dataset
|
||||
auto [Xd, maxes] = discretize(X, y, features);
|
||||
maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
||||
map<string, vector<int>> states;
|
||||
for (auto feature : features) {
|
||||
states[feature] = vector<int>(maxes[feature]);
|
||||
}
|
||||
states[className] = vector<int>(maxes[className]);
|
||||
auto clf = platform::Models::instance()->create(model_name);
|
||||
clf->fit(Xd, y, features, className, states);
|
||||
if (dump_cpt) {
|
||||
cout << "--- CPT Tables ---" << endl;
|
||||
clf->dump_cpt();
|
||||
}
|
||||
auto lines = clf->show();
|
||||
for (auto line : lines) {
|
||||
cout << line << endl;
|
||||
}
|
||||
cout << "--- Topological Order ---" << endl;
|
||||
auto order = clf->topological_order();
|
||||
for (auto name : order) {
|
||||
cout << name << ", ";
|
||||
}
|
||||
cout << "end." << endl;
|
||||
auto score = clf->score(Xd, y);
|
||||
cout << "Score: " << score << endl;
|
||||
auto graph = clf->graph();
|
||||
auto dot_file = model_name + "_" + file_name;
|
||||
ofstream file(dot_file + ".dot");
|
||||
file << graph;
|
||||
file.close();
|
||||
cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << endl;
|
||||
cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << endl;
|
||||
string stratified_string = stratified ? " Stratified" : "";
|
||||
cout << nFolds << " Folds" << stratified_string << " Cross validation" << endl;
|
||||
cout << "==========================================" << endl;
|
||||
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);
|
||||
for (int i = 0; i < features.size(); ++i) {
|
||||
Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32));
|
||||
}
|
||||
float total_score = 0, total_score_train = 0, score_train, score_test;
|
||||
Fold* fold;
|
||||
if (stratified)
|
||||
fold = new StratifiedKFold(nFolds, y, seed);
|
||||
else
|
||||
fold = new KFold(nFolds, y.size(), seed);
|
||||
for (auto i = 0; i < nFolds; ++i) {
|
||||
auto [train, test] = fold->getFold(i);
|
||||
cout << "Fold: " << i + 1 << endl;
|
||||
if (tensors) {
|
||||
auto ttrain = torch::tensor(train, torch::kInt64);
|
||||
auto ttest = torch::tensor(test, torch::kInt64);
|
||||
torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain);
|
||||
torch::Tensor ytraint = yt.index({ ttrain });
|
||||
torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest);
|
||||
torch::Tensor ytestt = yt.index({ ttest });
|
||||
clf->fit(Xtraint, ytraint, features, className, states);
|
||||
auto temp = clf->predict(Xtraint);
|
||||
score_train = clf->score(Xtraint, ytraint);
|
||||
score_test = clf->score(Xtestt, ytestt);
|
||||
} else {
|
||||
auto [Xtrain, ytrain] = extract_indices(train, Xd, y);
|
||||
auto [Xtest, ytest] = extract_indices(test, Xd, y);
|
||||
clf->fit(Xtrain, ytrain, features, className, states);
|
||||
score_train = clf->score(Xtrain, ytrain);
|
||||
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;
|
||||
// auto handler = ArffFiles();
|
||||
// handler.load(complete_file_name, class_last);
|
||||
// // Get Dataset X, y
|
||||
// vector<mdlp::samples_t>& X = handler.getX();
|
||||
// mdlp::labels_t& y = handler.getY();
|
||||
// // Get className & Features
|
||||
// auto className = handler.getClassName();
|
||||
// vector<string> features;
|
||||
// auto attributes = handler.getAttributes();
|
||||
// transform(attributes.begin(), attributes.end(), back_inserter(features),
|
||||
// [](const pair<string, string>& item) { return item.first; });
|
||||
// // Discretize Dataset
|
||||
// auto [Xd, maxes] = discretize(X, y, features);
|
||||
// maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
||||
// map<string, vector<int>> states;
|
||||
// for (auto feature : features) {
|
||||
// states[feature] = vector<int>(maxes[feature]);
|
||||
// }
|
||||
// states[className] = vector<int>(maxes[className]);
|
||||
// auto clf = platform::Models::instance()->create(model_name);
|
||||
// clf->fit(Xd, y, features, className, states);
|
||||
// if (dump_cpt) {
|
||||
// cout << "--- CPT Tables ---" << endl;
|
||||
// clf->dump_cpt();
|
||||
// }
|
||||
// auto lines = clf->show();
|
||||
// for (auto line : lines) {
|
||||
// cout << line << endl;
|
||||
// }
|
||||
// cout << "--- Topological Order ---" << endl;
|
||||
// auto order = clf->topological_order();
|
||||
// for (auto name : order) {
|
||||
// cout << name << ", ";
|
||||
// }
|
||||
// cout << "end." << endl;
|
||||
// auto score = clf->score(Xd, y);
|
||||
// cout << "Score: " << score << endl;
|
||||
// auto graph = clf->graph();
|
||||
// auto dot_file = model_name + "_" + file_name;
|
||||
// ofstream file(dot_file + ".dot");
|
||||
// file << graph;
|
||||
// file.close();
|
||||
// cout << "Graph saved in " << model_name << "_" << file_name << ".dot" << endl;
|
||||
// cout << "dot -Tpng -o " + dot_file + ".png " + dot_file + ".dot " << endl;
|
||||
// string stratified_string = stratified ? " Stratified" : "";
|
||||
// cout << nFolds << " Folds" << stratified_string << " Cross validation" << endl;
|
||||
// cout << "==========================================" << endl;
|
||||
// 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);
|
||||
// for (int i = 0; i < features.size(); ++i) {
|
||||
// Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32));
|
||||
// }
|
||||
// float total_score = 0, total_score_train = 0, score_train, score_test;
|
||||
// platform::Fold* fold;
|
||||
// if (stratified)
|
||||
// fold = new platform::StratifiedKFold(nFolds, y, seed);
|
||||
// else
|
||||
// fold = new platform::KFold(nFolds, y.size(), seed);
|
||||
// for (auto i = 0; i < nFolds; ++i) {
|
||||
// auto [train, test] = fold->getFold(i);
|
||||
// cout << "Fold: " << i + 1 << endl;
|
||||
// if (tensors) {
|
||||
// auto ttrain = torch::tensor(train, torch::kInt64);
|
||||
// auto ttest = torch::tensor(test, torch::kInt64);
|
||||
// torch::Tensor Xtraint = torch::index_select(Xt, 1, ttrain);
|
||||
// torch::Tensor ytraint = yt.index({ ttrain });
|
||||
// torch::Tensor Xtestt = torch::index_select(Xt, 1, ttest);
|
||||
// torch::Tensor ytestt = yt.index({ ttest });
|
||||
// clf->fit(Xtraint, ytraint, features, className, states);
|
||||
// auto temp = clf->predict(Xtraint);
|
||||
// score_train = clf->score(Xtraint, ytraint);
|
||||
// score_test = clf->score(Xtestt, ytestt);
|
||||
// } else {
|
||||
// auto [Xtrain, ytrain] = extract_indices(train, Xd, y);
|
||||
// auto [Xtest, ytest] = extract_indices(test, Xd, y);
|
||||
// clf->fit(Xtrain, ytrain, features, className, states);
|
||||
// score_train = clf->score(Xtrain, ytrain);
|
||||
// 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;
|
||||
}
|
@@ -10,7 +10,6 @@ namespace bayesnet {
|
||||
AODE();
|
||||
virtual ~AODE() {};
|
||||
vector<string> graph(const string& title = "AODE") const override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -4,9 +4,9 @@
|
||||
namespace bayesnet {
|
||||
using namespace std;
|
||||
AODELd::AODELd() : Ensemble(), Proposal(dataset, features, className) {}
|
||||
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, vector<string>& features_, string className_, map<string, vector<int>>& states_)
|
||||
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||
{
|
||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
||||
checkInput(X_, y_);
|
||||
features = features_;
|
||||
className = className_;
|
||||
Xf = X_;
|
||||
@@ -26,6 +26,7 @@ namespace bayesnet {
|
||||
models.push_back(std::make_unique<SPODELd>(i));
|
||||
}
|
||||
n_models = models.size();
|
||||
significanceModels = vector<double>(n_models, 1.0);
|
||||
}
|
||||
void AODELd::trainModel(const torch::Tensor& weights)
|
||||
{
|
||||
|
@@ -12,11 +12,10 @@ namespace bayesnet {
|
||||
void buildModel(const torch::Tensor& weights) override;
|
||||
public:
|
||||
AODELd();
|
||||
AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, vector<string>& features_, string className_, map<string, vector<int>>& states_) override;
|
||||
AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_) override;
|
||||
virtual ~AODELd() = default;
|
||||
vector<string> graph(const string& name = "AODE") const override;
|
||||
vector<string> graph(const string& name = "AODELd") const override;
|
||||
static inline string version() { return "0.0.1"; };
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
};
|
||||
}
|
||||
#endif // !AODELD_H
|
@@ -5,19 +5,21 @@
|
||||
#include <vector>
|
||||
namespace bayesnet {
|
||||
using namespace std;
|
||||
enum status_t { NORMAL, WARNING, ERROR };
|
||||
class BaseClassifier {
|
||||
protected:
|
||||
virtual void trainModel(const torch::Tensor& weights) = 0;
|
||||
public:
|
||||
// X is nxm vector, y is nx1 vector
|
||||
virtual BaseClassifier& fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states) = 0;
|
||||
virtual BaseClassifier& fit(vector<vector<int>>& X, vector<int>& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) = 0;
|
||||
// X is nxm tensor, y is nx1 tensor
|
||||
virtual BaseClassifier& fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) = 0;
|
||||
virtual BaseClassifier& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states) = 0;
|
||||
virtual BaseClassifier& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights) = 0;
|
||||
virtual BaseClassifier& fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) = 0;
|
||||
virtual BaseClassifier& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states) = 0;
|
||||
virtual BaseClassifier& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states, const torch::Tensor& weights) = 0;
|
||||
virtual ~BaseClassifier() = default;
|
||||
torch::Tensor virtual predict(torch::Tensor& X) = 0;
|
||||
vector<int> virtual predict(vector<vector<int>>& X) = 0;
|
||||
status_t virtual getStatus() const = 0;
|
||||
float virtual score(vector<vector<int>>& X, vector<int>& y) = 0;
|
||||
float virtual score(torch::Tensor& X, torch::Tensor& y) = 0;
|
||||
int virtual getNumberOfNodes()const = 0;
|
||||
@@ -25,7 +27,7 @@ namespace bayesnet {
|
||||
int virtual getNumberOfStates() const = 0;
|
||||
vector<string> virtual show() const = 0;
|
||||
vector<string> virtual graph(const string& title = "") const = 0;
|
||||
const string inline getVersion() const { return "0.1.0"; };
|
||||
const string inline getVersion() const { return "0.2.0"; };
|
||||
vector<string> virtual topological_order() = 0;
|
||||
void virtual dump_cpt()const = 0;
|
||||
virtual void setHyperparameters(nlohmann::json& hyperparameters) = 0;
|
||||
|
@@ -77,7 +77,6 @@ namespace bayesnet {
|
||||
auto source = vector<string>(features);
|
||||
source.push_back(className);
|
||||
auto combinations = doCombinations(source);
|
||||
double totalWeight = weights.sum().item<double>();
|
||||
// Compute class prior
|
||||
auto margin = torch::zeros({ classNumStates }, torch::kFloat);
|
||||
for (int value = 0; value < classNumStates; ++value) {
|
||||
|
@@ -1,6 +1,9 @@
|
||||
#include "BoostAODE.h"
|
||||
#include <set>
|
||||
#include "BayesMetrics.h"
|
||||
#include "Colors.h"
|
||||
#include "Folding.h"
|
||||
#include <limits.h>
|
||||
|
||||
namespace bayesnet {
|
||||
BoostAODE::BoostAODE() : Ensemble() {}
|
||||
@@ -11,7 +14,7 @@ namespace bayesnet {
|
||||
void BoostAODE::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid
|
||||
const vector<string> validKeys = { "repeatSparent", "maxModels", "ascending" };
|
||||
const vector<string> validKeys = { "repeatSparent", "maxModels", "ascending", "convergence" };
|
||||
checkHyperparameters(validKeys, hyperparameters);
|
||||
if (hyperparameters.contains("repeatSparent")) {
|
||||
repeatSparent = hyperparameters["repeatSparent"];
|
||||
@@ -22,6 +25,38 @@ namespace bayesnet {
|
||||
if (hyperparameters.contains("ascending")) {
|
||||
ascending = hyperparameters["ascending"];
|
||||
}
|
||||
if (hyperparameters.contains("convergence")) {
|
||||
convergence = hyperparameters["convergence"];
|
||||
}
|
||||
}
|
||||
void BoostAODE::validationInit()
|
||||
{
|
||||
auto y_ = dataset.index({ -1, "..." });
|
||||
if (convergence) {
|
||||
// Prepare train & validation sets from train data
|
||||
auto fold = platform::StratifiedKFold(5, y_, 271);
|
||||
dataset_ = torch::clone(dataset);
|
||||
// save input dataset
|
||||
auto [train, test] = fold.getFold(0);
|
||||
auto train_t = torch::tensor(train);
|
||||
auto test_t = torch::tensor(test);
|
||||
// Get train and validation sets
|
||||
X_train = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), train_t });
|
||||
y_train = dataset.index({ -1, train_t });
|
||||
X_test = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), test_t });
|
||||
y_test = dataset.index({ -1, test_t });
|
||||
dataset = X_train;
|
||||
m = X_train.size(1);
|
||||
auto n_classes = states.at(className).size();
|
||||
metrics = Metrics(dataset, features, className, n_classes);
|
||||
// Build dataset with train data
|
||||
buildDataset(y_train);
|
||||
} else {
|
||||
// Use all data to train
|
||||
X_train = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." });
|
||||
y_train = y_;
|
||||
}
|
||||
|
||||
}
|
||||
void BoostAODE::trainModel(const torch::Tensor& weights)
|
||||
{
|
||||
@@ -29,15 +64,22 @@ namespace bayesnet {
|
||||
n_models = 0;
|
||||
if (maxModels == 0)
|
||||
maxModels = .1 * n > 10 ? .1 * n : n;
|
||||
validationInit();
|
||||
Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
|
||||
auto X_ = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." });
|
||||
auto y_ = dataset.index({ -1, "..." });
|
||||
bool exitCondition = false;
|
||||
unordered_set<int> featuresUsed;
|
||||
// Variables to control the accuracy finish condition
|
||||
double priorAccuracy = 0.0;
|
||||
double delta = 1.0;
|
||||
double threshold = 1e-4;
|
||||
int tolerance = 5; // number of times the accuracy can be lower than the threshold
|
||||
int count = 0; // number of times the accuracy is lower than the threshold
|
||||
fitted = true; // to enable predict
|
||||
// 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();
|
||||
// epsiolon sub t > 0.5 => inverse the weights policy
|
||||
// validation error is not decreasing
|
||||
while (!exitCondition) {
|
||||
// Step 1: Build ranking with mutual information
|
||||
auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted
|
||||
@@ -60,29 +102,44 @@ namespace bayesnet {
|
||||
}
|
||||
featuresUsed.insert(feature);
|
||||
model = std::make_unique<SPODE>(feature);
|
||||
n_models++;
|
||||
model->fit(dataset, features, className, states, weights_);
|
||||
auto ypred = model->predict(X_);
|
||||
auto ypred = model->predict(X_train);
|
||||
// Step 3.1: Compute the classifier amout of say
|
||||
auto mask_wrong = ypred != y_;
|
||||
auto mask_wrong = ypred != y_train;
|
||||
auto mask_right = ypred == y_train;
|
||||
auto masked_weights = weights_ * mask_wrong.to(weights_.dtype());
|
||||
double wrongWeights = masked_weights.sum().item<double>();
|
||||
double significance = wrongWeights == 0 ? 1 : 0.5 * log((1 - wrongWeights) / wrongWeights);
|
||||
double epsilon_t = masked_weights.sum().item<double>();
|
||||
double wt = (1 - epsilon_t) / epsilon_t;
|
||||
double alpha_t = epsilon_t == 0 ? 1 : 0.5 * log(wt);
|
||||
// Step 3.2: Update weights for next classifier
|
||||
// Step 3.2.1: Update weights of wrong samples
|
||||
weights_ += mask_wrong.to(weights_.dtype()) * exp(significance) * weights_;
|
||||
weights_ += mask_wrong.to(weights_.dtype()) * exp(alpha_t) * weights_;
|
||||
// Step 3.2.2: Update weights of right samples
|
||||
weights_ += mask_right.to(weights_.dtype()) * exp(-alpha_t) * weights_;
|
||||
// Step 3.3: Normalise the weights
|
||||
double totalWeights = torch::sum(weights_).item<double>();
|
||||
weights_ = weights_ / totalWeights;
|
||||
// Step 3.4: Store classifier and its accuracy to weigh its future vote
|
||||
models.push_back(std::move(model));
|
||||
significanceModels.push_back(significance);
|
||||
exitCondition = n_models == maxModels && repeatSparent;
|
||||
significanceModels.push_back(alpha_t);
|
||||
n_models++;
|
||||
if (convergence) {
|
||||
auto y_val_predict = predict(X_test);
|
||||
double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0);
|
||||
if (priorAccuracy == 0) {
|
||||
priorAccuracy = accuracy;
|
||||
} else {
|
||||
delta = accuracy - priorAccuracy;
|
||||
}
|
||||
if (delta < threshold) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
exitCondition = n_models == maxModels && repeatSparent || epsilon_t > 0.5 || count > tolerance;
|
||||
}
|
||||
if (featuresUsed.size() != features.size()) {
|
||||
cout << "Warning: BoostAODE did not use all the features" << endl;
|
||||
status = WARNING;
|
||||
}
|
||||
weights.copy_(weights_);
|
||||
}
|
||||
vector<string> BoostAODE::graph(const string& title) const
|
||||
{
|
||||
|
@@ -13,9 +13,13 @@ namespace bayesnet {
|
||||
void buildModel(const torch::Tensor& weights) override;
|
||||
void trainModel(const torch::Tensor& weights) override;
|
||||
private:
|
||||
torch::Tensor dataset_;
|
||||
torch::Tensor X_train, y_train, X_test, y_test;
|
||||
void validationInit();
|
||||
bool repeatSparent = false;
|
||||
int maxModels = 0;
|
||||
bool ascending = false; //Process KBest features ascending or descending order
|
||||
bool convergence = false; //if true, stop when the model does not improve
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -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<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||
Classifier& Classifier::build(const vector<string>& features, const string& className, map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||
{
|
||||
this->features = features;
|
||||
this->className = className;
|
||||
@@ -13,7 +13,7 @@ namespace bayesnet {
|
||||
m = dataset.size(1);
|
||||
n = dataset.size(0) - 1;
|
||||
checkFitParameters();
|
||||
auto n_classes = states[className].size();
|
||||
auto n_classes = states.at(className).size();
|
||||
metrics = Metrics(dataset, features, className, n_classes);
|
||||
model.initialize();
|
||||
buildModel(weights);
|
||||
@@ -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<string>& features, string className, map<string, vector<int>>& states)
|
||||
Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& 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<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states)
|
||||
Classifier& Classifier::fit(vector<vector<int>>& X, vector<int>& y, const vector<string>& features, const string& className, map<string, vector<int>>& states)
|
||||
{
|
||||
dataset = torch::zeros({ static_cast<int>(X.size()), static_cast<int>(X[0].size()) }, kInt32);
|
||||
for (int i = 0; i < X.size(); ++i) {
|
||||
@@ -58,21 +58,24 @@ 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<string>& features, string className, map<string, vector<int>>& states)
|
||||
Classifier& Classifier::fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& 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<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||
Classifier& Classifier::fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||
{
|
||||
this->dataset = dataset;
|
||||
return build(features, className, states, weights);
|
||||
}
|
||||
void Classifier::checkFitParameters()
|
||||
{
|
||||
if (torch::is_floating_point(dataset)) {
|
||||
throw invalid_argument("dataset (X, y) must be of type Integer");
|
||||
}
|
||||
if (n != features.size()) {
|
||||
throw invalid_argument("X " + to_string(n) + " and features " + to_string(features.size()) + " must have the same number of features");
|
||||
throw invalid_argument("Classifier: X " + to_string(n) + " and features " + to_string(features.size()) + " must have the same number of features");
|
||||
}
|
||||
if (states.find(className) == states.end()) {
|
||||
throw invalid_argument("className not found in states");
|
||||
@@ -160,4 +163,10 @@ namespace bayesnet {
|
||||
}
|
||||
}
|
||||
}
|
||||
void Classifier::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid, default is no hyperparameters
|
||||
const vector<string> validKeys = { };
|
||||
checkHyperparameters(validKeys, hyperparameters);
|
||||
}
|
||||
}
|
@@ -10,8 +10,7 @@ using namespace torch;
|
||||
namespace bayesnet {
|
||||
class Classifier : public BaseClassifier {
|
||||
private:
|
||||
void buildDataset(torch::Tensor& y);
|
||||
Classifier& build(vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights);
|
||||
Classifier& build(const vector<string>& features, const string& className, map<string, vector<int>>& states, const torch::Tensor& weights);
|
||||
protected:
|
||||
bool fitted;
|
||||
int m, n; // m: number of samples, n: number of features
|
||||
@@ -21,28 +20,32 @@ namespace bayesnet {
|
||||
string className;
|
||||
map<string, vector<int>> states;
|
||||
Tensor dataset; // (n+1)xm tensor
|
||||
status_t status = NORMAL;
|
||||
void checkFitParameters();
|
||||
virtual void buildModel(const torch::Tensor& weights) = 0;
|
||||
void trainModel(const torch::Tensor& weights) override;
|
||||
void checkHyperparameters(const vector<string>& validKeys, nlohmann::json& hyperparameters);
|
||||
void buildDataset(torch::Tensor& y);
|
||||
public:
|
||||
Classifier(Network model);
|
||||
virtual ~Classifier() = default;
|
||||
Classifier& fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states) override;
|
||||
Classifier& fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) override;
|
||||
Classifier& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states) override;
|
||||
Classifier& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights) override;
|
||||
Classifier& fit(vector<vector<int>>& X, vector<int>& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
Classifier& fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
Classifier& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
Classifier& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states, const torch::Tensor& weights) override;
|
||||
void addNodes();
|
||||
int getNumberOfNodes() const override;
|
||||
int getNumberOfEdges() const override;
|
||||
int getNumberOfStates() const override;
|
||||
Tensor predict(Tensor& X) override;
|
||||
status_t getStatus() const override { return status; }
|
||||
vector<int> predict(vector<vector<int>>& X) override;
|
||||
float score(Tensor& X, Tensor& y) override;
|
||||
float score(vector<vector<int>>& X, vector<int>& y) override;
|
||||
vector<string> show() const override;
|
||||
vector<string> topological_order() override;
|
||||
void dump_cpt() const override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
@@ -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)
|
||||
{
|
||||
@@ -17,10 +17,14 @@ namespace bayesnet {
|
||||
{
|
||||
auto y_pred_ = y_pred.accessor<int, 2>();
|
||||
vector<int> y_pred_final;
|
||||
int numClasses = states.at(className).size();
|
||||
// y_pred is m x n_models with the prediction of every model for each sample
|
||||
for (int i = 0; i < y_pred.size(0); ++i) {
|
||||
vector<double> votes(y_pred.size(1), 0);
|
||||
for (int j = 0; j < y_pred.size(1); ++j) {
|
||||
votes[y_pred_[i][j]] += significanceModels[j];
|
||||
// votes store in each index (value of class) the significance added by each model
|
||||
// i.e. votes[0] contains how much value has the value 0 of class. That value is generated by the models predictions
|
||||
vector<double> votes(numClasses, 0.0);
|
||||
for (int j = 0; j < n_models; ++j) {
|
||||
votes[y_pred_[i][j]] += significanceModels.at(j);
|
||||
}
|
||||
// argsort in descending order
|
||||
auto indices = argsort(votes);
|
||||
@@ -34,7 +38,6 @@ namespace bayesnet {
|
||||
throw logic_error("Ensemble has not been fitted");
|
||||
}
|
||||
Tensor y_pred = torch::zeros({ X.size(1), n_models }, kInt32);
|
||||
//Create a threadpool
|
||||
auto threads{ vector<thread>() };
|
||||
mutex mtx;
|
||||
for (auto i = 0; i < n_models; ++i) {
|
||||
|
@@ -4,6 +4,18 @@ namespace bayesnet {
|
||||
using namespace torch;
|
||||
|
||||
KDB::KDB(int k, float theta) : Classifier(Network()), k(k), theta(theta) {}
|
||||
void KDB::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid
|
||||
const vector<string> validKeys = { "k", "theta" };
|
||||
checkHyperparameters(validKeys, hyperparameters);
|
||||
if (hyperparameters.contains("k")) {
|
||||
k = hyperparameters["k"];
|
||||
}
|
||||
if (hyperparameters.contains("theta")) {
|
||||
theta = hyperparameters["theta"];
|
||||
}
|
||||
}
|
||||
void KDB::buildModel(const torch::Tensor& weights)
|
||||
{
|
||||
/*
|
||||
|
@@ -16,7 +16,7 @@ namespace bayesnet {
|
||||
public:
|
||||
explicit KDB(int k, float theta = 0.03);
|
||||
virtual ~KDB() {};
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||
vector<string> graph(const string& name = "KDB") const override;
|
||||
};
|
||||
}
|
||||
|
@@ -3,9 +3,9 @@
|
||||
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<string>& features_, string className_, map<string, vector<int>>& states_)
|
||||
KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||
{
|
||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
||||
checkInput(X_, y_);
|
||||
features = features_;
|
||||
className = className_;
|
||||
Xf = X_;
|
||||
|
@@ -10,10 +10,9 @@ namespace bayesnet {
|
||||
public:
|
||||
explicit KDBLd(int k);
|
||||
virtual ~KDBLd() = default;
|
||||
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, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
vector<string> graph(const string& name = "KDB") const override;
|
||||
Tensor predict(Tensor& X) override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
static inline string version() { return "0.0.1"; };
|
||||
};
|
||||
}
|
||||
|
@@ -3,8 +3,8 @@
|
||||
#include "Network.h"
|
||||
#include "bayesnetUtils.h"
|
||||
namespace bayesnet {
|
||||
Network::Network() : features(vector<string>()), className(""), classNumStates(0), fitted(false) {}
|
||||
Network::Network(float maxT) : features(vector<string>()), className(""), classNumStates(0), maxThreads(maxT), fitted(false) {}
|
||||
Network::Network() : features(vector<string>()), className(""), classNumStates(0), fitted(false), laplaceSmoothing(0) {}
|
||||
Network::Network(float maxT) : features(vector<string>()), 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)
|
||||
{
|
||||
@@ -132,10 +132,10 @@ namespace bayesnet {
|
||||
void Network::setStates(const map<string, vector<int>>& states)
|
||||
{
|
||||
// Set states to every Node in the network
|
||||
for (int i = 0; i < features.size(); ++i) {
|
||||
nodes[features[i]]->setNumStates(states.at(features[i]).size());
|
||||
}
|
||||
classNumStates = nodes[className]->getNumStates();
|
||||
for_each(features.begin(), features.end(), [this, &states](const string& feature) {
|
||||
nodes.at(feature)->setNumStates(states.at(feature).size());
|
||||
});
|
||||
classNumStates = nodes.at(className)->getNumStates();
|
||||
}
|
||||
// X comes in nxm, where n is the number of features and m the number of samples
|
||||
void Network::fit(const torch::Tensor& X, const torch::Tensor& y, const torch::Tensor& weights, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
||||
@@ -174,37 +174,11 @@ namespace bayesnet {
|
||||
{
|
||||
setStates(states);
|
||||
laplaceSmoothing = 1.0 / samples.size(1); // To use in CPT computation
|
||||
int maxThreadsRunning = static_cast<int>(std::thread::hardware_concurrency() * maxThreads);
|
||||
if (maxThreadsRunning < 1) {
|
||||
maxThreadsRunning = 1;
|
||||
}
|
||||
vector<thread> threads;
|
||||
mutex mtx;
|
||||
condition_variable cv;
|
||||
int activeThreads = 0;
|
||||
int nextNodeIndex = 0;
|
||||
while (nextNodeIndex < nodes.size()) {
|
||||
unique_lock<mutex> lock(mtx);
|
||||
cv.wait(lock, [&activeThreads, &maxThreadsRunning]() { return activeThreads < maxThreadsRunning; });
|
||||
threads.emplace_back([this, &nextNodeIndex, &mtx, &cv, &activeThreads, &weights]() {
|
||||
while (true) {
|
||||
unique_lock<mutex> lock(mtx);
|
||||
if (nextNodeIndex >= nodes.size()) {
|
||||
break; // No more work remaining
|
||||
}
|
||||
auto& pair = *std::next(nodes.begin(), nextNodeIndex);
|
||||
++nextNodeIndex;
|
||||
lock.unlock();
|
||||
pair.second->computeCPT(samples, features, laplaceSmoothing, weights);
|
||||
lock.lock();
|
||||
nodes[pair.first] = std::move(pair.second);
|
||||
lock.unlock();
|
||||
}
|
||||
lock_guard<mutex> lock(mtx);
|
||||
--activeThreads;
|
||||
cv.notify_one();
|
||||
for (auto& node : nodes) {
|
||||
threads.emplace_back([this, &node, &weights]() {
|
||||
node.second->computeCPT(samples, features, laplaceSmoothing, weights);
|
||||
});
|
||||
++activeThreads;
|
||||
}
|
||||
for (auto& thread : threads) {
|
||||
thread.join();
|
||||
@@ -399,7 +373,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) {
|
||||
|
@@ -27,6 +27,7 @@ namespace bayesnet {
|
||||
Network();
|
||||
explicit Network(float);
|
||||
explicit Network(Network&);
|
||||
~Network() = default;
|
||||
torch::Tensor& getSamples();
|
||||
float getmaxThreads();
|
||||
void addNode(const string&);
|
||||
@@ -52,7 +53,7 @@ namespace bayesnet {
|
||||
vector<string> graph(const string& title) const; // Returns a vector of strings representing the graph in graphviz format
|
||||
void initialize();
|
||||
void dump_cpt() const;
|
||||
inline string version() { return "0.1.0"; }
|
||||
inline string version() { return "0.2.0"; }
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -100,7 +100,7 @@ namespace bayesnet {
|
||||
}
|
||||
int name_index = pos - features.begin();
|
||||
for (int n_sample = 0; n_sample < dataset.size(1); ++n_sample) {
|
||||
torch::List<c10::optional<torch::Tensor>> coordinates;
|
||||
c10::List<c10::optional<at::Tensor>> coordinates;
|
||||
coordinates.push_back(dataset.index({ name_index, n_sample }));
|
||||
for (auto parent : parents) {
|
||||
pos = find(features.begin(), features.end(), parent->getName());
|
||||
@@ -118,10 +118,10 @@ namespace bayesnet {
|
||||
}
|
||||
float Node::getFactorValue(map<string, int>& evidence)
|
||||
{
|
||||
torch::List<c10::optional<torch::Tensor>> coordinates;
|
||||
c10::List<c10::optional<at::Tensor>> coordinates;
|
||||
// following predetermined order of indices in the cpTable (see Node.h)
|
||||
coordinates.push_back(torch::tensor(evidence[name]));
|
||||
transform(parents.begin(), parents.end(), back_inserter(coordinates), [&evidence](const auto& parent) { return torch::tensor(evidence[parent->getName()]); });
|
||||
coordinates.push_back(at::tensor(evidence[name]));
|
||||
transform(parents.begin(), parents.end(), back_inserter(coordinates), [&evidence](const auto& parent) { return at::tensor(evidence[parent->getName()]); });
|
||||
return cpTable.index({ coordinates }).item<float>();
|
||||
}
|
||||
vector<string> Node::graph(const string& className)
|
||||
|
@@ -9,6 +9,15 @@ namespace bayesnet {
|
||||
delete value;
|
||||
}
|
||||
}
|
||||
void Proposal::checkInput(const torch::Tensor& X, const torch::Tensor& y)
|
||||
{
|
||||
if (!torch::is_floating_point(X)) {
|
||||
throw std::invalid_argument("X must be a floating point tensor");
|
||||
}
|
||||
if (torch::is_floating_point(y)) {
|
||||
throw std::invalid_argument("y must be an integer tensor");
|
||||
}
|
||||
}
|
||||
map<string, vector<int>> Proposal::localDiscretizationProposal(const map<string, vector<int>>& oldStates, Network& model)
|
||||
{
|
||||
// order of local discretization is important. no good 0, 1, 2...
|
||||
@@ -44,15 +53,6 @@ namespace bayesnet {
|
||||
auto xvf_ptr = Xf.index({ index }).data_ptr<float>();
|
||||
auto xvf = vector<mdlp::precision_t>(xvf_ptr, xvf_ptr + Xf.size(1));
|
||||
discretizers[feature]->fit(xvf, yxv);
|
||||
//
|
||||
//
|
||||
//
|
||||
// auto tmp = discretizers[feature]->transform(xvf);
|
||||
// Xv[index] = tmp;
|
||||
// auto xStates = vector<int>(discretizers[pFeatures[index]]->getCutPoints().size() + 1);
|
||||
// iota(xStates.begin(), xStates.end(), 0);
|
||||
// //Update new states of the feature/node
|
||||
// states[feature] = xStates;
|
||||
}
|
||||
if (upgrade) {
|
||||
// Discretize again X (only the affected indices) with the new fitted discretizers
|
||||
|
@@ -13,6 +13,7 @@ namespace bayesnet {
|
||||
Proposal(torch::Tensor& pDataset, vector<string>& features_, string& className_);
|
||||
virtual ~Proposal();
|
||||
protected:
|
||||
void checkInput(const torch::Tensor& X, const torch::Tensor& y);
|
||||
torch::Tensor prepareX(torch::Tensor& X);
|
||||
map<string, vector<int>> localDiscretizationProposal(const map<string, vector<int>>& states, Network& model);
|
||||
map<string, vector<int>> fit_local_discretization(const torch::Tensor& y);
|
||||
|
@@ -12,7 +12,6 @@ namespace bayesnet {
|
||||
explicit SPODE(int root);
|
||||
virtual ~SPODE() {};
|
||||
vector<string> graph(const string& name = "SPODE") const override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -3,9 +3,9 @@
|
||||
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<string>& features_, string className_, map<string, vector<int>>& states_)
|
||||
SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||
{
|
||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
||||
checkInput(X_, y_);
|
||||
features = features_;
|
||||
className = className_;
|
||||
Xf = X_;
|
||||
@@ -18,11 +18,13 @@ namespace bayesnet {
|
||||
states = localDiscretizationProposal(states, model);
|
||||
return *this;
|
||||
}
|
||||
SPODELd& SPODELd::fit(torch::Tensor& dataset, vector<string>& features_, string className_, map<string, vector<int>>& states_)
|
||||
SPODELd& SPODELd::fit(torch::Tensor& dataset, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||
{
|
||||
if (!torch::is_floating_point(dataset)) {
|
||||
throw std::runtime_error("Dataset must be a floating point tensor");
|
||||
}
|
||||
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
|
||||
y = dataset.index({ -1, "..." }).clone();
|
||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
||||
features = features_;
|
||||
className = className_;
|
||||
// Fills vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||
|
@@ -9,11 +9,10 @@ namespace bayesnet {
|
||||
public:
|
||||
explicit SPODELd(int root);
|
||||
virtual ~SPODELd() = default;
|
||||
SPODELd& fit(torch::Tensor& X, torch::Tensor& y, 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;
|
||||
SPODELd& fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
SPODELd& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
vector<string> graph(const string& name = "SPODE") const override;
|
||||
Tensor predict(Tensor& X) override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
static inline string version() { return "0.0.1"; };
|
||||
};
|
||||
}
|
||||
|
@@ -11,7 +11,6 @@ namespace bayesnet {
|
||||
TAN();
|
||||
virtual ~TAN() {};
|
||||
vector<string> graph(const string& name = "TAN") const override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -3,9 +3,9 @@
|
||||
namespace bayesnet {
|
||||
using namespace std;
|
||||
TANLd::TANLd() : TAN(), Proposal(dataset, features, className) {}
|
||||
TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, vector<string>& features_, string className_, map<string, vector<int>>& states_)
|
||||
TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||
{
|
||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
||||
checkInput(X_, y_);
|
||||
features = features_;
|
||||
className = className_;
|
||||
Xf = X_;
|
||||
|
@@ -10,11 +10,10 @@ namespace bayesnet {
|
||||
public:
|
||||
TANLd();
|
||||
virtual ~TANLd() = default;
|
||||
TANLd& fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) override;
|
||||
TANLd& fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||
vector<string> graph(const string& name = "TAN") const override;
|
||||
Tensor predict(Tensor& X) override;
|
||||
static inline string version() { return "0.0.1"; };
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
||||
};
|
||||
}
|
||||
#endif // !TANLD_H
|
@@ -8,5 +8,9 @@ add_executable(main main.cc Folding.cc platformUtils.cc Experiment.cc Datasets.c
|
||||
add_executable(manage manage.cc Results.cc ReportConsole.cc ReportExcel.cc ReportBase.cc)
|
||||
add_executable(list list.cc platformUtils Datasets.cc)
|
||||
target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
|
||||
if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Linux")
|
||||
target_link_libraries(manage "${TORCH_LIBRARIES}" OpenXLSX::OpenXLSX stdc++fs)
|
||||
else()
|
||||
target_link_libraries(manage "${TORCH_LIBRARIES}" OpenXLSX::OpenXLSX)
|
||||
endif()
|
||||
target_link_libraries(list ArffFiles mdlp "${TORCH_LIBRARIES}")
|
@@ -1,6 +1,7 @@
|
||||
#include "Datasets.h"
|
||||
#include "platformUtils.h"
|
||||
#include "ArffFiles.h"
|
||||
#include <fstream>
|
||||
namespace platform {
|
||||
void Datasets::load()
|
||||
{
|
||||
@@ -212,10 +213,11 @@ namespace platform {
|
||||
{
|
||||
for (int i = 0; i < features.size(); ++i) {
|
||||
states[features[i]] = vector<int>(*max_element(Xd[i].begin(), Xd[i].end()) + 1);
|
||||
iota(begin(states[features[i]]), end(states[features[i]]), 0);
|
||||
auto item = states.at(features[i]);
|
||||
iota(begin(item), end(item), 0);
|
||||
}
|
||||
states[className] = vector<int>(*max_element(yv.begin(), yv.end()) + 1);
|
||||
iota(begin(states[className]), end(states[className]), 0);
|
||||
iota(begin(states.at(className)), end(states.at(className)), 0);
|
||||
}
|
||||
void Dataset::load_arff()
|
||||
{
|
||||
|
@@ -2,7 +2,7 @@
|
||||
#include "Datasets.h"
|
||||
#include "Models.h"
|
||||
#include "ReportConsole.h"
|
||||
|
||||
#include <fstream>
|
||||
namespace platform {
|
||||
using json = nlohmann::json;
|
||||
string get_date()
|
||||
@@ -111,6 +111,26 @@ namespace platform {
|
||||
}
|
||||
}
|
||||
|
||||
string getColor(bayesnet::status_t status)
|
||||
{
|
||||
switch (status) {
|
||||
case bayesnet::NORMAL:
|
||||
return Colors::GREEN();
|
||||
case bayesnet::WARNING:
|
||||
return Colors::YELLOW();
|
||||
case bayesnet::ERROR:
|
||||
return Colors::RED();
|
||||
default:
|
||||
return Colors::RESET();
|
||||
}
|
||||
}
|
||||
|
||||
void showProgress(int fold, const string& color, const string& phase)
|
||||
{
|
||||
string prefix = phase == "a" ? "" : "\b\b\b\b";
|
||||
cout << prefix << color << fold << Colors::RESET() << "(" << color << phase << Colors::RESET() << ")" << flush;
|
||||
|
||||
}
|
||||
void Experiment::cross_validation(const string& path, const string& fileName)
|
||||
{
|
||||
auto datasets = platform::Datasets(path, discretized, platform::ARFF);
|
||||
@@ -159,28 +179,34 @@ namespace platform {
|
||||
auto y_train = y.index({ train_t });
|
||||
auto X_test = X.index({ "...", test_t });
|
||||
auto y_test = y.index({ test_t });
|
||||
cout << nfold + 1 << ", " << flush;
|
||||
showProgress(nfold + 1, getColor(clf->getStatus()), "a");
|
||||
// Train model
|
||||
clf->fit(X_train, y_train, features, className, states);
|
||||
showProgress(nfold + 1, getColor(clf->getStatus()), "b");
|
||||
nodes[item] = clf->getNumberOfNodes();
|
||||
edges[item] = clf->getNumberOfEdges();
|
||||
num_states[item] = clf->getNumberOfStates();
|
||||
train_time[item] = train_timer.getDuration();
|
||||
// Score train
|
||||
auto accuracy_train_value = clf->score(X_train, y_train);
|
||||
// Test model
|
||||
showProgress(nfold + 1, getColor(clf->getStatus()), "c");
|
||||
test_timer.start();
|
||||
auto accuracy_test_value = clf->score(X_test, y_test);
|
||||
test_time[item] = test_timer.getDuration();
|
||||
accuracy_train[item] = accuracy_train_value;
|
||||
accuracy_test[item] = accuracy_test_value;
|
||||
cout << "\b\b\b, " << flush;
|
||||
// Store results and times in vector
|
||||
result.addScoreTrain(accuracy_train_value);
|
||||
result.addScoreTest(accuracy_test_value);
|
||||
result.addTimeTrain(train_time[item].item<double>());
|
||||
result.addTimeTest(test_time[item].item<double>());
|
||||
item++;
|
||||
clf.reset();
|
||||
}
|
||||
cout << "end. " << flush;
|
||||
delete fold;
|
||||
}
|
||||
result.setScoreTest(torch::mean(accuracy_test).item<double>()).setScoreTrain(torch::mean(accuracy_train).item<double>());
|
||||
result.setScoreTestStd(torch::std(accuracy_test).item<double>()).setScoreTrainStd(torch::std(accuracy_train).item<double>());
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#include "Folding.h"
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
namespace platform {
|
||||
Fold::Fold(int k, int n, int seed) : k(k), n(n), seed(seed)
|
||||
{
|
||||
random_device rd;
|
||||
@@ -93,3 +94,4 @@ pair<vector<int>, vector<int>> StratifiedKFold::getFold(int nFold)
|
||||
}
|
||||
return { train_indices, test_indices };
|
||||
}
|
||||
}
|
@@ -4,7 +4,7 @@
|
||||
#include <vector>
|
||||
#include <random>
|
||||
using namespace std;
|
||||
|
||||
namespace platform {
|
||||
class Fold {
|
||||
protected:
|
||||
int k;
|
||||
@@ -34,4 +34,5 @@ public:
|
||||
StratifiedKFold(int k, torch::Tensor& y, int seed = -1);
|
||||
pair<vector<int>, vector<int>> getFold(int nFold) override;
|
||||
};
|
||||
}
|
||||
#endif
|
@@ -26,7 +26,7 @@ namespace platform {
|
||||
instance = it->second();
|
||||
// wrap instance in a shared ptr and return
|
||||
if (instance != nullptr)
|
||||
return shared_ptr<bayesnet::BaseClassifier>(instance);
|
||||
return unique_ptr<bayesnet::BaseClassifier>(instance);
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
@@ -36,14 +36,21 @@ namespace platform {
|
||||
}
|
||||
void ReportConsole::body()
|
||||
{
|
||||
cout << Colors::GREEN() << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl;
|
||||
cout << "============================== ====== ===== === ========= ========= ========= =============== ================== ===============" << endl;
|
||||
cout << Colors::GREEN() << " # Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl;
|
||||
cout << "=== ============================== ====== ===== === ========= ========= ========= =============== ================== ===============" << endl;
|
||||
json lastResult;
|
||||
double totalScore = 0.0;
|
||||
bool odd = true;
|
||||
int index = 0;
|
||||
for (const auto& r : data["results"]) {
|
||||
if (selectedIndex != -1 && index != selectedIndex) {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
auto color = odd ? Colors::CYAN() : Colors::BLUE();
|
||||
cout << color << setw(30) << left << r["dataset"].get<string>() << " ";
|
||||
cout << color;
|
||||
cout << setw(3) << index++ << " ";
|
||||
cout << setw(30) << left << r["dataset"].get<string>() << " ";
|
||||
cout << setw(6) << right << r["samples"].get<int>() << " ";
|
||||
cout << setw(5) << right << r["features"].get<int>() << " ";
|
||||
cout << setw(3) << right << r["classes"].get<int>() << " ";
|
||||
@@ -63,7 +70,7 @@ namespace platform {
|
||||
totalScore += r["score"].get<double>();
|
||||
odd = !odd;
|
||||
}
|
||||
if (data["results"].size() == 1) {
|
||||
if (data["results"].size() == 1 || selectedIndex != -1) {
|
||||
cout << string(MAXL, '*') << endl;
|
||||
cout << headerLine(fVector("Train scores: ", lastResult["scores_train"], 14, 12));
|
||||
cout << headerLine(fVector("Test scores: ", lastResult["scores_test"], 14, 12));
|
||||
|
@@ -7,12 +7,13 @@
|
||||
|
||||
namespace platform {
|
||||
using namespace std;
|
||||
const int MAXL = 128;
|
||||
const int MAXL = 132;
|
||||
class ReportConsole : public ReportBase {
|
||||
public:
|
||||
explicit ReportConsole(json data_) : ReportBase(data_) {};
|
||||
explicit ReportConsole(json data_, int index = -1) : ReportBase(data_), selectedIndex(index) {};
|
||||
virtual ~ReportConsole() = default;
|
||||
private:
|
||||
int selectedIndex;
|
||||
string headerLine(const string& text);
|
||||
void header() override;
|
||||
void body() override;
|
||||
|
@@ -47,11 +47,11 @@ namespace platform {
|
||||
|
||||
void ReportExcel::body()
|
||||
{
|
||||
auto header = vector<string>(
|
||||
auto head = vector<string>(
|
||||
{ "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;
|
||||
|
@@ -23,6 +23,7 @@ namespace platform {
|
||||
title = data["title"];
|
||||
duration = data["duration"];
|
||||
model = data["model"];
|
||||
complete = data["results"].size() > 1;
|
||||
}
|
||||
json Result::load() const
|
||||
{
|
||||
@@ -41,7 +42,7 @@ namespace platform {
|
||||
if (filename.find(".json") != string::npos && filename.find("results_") == 0) {
|
||||
auto result = Result(path, filename);
|
||||
bool addResult = true;
|
||||
if (model != "any" && result.getModel() != model || scoreName != "any" && scoreName != result.getScoreName())
|
||||
if (model != "any" && result.getModel() != model || scoreName != "any" && scoreName != result.getScoreName() || complete && !result.isComplete() || partial && result.isComplete())
|
||||
addResult = false;
|
||||
if (addResult)
|
||||
files.push_back(result);
|
||||
@@ -55,6 +56,8 @@ namespace platform {
|
||||
oss << setw(12) << left << model << " ";
|
||||
oss << setw(11) << left << scoreName << " ";
|
||||
oss << right << setw(11) << setprecision(7) << fixed << score << " ";
|
||||
auto completeString = isComplete() ? "C" : "P";
|
||||
oss << setw(1) << " " << completeString << " ";
|
||||
oss << setw(9) << setprecision(3) << fixed << duration << " ";
|
||||
oss << setw(50) << left << title << " ";
|
||||
return oss.str();
|
||||
@@ -63,9 +66,15 @@ namespace platform {
|
||||
{
|
||||
cout << Colors::GREEN() << "Results found: " << files.size() << endl;
|
||||
cout << "-------------------" << endl;
|
||||
if (complete) {
|
||||
cout << Colors::MAGENTA() << "Only listing complete results" << endl;
|
||||
}
|
||||
if (partial) {
|
||||
cout << Colors::MAGENTA() << "Only listing partial results" << endl;
|
||||
}
|
||||
auto i = 0;
|
||||
cout << " # Date Model Score Name Score Duration Title" << endl;
|
||||
cout << "=== ========== ============ =========== =========== ========= =============================================================" << endl;
|
||||
cout << Colors::GREEN() << " # Date Model Score Name Score C/P Duration Title" << endl;
|
||||
cout << "=== ========== ============ =========== =========== === ========= =============================================================" << endl;
|
||||
bool odd = true;
|
||||
for (const auto& result : files) {
|
||||
auto color = odd ? Colors::BLUE() : Colors::CYAN();
|
||||
@@ -100,13 +109,24 @@ 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::showIndex(const int index, const int idx) const
|
||||
{
|
||||
auto data = files.at(index).load();
|
||||
if (idx < 0 or idx >= static_cast<int>(data["results"].size())) {
|
||||
cout << "Invalid index" << endl;
|
||||
return;
|
||||
}
|
||||
cout << Colors::YELLOW() << "Showing " << files.at(index).getFilename() << endl;
|
||||
ReportConsole reporter(data, idx);
|
||||
reporter.show();
|
||||
}
|
||||
void Results::menu()
|
||||
{
|
||||
char option;
|
||||
@@ -126,9 +146,16 @@ namespace platform {
|
||||
option = line[0];
|
||||
} else {
|
||||
if (all_of(line.begin(), line.end(), ::isdigit)) {
|
||||
index = stoi(line);
|
||||
int idx = stoi(line);
|
||||
if (indexList) {
|
||||
index = idx;
|
||||
if (index >= 0 && index < files.size()) {
|
||||
report(index, false);
|
||||
indexList = false;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
showIndex(index, idx);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -141,6 +168,7 @@ namespace platform {
|
||||
break;
|
||||
case 'l':
|
||||
show();
|
||||
indexList = true;
|
||||
break;
|
||||
case 'd':
|
||||
index = getIndex("delete");
|
||||
@@ -152,6 +180,7 @@ namespace platform {
|
||||
files.erase(files.begin() + index);
|
||||
cout << "File: " + filename + " deleted!" << endl;
|
||||
show();
|
||||
indexList = true;
|
||||
break;
|
||||
case 'h':
|
||||
index = getIndex("hide");
|
||||
@@ -163,21 +192,25 @@ namespace platform {
|
||||
files.erase(files.begin() + index);
|
||||
show();
|
||||
menu();
|
||||
indexList = true;
|
||||
break;
|
||||
case 's':
|
||||
sortList();
|
||||
indexList = true;
|
||||
show();
|
||||
break;
|
||||
case 'r':
|
||||
index = getIndex("report");
|
||||
if (index == -1)
|
||||
break;
|
||||
indexList = false;
|
||||
report(index, false);
|
||||
break;
|
||||
case 'e':
|
||||
index = getIndex("excel");
|
||||
if (index == -1)
|
||||
break;
|
||||
indexList = true;
|
||||
report(index, true);
|
||||
break;
|
||||
default:
|
||||
|
@@ -20,6 +20,7 @@ namespace platform {
|
||||
double getDuration() const { return duration; };
|
||||
string getModel() const { return model; };
|
||||
string getScoreName() const { return scoreName; };
|
||||
bool isComplete() const { return complete; };
|
||||
private:
|
||||
string path;
|
||||
string filename;
|
||||
@@ -29,20 +30,25 @@ namespace platform {
|
||||
double duration;
|
||||
string model;
|
||||
string scoreName;
|
||||
bool complete;
|
||||
};
|
||||
class Results {
|
||||
public:
|
||||
Results(const string& path, const int max, const string& model, const string& score) : path(path), max(max), model(model), scoreName(score) { load(); };
|
||||
Results(const string& path, const int max, const string& model, const string& score, bool complete, bool partial) : path(path), max(max), model(model), scoreName(score), complete(complete), partial(partial) { load(); };
|
||||
void manage();
|
||||
private:
|
||||
string path;
|
||||
int max;
|
||||
string model;
|
||||
string scoreName;
|
||||
bool complete;
|
||||
bool partial;
|
||||
bool indexList = true;
|
||||
vector<Result> files;
|
||||
void load(); // Loads the list of results
|
||||
void show() const;
|
||||
void report(const int index, const bool excelReport) const;
|
||||
void showIndex(const int index, const int idx) const;
|
||||
int getIndex(const string& intent) const;
|
||||
void menu();
|
||||
void sortList();
|
||||
|
@@ -12,6 +12,8 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
|
||||
program.add_argument("-n", "--number").default_value(0).help("Number of results to show (0 = all)").scan<'i', int>();
|
||||
program.add_argument("-m", "--model").default_value("any").help("Filter results of the selected model)");
|
||||
program.add_argument("-s", "--score").default_value("any").help("Filter results of the score name supplied");
|
||||
program.add_argument("--complete").help("Show only results with all datasets").default_value(false).implicit_value(true);
|
||||
program.add_argument("--partial").help("Show only partial results").default_value(false).implicit_value(true);
|
||||
try {
|
||||
program.parse_args(argc, argv);
|
||||
auto number = program.get<int>("number");
|
||||
@@ -20,6 +22,8 @@ argparse::ArgumentParser manageArguments(int argc, char** argv)
|
||||
}
|
||||
auto model = program.get<string>("model");
|
||||
auto score = program.get<string>("score");
|
||||
auto complete = program.get<bool>("complete");
|
||||
auto partial = program.get<bool>("partial");
|
||||
}
|
||||
catch (const exception& err) {
|
||||
cerr << err.what() << endl;
|
||||
@@ -35,7 +39,11 @@ int main(int argc, char** argv)
|
||||
auto number = program.get<int>("number");
|
||||
auto model = program.get<string>("model");
|
||||
auto score = program.get<string>("score");
|
||||
auto results = platform::Results(platform::Paths::results(), number, model, score);
|
||||
auto complete = program.get<bool>("complete");
|
||||
auto partial = program.get<bool>("partial");
|
||||
if (complete)
|
||||
partial = false;
|
||||
auto results = platform::Results(platform::Paths::results(), number, model, score, complete, partial);
|
||||
results.manage();
|
||||
return 0;
|
||||
}
|
||||
|
@@ -69,11 +69,12 @@ tuple<Tensor, Tensor, vector<string>, string, map<string, vector<int>>> loadData
|
||||
Xd = torch::zeros({ static_cast<int>(Xr[0].size()), static_cast<int>(Xr.size()) }, torch::kInt32);
|
||||
for (int i = 0; i < features.size(); ++i) {
|
||||
states[features[i]] = vector<int>(*max_element(Xr[i].begin(), Xr[i].end()) + 1);
|
||||
iota(begin(states[features[i]]), end(states[features[i]]), 0);
|
||||
auto item = states.at(features[i]);
|
||||
iota(begin(item), end(item), 0);
|
||||
Xd.index_put_({ "...", i }, torch::tensor(Xr[i], torch::kInt32));
|
||||
}
|
||||
states[className] = vector<int>(*max_element(y.begin(), y.end()) + 1);
|
||||
iota(begin(states[className]), end(states[className]), 0);
|
||||
iota(begin(states.at(className)), end(states.at(className)), 0);
|
||||
} else {
|
||||
Xd = torch::zeros({ static_cast<int>(X[0].size()), static_cast<int>(X.size()) }, torch::kFloat32);
|
||||
for (int i = 0; i < features.size(); ++i) {
|
||||
|
@@ -4,6 +4,7 @@ if(ENABLE_TESTING)
|
||||
include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
||||
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
||||
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
|
||||
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
|
||||
set(TEST_SOURCES BayesModels.cc BayesNetwork.cc ${BayesNet_SOURCE_DIR}/src/Platform/platformUtils.cc ${BayesNet_SOURCES})
|
||||
add_executable(${TEST_MAIN} ${TEST_SOURCES})
|
||||
target_link_libraries(${TEST_MAIN} PUBLIC "${TORCH_LIBRARIES}" ArffFiles mdlp Catch2::Catch2WithMain)
|
||||
|
Reference in New Issue
Block a user