Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
1a534888d6
|
|||
59ffd179f4
|
|||
9972738deb
|
|||
bafcb26bb6
|
|||
2d7999d5f2
|
|||
a6bb22dfb5
|
|||
704dc937be
|
|||
a3e665eed6
|
|||
918a7b4180
|
|||
80b20f35b4
|
|||
4d4780c1d5
|
|||
fa612c531e
|
|||
24b68f9ae2
|
|||
a062ebf445 | |||
f26ea1f0ac
|
|||
af0419c9da
|
11
.vscode/launch.json
vendored
11
.vscode/launch.json
vendored
@@ -25,9 +25,10 @@
|
|||||||
"program": "${workspaceFolder}/build/src/Platform/main",
|
"program": "${workspaceFolder}/build/src/Platform/main",
|
||||||
"args": [
|
"args": [
|
||||||
"-m",
|
"-m",
|
||||||
"SPODELd",
|
"BoostAODE",
|
||||||
"-p",
|
"-p",
|
||||||
"/Users/rmontanana/Code/discretizbench/datasets",
|
"/Users/rmontanana/Code/discretizbench/datasets",
|
||||||
|
"--discretize",
|
||||||
"--stratified",
|
"--stratified",
|
||||||
"-d",
|
"-d",
|
||||||
"iris"
|
"iris"
|
||||||
@@ -45,6 +46,14 @@
|
|||||||
],
|
],
|
||||||
"cwd": "/Users/rmontanana/Code/discretizbench",
|
"cwd": "/Users/rmontanana/Code/discretizbench",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "lldb",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "list",
|
||||||
|
"program": "${workspaceFolder}/build/src/Platform/list",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "/Users/rmontanana/Code/discretizbench",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "Build & debug active file",
|
"name": "Build & debug active file",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
|
23
.vscode/tasks.json
vendored
23
.vscode/tasks.json
vendored
@@ -32,6 +32,29 @@
|
|||||||
],
|
],
|
||||||
"group": "build",
|
"group": "build",
|
||||||
"detail": "Task generated by Debugger."
|
"detail": "Task generated by Debugger."
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "cppbuild",
|
||||||
|
"label": "C/C++: g++ build active file",
|
||||||
|
"command": "/usr/bin/g++",
|
||||||
|
"args": [
|
||||||
|
"-fdiagnostics-color=always",
|
||||||
|
"-g",
|
||||||
|
"${file}",
|
||||||
|
"-o",
|
||||||
|
"${fileDirname}/${fileBasenameNoExtension}"
|
||||||
|
],
|
||||||
|
"options": {
|
||||||
|
"cwd": "${fileDirname}"
|
||||||
|
},
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
},
|
||||||
|
"detail": "Task generated by Debugger."
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
4
Makefile
4
Makefile
@@ -15,7 +15,7 @@ dependency: ## Create a dependency graph diagram of the project (build/dependenc
|
|||||||
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
|
||||||
|
|
||||||
build: ## Build the main and BayesNetSample
|
build: ## Build the main and BayesNetSample
|
||||||
cmake --build build -t main -t BayesNetSample -j 32
|
cmake --build build -t main -t BayesNetSample -t manage -t list -j 32
|
||||||
|
|
||||||
clean: ## Clean the debug info
|
clean: ## Clean the debug info
|
||||||
@echo ">>> Cleaning Debug BayesNet ...";
|
@echo ">>> Cleaning Debug BayesNet ...";
|
||||||
@@ -35,7 +35,7 @@ release: ## Build a Release version of the project
|
|||||||
@if [ -d ./build ]; then rm -rf ./build; fi
|
@if [ -d ./build ]; then rm -rf ./build; fi
|
||||||
@mkdir build;
|
@mkdir build;
|
||||||
cmake -S . -B build -D CMAKE_BUILD_TYPE=Release; \
|
cmake -S . -B build -D CMAKE_BUILD_TYPE=Release; \
|
||||||
cmake --build build -t main -t BayesNetSample -j 32;
|
cmake --build build -t main -t BayesNetSample -t manage -t list -j 32;
|
||||||
@echo ">>> Done";
|
@echo ">>> Done";
|
||||||
|
|
||||||
test: ## Run tests
|
test: ## Run tests
|
||||||
|
@@ -1,2 +1 @@
|
|||||||
add_library(ArffFiles ArffFiles.cc)
|
add_library(ArffFiles ArffFiles.cc)
|
||||||
#target_link_libraries(BayesNet "${TORCH_LIBRARIES}")
|
|
@@ -141,43 +141,58 @@ int main(int argc, char** argv)
|
|||||||
/*
|
/*
|
||||||
* Begin Processing
|
* Begin Processing
|
||||||
*/
|
*/
|
||||||
auto handler = ArffFiles();
|
auto ypred = torch::tensor({ 1,2,3,2,2,3,4,5,2,1 });
|
||||||
handler.load(complete_file_name, class_last);
|
auto y = torch::tensor({ 0,0,0,0,2,3,4,0,0,0 });
|
||||||
// Get Dataset X, y
|
auto weights = torch::ones({ 10 }, kDouble);
|
||||||
vector<mdlp::samples_t>& X = handler.getX();
|
auto mask = ypred == y;
|
||||||
mdlp::labels_t& y = handler.getY();
|
cout << "ypred:" << ypred << endl;
|
||||||
// Get className & Features
|
cout << "y:" << y << endl;
|
||||||
auto className = handler.getClassName();
|
cout << "weights:" << weights << endl;
|
||||||
vector<string> features;
|
cout << "mask:" << mask << endl;
|
||||||
auto attributes = handler.getAttributes();
|
double value_to_add = 0.5;
|
||||||
transform(attributes.begin(), attributes.end(), back_inserter(features),
|
weights += mask.to(torch::kDouble) * value_to_add;
|
||||||
[](const pair<string, string>& item) { return item.first; });
|
cout << "New weights:" << weights << endl;
|
||||||
// Discretize Dataset
|
auto masked_weights = weights * mask.to(weights.dtype());
|
||||||
auto [Xd, maxes] = discretize(X, y, features);
|
double sum_of_weights = masked_weights.sum().item<double>();
|
||||||
maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
cout << "Sum of weights: " << sum_of_weights << endl;
|
||||||
map<string, vector<int>> states;
|
//weights.index_put_({ mask }, weights + 10);
|
||||||
for (auto feature : features) {
|
// auto handler = ArffFiles();
|
||||||
states[feature] = vector<int>(maxes[feature]);
|
// handler.load(complete_file_name, class_last);
|
||||||
}
|
// // Get Dataset X, y
|
||||||
states[className] = vector<int>(maxes[className]);
|
// vector<mdlp::samples_t>& X = handler.getX();
|
||||||
auto clf = platform::Models::instance()->create(model_name);
|
// mdlp::labels_t& y = handler.getY();
|
||||||
clf->fit(Xd, y, features, className, states);
|
// // Get className & Features
|
||||||
if (dump_cpt) {
|
// auto className = handler.getClassName();
|
||||||
cout << "--- CPT Tables ---" << endl;
|
// vector<string> features;
|
||||||
clf->dump_cpt();
|
// auto attributes = handler.getAttributes();
|
||||||
}
|
// transform(attributes.begin(), attributes.end(), back_inserter(features),
|
||||||
auto lines = clf->show();
|
// [](const pair<string, string>& item) { return item.first; });
|
||||||
for (auto line : lines) {
|
// // Discretize Dataset
|
||||||
cout << line << endl;
|
// auto [Xd, maxes] = discretize(X, y, features);
|
||||||
}
|
// maxes[className] = *max_element(y.begin(), y.end()) + 1;
|
||||||
cout << "--- Topological Order ---" << endl;
|
// map<string, vector<int>> states;
|
||||||
auto order = clf->topological_order();
|
// for (auto feature : features) {
|
||||||
for (auto name : order) {
|
// states[feature] = vector<int>(maxes[feature]);
|
||||||
cout << name << ", ";
|
// }
|
||||||
}
|
// states[className] = vector<int>(maxes[className]);
|
||||||
cout << "end." << endl;
|
// auto clf = platform::Models::instance()->create(model_name);
|
||||||
auto score = clf->score(Xd, y);
|
// clf->fit(Xd, y, features, className, states);
|
||||||
cout << "Score: " << score << endl;
|
// 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 graph = clf->graph();
|
||||||
// auto dot_file = model_name + "_" + file_name;
|
// auto dot_file = model_name + "_" + file_name;
|
||||||
// ofstream file(dot_file + ".dot");
|
// ofstream file(dot_file + ".dot");
|
||||||
|
@@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
AODE::AODE() : Ensemble() {}
|
AODE::AODE() : Ensemble() {}
|
||||||
void AODE::buildModel()
|
void AODE::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
models.clear();
|
models.clear();
|
||||||
for (int i = 0; i < features.size(); ++i) {
|
for (int i = 0; i < features.size(); ++i) {
|
||||||
models.push_back(std::make_unique<SPODE>(i));
|
models.push_back(std::make_unique<SPODE>(i));
|
||||||
}
|
}
|
||||||
|
n_models = models.size();
|
||||||
|
significanceModels = vector<double>(n_models, 1.0);
|
||||||
}
|
}
|
||||||
vector<string> AODE::graph(const string& title) const
|
vector<string> AODE::graph(const string& title) const
|
||||||
{
|
{
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
class AODE : public Ensemble {
|
class AODE : public Ensemble {
|
||||||
protected:
|
protected:
|
||||||
void buildModel() override;
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
AODE();
|
AODE();
|
||||||
virtual ~AODE() {};
|
virtual ~AODE() {};
|
||||||
|
@@ -19,7 +19,7 @@ namespace bayesnet {
|
|||||||
return *this;
|
return *this;
|
||||||
|
|
||||||
}
|
}
|
||||||
void AODELd::buildModel()
|
void AODELd::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
models.clear();
|
models.clear();
|
||||||
for (int i = 0; i < features.size(); ++i) {
|
for (int i = 0; i < features.size(); ++i) {
|
||||||
@@ -27,7 +27,7 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
n_models = models.size();
|
n_models = models.size();
|
||||||
}
|
}
|
||||||
void AODELd::trainModel()
|
void AODELd::trainModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
for (const auto& model : models) {
|
for (const auto& model : models) {
|
||||||
model->fit(Xf, y, features, className, states);
|
model->fit(Xf, y, features, className, states);
|
||||||
|
@@ -8,8 +8,8 @@ namespace bayesnet {
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
class AODELd : public Ensemble, public Proposal {
|
class AODELd : public Ensemble, public Proposal {
|
||||||
protected:
|
protected:
|
||||||
void trainModel() override;
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
void buildModel() override;
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
AODELd();
|
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_, vector<string>& features_, string className_, map<string, vector<int>>& states_) override;
|
||||||
|
@@ -6,13 +6,14 @@ namespace bayesnet {
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
class BaseClassifier {
|
class BaseClassifier {
|
||||||
protected:
|
protected:
|
||||||
virtual void trainModel() = 0;
|
virtual void trainModel(const torch::Tensor& weights) = 0;
|
||||||
public:
|
public:
|
||||||
// X is nxm vector, y is nx1 vector
|
// 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, vector<string>& features, string className, map<string, vector<int>>& states) = 0;
|
||||||
// X is nxm tensor, y is nx1 tensor
|
// 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& 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) = 0;
|
||||||
|
virtual BaseClassifier& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights) = 0;
|
||||||
virtual ~BaseClassifier() = default;
|
virtual ~BaseClassifier() = default;
|
||||||
torch::Tensor virtual predict(torch::Tensor& X) = 0;
|
torch::Tensor virtual predict(torch::Tensor& X) = 0;
|
||||||
vector<int> virtual predict(vector<vector<int>>& X) = 0;
|
vector<int> virtual predict(vector<vector<int>>& X) = 0;
|
||||||
|
@@ -21,6 +21,31 @@ 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)
|
||||||
|
{
|
||||||
|
auto n = samples.size(0) - 1;
|
||||||
|
if (k == 0) {
|
||||||
|
k = n;
|
||||||
|
}
|
||||||
|
// compute scores
|
||||||
|
scoresKBest.reserve(n);
|
||||||
|
auto label = samples.index({ -1, "..." });
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
scoresKBest.push_back(mutualInformation(label, samples.index({ i, "..." }), weights));
|
||||||
|
featuresKBest.push_back(i);
|
||||||
|
}
|
||||||
|
// sort & reduce scores and features
|
||||||
|
sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
|
||||||
|
{ return scoresKBest[i] > scoresKBest[j]; });
|
||||||
|
sort(scoresKBest.begin(), scoresKBest.end(), std::greater<double>());
|
||||||
|
featuresKBest.resize(k);
|
||||||
|
scoresKBest.resize(k);
|
||||||
|
return featuresKBest;
|
||||||
|
}
|
||||||
|
vector<double> Metrics::getScoresKBest() const
|
||||||
|
{
|
||||||
|
return scoresKBest;
|
||||||
|
}
|
||||||
vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source)
|
vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source)
|
||||||
{
|
{
|
||||||
vector<pair<string, string>> result;
|
vector<pair<string, string>> result;
|
||||||
@@ -32,17 +57,18 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
torch::Tensor Metrics::conditionalEdge()
|
torch::Tensor Metrics::conditionalEdge(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
auto result = vector<double>();
|
auto result = vector<double>();
|
||||||
auto source = vector<string>(features);
|
auto source = vector<string>(features);
|
||||||
source.push_back(className);
|
source.push_back(className);
|
||||||
auto combinations = doCombinations(source);
|
auto combinations = doCombinations(source);
|
||||||
|
double totalWeight = weights.sum().item<double>();
|
||||||
// Compute class prior
|
// Compute class prior
|
||||||
auto margin = torch::zeros({ classNumStates });
|
auto margin = torch::zeros({ classNumStates }, torch::kFloat);
|
||||||
for (int value = 0; value < classNumStates; ++value) {
|
for (int value = 0; value < classNumStates; ++value) {
|
||||||
auto mask = samples.index({ -1, "..." }) == value;
|
auto mask = samples.index({ -1, "..." }) == value;
|
||||||
margin[value] = mask.sum().item<float>() / samples.size(1);
|
margin[value] = mask.sum().item<double>() / samples.size(1);
|
||||||
}
|
}
|
||||||
for (auto [first, second] : combinations) {
|
for (auto [first, second] : combinations) {
|
||||||
int index_first = find(features.begin(), features.end(), first) - features.begin();
|
int index_first = find(features.begin(), features.end(), first) - features.begin();
|
||||||
@@ -52,8 +78,9 @@ namespace bayesnet {
|
|||||||
auto mask = samples.index({ -1, "..." }) == value;
|
auto mask = samples.index({ -1, "..." }) == value;
|
||||||
auto first_dataset = samples.index({ index_first, mask });
|
auto first_dataset = samples.index({ index_first, mask });
|
||||||
auto second_dataset = samples.index({ index_second, mask });
|
auto second_dataset = samples.index({ index_second, mask });
|
||||||
auto mi = mutualInformation(first_dataset, second_dataset);
|
auto weights_dataset = weights.index({ mask });
|
||||||
auto pb = margin[value].item<float>();
|
auto mi = mutualInformation(first_dataset, second_dataset, weights_dataset);
|
||||||
|
auto pb = margin[value].item<double>();
|
||||||
accumulated += pb * mi;
|
accumulated += pb * mi;
|
||||||
}
|
}
|
||||||
result.push_back(accumulated);
|
result.push_back(accumulated);
|
||||||
@@ -70,31 +97,32 @@ namespace bayesnet {
|
|||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
// To use in Python
|
// To use in Python
|
||||||
vector<float> Metrics::conditionalEdgeWeights()
|
vector<float> Metrics::conditionalEdgeWeights(vector<float>& weights_)
|
||||||
{
|
{
|
||||||
auto matrix = conditionalEdge();
|
const torch::Tensor weights = torch::tensor(weights_);
|
||||||
|
auto matrix = conditionalEdge(weights);
|
||||||
std::vector<float> v(matrix.data_ptr<float>(), matrix.data_ptr<float>() + matrix.numel());
|
std::vector<float> v(matrix.data_ptr<float>(), matrix.data_ptr<float>() + matrix.numel());
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
double Metrics::entropy(const torch::Tensor& feature)
|
double Metrics::entropy(const torch::Tensor& feature, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
torch::Tensor counts = feature.bincount();
|
torch::Tensor counts = feature.bincount(weights);
|
||||||
int totalWeight = counts.sum().item<int>();
|
double totalWeight = counts.sum().item<double>();
|
||||||
torch::Tensor probs = counts.to(torch::kFloat) / totalWeight;
|
torch::Tensor probs = counts.to(torch::kFloat) / totalWeight;
|
||||||
torch::Tensor logProbs = torch::log(probs);
|
torch::Tensor logProbs = torch::log(probs);
|
||||||
torch::Tensor entropy = -probs * logProbs;
|
torch::Tensor entropy = -probs * logProbs;
|
||||||
return entropy.nansum().item<double>();
|
return entropy.nansum().item<double>();
|
||||||
}
|
}
|
||||||
// H(Y|X) = sum_{x in X} p(x) H(Y|X=x)
|
// H(Y|X) = sum_{x in X} p(x) H(Y|X=x)
|
||||||
double Metrics::conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature)
|
double Metrics::conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
int numSamples = firstFeature.sizes()[0];
|
int numSamples = firstFeature.sizes()[0];
|
||||||
torch::Tensor featureCounts = secondFeature.bincount();
|
torch::Tensor featureCounts = secondFeature.bincount(weights);
|
||||||
unordered_map<int, unordered_map<int, double>> jointCounts;
|
unordered_map<int, unordered_map<int, double>> jointCounts;
|
||||||
double totalWeight = 0;
|
double totalWeight = 0;
|
||||||
for (auto i = 0; i < numSamples; i++) {
|
for (auto i = 0; i < numSamples; i++) {
|
||||||
jointCounts[secondFeature[i].item<int>()][firstFeature[i].item<int>()] += 1;
|
jointCounts[secondFeature[i].item<int>()][firstFeature[i].item<int>()] += weights[i].item<double>();
|
||||||
totalWeight += 1;
|
totalWeight += weights[i].item<float>();
|
||||||
}
|
}
|
||||||
if (totalWeight == 0)
|
if (totalWeight == 0)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -115,9 +143,9 @@ namespace bayesnet {
|
|||||||
return entropyValue;
|
return entropyValue;
|
||||||
}
|
}
|
||||||
// I(X;Y) = H(Y) - H(Y|X)
|
// I(X;Y) = H(Y) - H(Y|X)
|
||||||
double Metrics::mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature)
|
double Metrics::mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
return entropy(firstFeature) - conditionalEntropy(firstFeature, secondFeature);
|
return entropy(firstFeature, weights) - conditionalEntropy(firstFeature, secondFeature, weights);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Compute the maximum spanning tree considering the weights as distances
|
Compute the maximum spanning tree considering the weights as distances
|
||||||
|
@@ -12,16 +12,20 @@ namespace bayesnet {
|
|||||||
vector<string> features;
|
vector<string> features;
|
||||||
string className;
|
string className;
|
||||||
int classNumStates = 0;
|
int classNumStates = 0;
|
||||||
|
vector<double> scoresKBest;
|
||||||
|
vector<int> featuresKBest; // sorted indices of the features
|
||||||
|
double entropy(const Tensor& feature, const Tensor& weights);
|
||||||
|
double conditionalEntropy(const Tensor& firstFeature, const Tensor& secondFeature, const Tensor& weights);
|
||||||
|
vector<pair<string, string>> doCombinations(const vector<string>&);
|
||||||
public:
|
public:
|
||||||
Metrics() = default;
|
Metrics() = default;
|
||||||
Metrics(const Tensor&, const vector<string>&, const string&, const int);
|
Metrics(const torch::Tensor& samples, const vector<string>& features, const string& className, const int classNumStates);
|
||||||
Metrics(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&, const int);
|
Metrics(const vector<vector<int>>& vsamples, const vector<int>& labels, const vector<string>& features, const string& className, const int classNumStates);
|
||||||
double entropy(const Tensor&);
|
vector<int> SelectKBestWeighted(const torch::Tensor& weights, unsigned k = 0);
|
||||||
double conditionalEntropy(const Tensor&, const Tensor&);
|
vector<double> getScoresKBest() const;
|
||||||
double mutualInformation(const Tensor&, const Tensor&);
|
double mutualInformation(const Tensor& firstFeature, const Tensor& secondFeature, const Tensor& weights);
|
||||||
vector<float> conditionalEdgeWeights(); // To use in Python
|
vector<float> conditionalEdgeWeights(vector<float>& weights); // To use in Python
|
||||||
Tensor conditionalEdge();
|
Tensor conditionalEdge(const torch::Tensor& weights);
|
||||||
vector<pair<string, string>> doCombinations(const vector<string>&);
|
|
||||||
vector<pair<int, int>> maximumSpanningTree(const vector<string>& features, const Tensor& weights, const int root);
|
vector<pair<int, int>> maximumSpanningTree(const vector<string>& features, const Tensor& weights, const int root);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
82
src/BayesNet/BoostAODE.cc
Normal file
82
src/BayesNet/BoostAODE.cc
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
#include "BoostAODE.h"
|
||||||
|
#include "BayesMetrics.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
BoostAODE::BoostAODE() : Ensemble() {}
|
||||||
|
void BoostAODE::buildModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
// Models shall be built in trainModel
|
||||||
|
}
|
||||||
|
void BoostAODE::trainModel(const torch::Tensor& weights)
|
||||||
|
{
|
||||||
|
models.clear();
|
||||||
|
n_models = 0;
|
||||||
|
int max_models = .1 * n > 10 ? .1 * n : n;
|
||||||
|
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;
|
||||||
|
bool repeatSparent = false;
|
||||||
|
vector<int> featuresUsed;
|
||||||
|
// Step 0: Set the finish condition
|
||||||
|
// if not repeatSparent a finish condition is run out of features
|
||||||
|
// n_models == max_models
|
||||||
|
int numClasses = states[className].size();
|
||||||
|
while (!exitCondition) {
|
||||||
|
// Step 1: Build ranking with mutual information
|
||||||
|
auto featureSelection = metrics.SelectKBestWeighted(weights_, n); // Get all the features sorted
|
||||||
|
auto feature = featureSelection[0];
|
||||||
|
unique_ptr<Classifier> model;
|
||||||
|
if (!repeatSparent) {
|
||||||
|
if (n_models == 0) {
|
||||||
|
models.resize(n); // Resize for n==nfeatures SPODEs
|
||||||
|
significanceModels.resize(n);
|
||||||
|
}
|
||||||
|
bool found = false;
|
||||||
|
for (int i = 0; i < featureSelection.size(); ++i) {
|
||||||
|
if (find(featuresUsed.begin(), featuresUsed.end(), i) != featuresUsed.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
found = true;
|
||||||
|
feature = i;
|
||||||
|
featuresUsed.push_back(feature);
|
||||||
|
n_models++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
exitCondition = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
model = std::make_unique<SPODE>(feature);
|
||||||
|
model->fit(dataset, features, className, states, weights_);
|
||||||
|
auto ypred = model->predict(X_);
|
||||||
|
// Step 3.1: Compute the classifier amout of say
|
||||||
|
auto mask_wrong = ypred != y_;
|
||||||
|
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);
|
||||||
|
// 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_;
|
||||||
|
// 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
|
||||||
|
if (!repeatSparent) {
|
||||||
|
models[feature] = std::move(model);
|
||||||
|
significanceModels[feature] = significance;
|
||||||
|
} else {
|
||||||
|
models.push_back(std::move(model));
|
||||||
|
significanceModels.push_back(significance);
|
||||||
|
n_models++;
|
||||||
|
}
|
||||||
|
exitCondition = n_models == max_models;
|
||||||
|
}
|
||||||
|
weights.copy_(weights_);
|
||||||
|
}
|
||||||
|
vector<string> BoostAODE::graph(const string& title) const
|
||||||
|
{
|
||||||
|
return Ensemble::graph(title);
|
||||||
|
}
|
||||||
|
}
|
16
src/BayesNet/BoostAODE.h
Normal file
16
src/BayesNet/BoostAODE.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef BOOSTAODE_H
|
||||||
|
#define BOOSTAODE_H
|
||||||
|
#include "Ensemble.h"
|
||||||
|
#include "SPODE.h"
|
||||||
|
namespace bayesnet {
|
||||||
|
class BoostAODE : public Ensemble {
|
||||||
|
protected:
|
||||||
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
|
public:
|
||||||
|
BoostAODE();
|
||||||
|
virtual ~BoostAODE() {};
|
||||||
|
vector<string> graph(const string& title = "BoostAODE") const override;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
@@ -3,5 +3,6 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
|||||||
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
|
||||||
KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc Mst.cc Proposal.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
|
KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc BoostAODE.cc
|
||||||
target_link_libraries(BayesNet mdlp ArffFiles "${TORCH_LIBRARIES}")
|
Mst.cc Proposal.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
|
||||||
|
target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}")
|
@@ -5,7 +5,7 @@ namespace bayesnet {
|
|||||||
using namespace torch;
|
using namespace torch;
|
||||||
|
|
||||||
Classifier::Classifier(Network model) : model(model), m(0), n(0), metrics(Metrics()), fitted(false) {}
|
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)
|
Classifier& Classifier::build(vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
this->features = features;
|
this->features = features;
|
||||||
this->className = className;
|
this->className = className;
|
||||||
@@ -16,12 +16,11 @@ namespace bayesnet {
|
|||||||
auto n_classes = states[className].size();
|
auto n_classes = states[className].size();
|
||||||
metrics = Metrics(dataset, features, className, n_classes);
|
metrics = Metrics(dataset, features, className, n_classes);
|
||||||
model.initialize();
|
model.initialize();
|
||||||
buildModel();
|
buildModel(weights);
|
||||||
trainModel();
|
trainModel(weights);
|
||||||
fitted = true;
|
fitted = true;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Classifier::buildDataset(Tensor& ytmp)
|
void Classifier::buildDataset(Tensor& ytmp)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@@ -35,16 +34,17 @@ namespace bayesnet {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Classifier::trainModel()
|
void Classifier::trainModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
model.fit(dataset, features, className, states);
|
model.fit(dataset, weights, features, className, states);
|
||||||
}
|
}
|
||||||
// X is nxm where n is the number of features and m the number of samples
|
// 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, vector<string>& features, string className, map<string, vector<int>>& states)
|
||||||
{
|
{
|
||||||
dataset = X;
|
dataset = X;
|
||||||
buildDataset(y);
|
buildDataset(y);
|
||||||
return build(features, className, states);
|
const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
|
||||||
|
return build(features, className, states, weights);
|
||||||
}
|
}
|
||||||
// X is nxm where n is the number of features and m the number of samples
|
// 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, vector<string>& features, string className, map<string, vector<int>>& states)
|
||||||
@@ -55,12 +55,19 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
auto ytmp = torch::tensor(y, kInt32);
|
auto ytmp = torch::tensor(y, kInt32);
|
||||||
buildDataset(ytmp);
|
buildDataset(ytmp);
|
||||||
return build(features, className, states);
|
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, vector<string>& features, string className, map<string, vector<int>>& states)
|
||||||
{
|
{
|
||||||
this->dataset = dataset;
|
this->dataset = dataset;
|
||||||
return build(features, className, states);
|
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)
|
||||||
|
{
|
||||||
|
this->dataset = dataset;
|
||||||
|
return build(features, className, states, weights);
|
||||||
}
|
}
|
||||||
void Classifier::checkFitParameters()
|
void Classifier::checkFitParameters()
|
||||||
{
|
{
|
||||||
|
@@ -11,25 +11,26 @@ namespace bayesnet {
|
|||||||
class Classifier : public BaseClassifier {
|
class Classifier : public BaseClassifier {
|
||||||
private:
|
private:
|
||||||
void buildDataset(torch::Tensor& y);
|
void buildDataset(torch::Tensor& y);
|
||||||
Classifier& build(vector<string>& features, string className, map<string, vector<int>>& states);
|
Classifier& build(vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights);
|
||||||
protected:
|
protected:
|
||||||
bool fitted;
|
bool fitted;
|
||||||
Network model;
|
|
||||||
int m, n; // m: number of samples, n: number of features
|
int m, n; // m: number of samples, n: number of features
|
||||||
Tensor dataset; // (n+1)xm tensor
|
Network model;
|
||||||
Metrics metrics;
|
Metrics metrics;
|
||||||
vector<string> features;
|
vector<string> features;
|
||||||
string className;
|
string className;
|
||||||
map<string, vector<int>> states;
|
map<string, vector<int>> states;
|
||||||
|
Tensor dataset; // (n+1)xm tensor
|
||||||
void checkFitParameters();
|
void checkFitParameters();
|
||||||
virtual void buildModel() = 0;
|
virtual void buildModel(const torch::Tensor& weights) = 0;
|
||||||
void trainModel() override;
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
Classifier(Network model);
|
Classifier(Network model);
|
||||||
virtual ~Classifier() = default;
|
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(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& 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) override;
|
||||||
|
Classifier& fit(torch::Tensor& dataset, vector<string>& features, string className, map<string, vector<int>>& states, const torch::Tensor& weights) override;
|
||||||
void addNodes();
|
void addNodes();
|
||||||
int getNumberOfNodes() const override;
|
int getNumberOfNodes() const override;
|
||||||
int getNumberOfEdges() const override;
|
int getNumberOfEdges() const override;
|
||||||
|
@@ -5,7 +5,7 @@ namespace bayesnet {
|
|||||||
|
|
||||||
Ensemble::Ensemble() : Classifier(Network()) {}
|
Ensemble::Ensemble() : Classifier(Network()) {}
|
||||||
|
|
||||||
void Ensemble::trainModel()
|
void Ensemble::trainModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
n_models = models.size();
|
n_models = models.size();
|
||||||
for (auto i = 0; i < n_models; ++i) {
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
@@ -18,9 +18,9 @@ namespace bayesnet {
|
|||||||
auto y_pred_ = y_pred.accessor<int, 2>();
|
auto y_pred_ = y_pred.accessor<int, 2>();
|
||||||
vector<int> y_pred_final;
|
vector<int> y_pred_final;
|
||||||
for (int i = 0; i < y_pred.size(0); ++i) {
|
for (int i = 0; i < y_pred.size(0); ++i) {
|
||||||
vector<float> votes(y_pred.size(1), 0);
|
vector<double> votes(y_pred.size(1), 0);
|
||||||
for (int j = 0; j < y_pred.size(1); ++j) {
|
for (int j = 0; j < y_pred.size(1); ++j) {
|
||||||
votes[y_pred_[i][j]] += 1;
|
votes[y_pred_[i][j]] += significanceModels[j];
|
||||||
}
|
}
|
||||||
// argsort in descending order
|
// argsort in descending order
|
||||||
auto indices = argsort(votes);
|
auto indices = argsort(votes);
|
||||||
|
@@ -14,7 +14,8 @@ namespace bayesnet {
|
|||||||
protected:
|
protected:
|
||||||
unsigned n_models;
|
unsigned n_models;
|
||||||
vector<unique_ptr<Classifier>> models;
|
vector<unique_ptr<Classifier>> models;
|
||||||
void trainModel() override;
|
vector<double> significanceModels;
|
||||||
|
void trainModel(const torch::Tensor& weights) override;
|
||||||
vector<int> voting(Tensor& y_pred);
|
vector<int> voting(Tensor& y_pred);
|
||||||
public:
|
public:
|
||||||
Ensemble();
|
Ensemble();
|
||||||
|
@@ -4,7 +4,7 @@ namespace bayesnet {
|
|||||||
using namespace torch;
|
using namespace torch;
|
||||||
|
|
||||||
KDB::KDB(int k, float theta) : Classifier(Network()), k(k), theta(theta) {}
|
KDB::KDB(int k, float theta) : Classifier(Network()), k(k), theta(theta) {}
|
||||||
void KDB::buildModel()
|
void KDB::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
1. For each feature Xi, compute mutual information, I(X;C),
|
1. For each feature Xi, compute mutual information, I(X;C),
|
||||||
@@ -29,13 +29,13 @@ namespace bayesnet {
|
|||||||
// where C is the class.
|
// where C is the class.
|
||||||
addNodes();
|
addNodes();
|
||||||
const Tensor& y = dataset.index({ -1, "..." });
|
const Tensor& y = dataset.index({ -1, "..." });
|
||||||
vector <float> mi;
|
vector<double> mi;
|
||||||
for (auto i = 0; i < features.size(); i++) {
|
for (auto i = 0; i < features.size(); i++) {
|
||||||
Tensor firstFeature = dataset.index({ i, "..." });
|
Tensor firstFeature = dataset.index({ i, "..." });
|
||||||
mi.push_back(metrics.mutualInformation(firstFeature, y));
|
mi.push_back(metrics.mutualInformation(firstFeature, y, weights));
|
||||||
}
|
}
|
||||||
// 2. Compute class conditional mutual information I(Xi;XjIC), f or each
|
// 2. Compute class conditional mutual information I(Xi;XjIC), f or each
|
||||||
auto conditionalEdgeWeights = metrics.conditionalEdge();
|
auto conditionalEdgeWeights = metrics.conditionalEdge(weights);
|
||||||
// 3. Let the used variable list, S, be empty.
|
// 3. Let the used variable list, S, be empty.
|
||||||
vector<int> S;
|
vector<int> S;
|
||||||
// 4. Let the DAG network being constructed, BN, begin with a single
|
// 4. Let the DAG network being constructed, BN, begin with a single
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#ifndef KDB_H
|
#ifndef KDB_H
|
||||||
#define KDB_H
|
#define KDB_H
|
||||||
|
#include <torch/torch.h>
|
||||||
#include "Classifier.h"
|
#include "Classifier.h"
|
||||||
#include "bayesnetUtils.h"
|
#include "bayesnetUtils.h"
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
@@ -11,7 +12,7 @@ namespace bayesnet {
|
|||||||
float theta;
|
float theta;
|
||||||
void add_m_edges(int idx, vector<int>& S, Tensor& weights);
|
void add_m_edges(int idx, vector<int>& S, Tensor& weights);
|
||||||
protected:
|
protected:
|
||||||
void buildModel() override;
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
explicit KDB(int k, float theta = 0.03);
|
explicit KDB(int k, float theta = 0.03);
|
||||||
virtual ~KDB() {};
|
virtual ~KDB() {};
|
||||||
|
@@ -5,7 +5,6 @@
|
|||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
Network::Network() : features(vector<string>()), className(""), classNumStates(0), fitted(false) {}
|
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(float maxT) : features(vector<string>()), className(""), classNumStates(0), maxThreads(maxT), fitted(false) {}
|
||||||
Network::Network(float maxT, int smoothing) : laplaceSmoothing(smoothing), features(vector<string>()), className(""), classNumStates(0), maxThreads(maxT), fitted(false) {}
|
|
||||||
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), features(other.features), className(other.className), classNumStates(other.getClassNumStates()), maxThreads(other.
|
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), features(other.features), className(other.className), classNumStates(other.getClassNumStates()), maxThreads(other.
|
||||||
getmaxThreads()), fitted(other.fitted)
|
getmaxThreads()), fitted(other.fitted)
|
||||||
{
|
{
|
||||||
@@ -104,8 +103,11 @@ namespace bayesnet {
|
|||||||
{
|
{
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
void Network::checkFitData(int n_samples, int n_features, int n_samples_y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
void Network::checkFitData(int n_samples, int n_features, int n_samples_y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
|
if (weights.size(0) != n_samples) {
|
||||||
|
throw invalid_argument("Weights (" + to_string(weights.size(0)) + ") must have the same number of elements as samples (" + to_string(n_samples) + ") in Network::fit");
|
||||||
|
}
|
||||||
if (n_samples != n_samples_y) {
|
if (n_samples != n_samples_y) {
|
||||||
throw invalid_argument("X and y must have the same number of samples in Network::fit (" + to_string(n_samples) + " != " + to_string(n_samples_y) + ")");
|
throw invalid_argument("X and y must have the same number of samples in Network::fit (" + to_string(n_samples) + " != " + to_string(n_samples_y) + ")");
|
||||||
}
|
}
|
||||||
@@ -136,28 +138,29 @@ namespace bayesnet {
|
|||||||
classNumStates = nodes[className]->getNumStates();
|
classNumStates = nodes[className]->getNumStates();
|
||||||
}
|
}
|
||||||
// X comes in nxm, where n is the number of features and m the number of samples
|
// 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 vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
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)
|
||||||
{
|
{
|
||||||
checkFitData(X.size(1), X.size(0), y.size(0), featureNames, className, states);
|
checkFitData(X.size(1), X.size(0), y.size(0), featureNames, className, states, weights);
|
||||||
this->className = className;
|
this->className = className;
|
||||||
Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
|
Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
|
||||||
samples = torch::cat({ X , ytmp }, 0);
|
samples = torch::cat({ X , ytmp }, 0);
|
||||||
for (int i = 0; i < featureNames.size(); ++i) {
|
for (int i = 0; i < featureNames.size(); ++i) {
|
||||||
auto row_feature = X.index({ i, "..." });
|
auto row_feature = X.index({ i, "..." });
|
||||||
}
|
}
|
||||||
completeFit(states);
|
completeFit(states, weights);
|
||||||
}
|
}
|
||||||
void Network::fit(const torch::Tensor& samples, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
void Network::fit(const torch::Tensor& samples, const torch::Tensor& weights, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
||||||
{
|
{
|
||||||
checkFitData(samples.size(1), samples.size(0) - 1, samples.size(1), featureNames, className, states);
|
checkFitData(samples.size(1), samples.size(0) - 1, samples.size(1), featureNames, className, states, weights);
|
||||||
this->className = className;
|
this->className = className;
|
||||||
this->samples = samples;
|
this->samples = samples;
|
||||||
completeFit(states);
|
completeFit(states, weights);
|
||||||
}
|
}
|
||||||
// input_data comes in nxm, where n is the number of features and m the number of samples
|
// input_data comes in nxm, where n is the number of features and m the number of samples
|
||||||
void Network::fit(const vector<vector<int>>& input_data, const vector<int>& labels, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
void Network::fit(const vector<vector<int>>& input_data, const vector<int>& labels, const vector<float>& weights_, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
|
||||||
{
|
{
|
||||||
checkFitData(input_data[0].size(), input_data.size(), labels.size(), featureNames, className, states);
|
const torch::Tensor weights = torch::tensor(weights_, torch::kFloat64);
|
||||||
|
checkFitData(input_data[0].size(), input_data.size(), labels.size(), featureNames, className, states, weights);
|
||||||
this->className = className;
|
this->className = className;
|
||||||
// Build tensor of samples (nxm) (n+1 because of the class)
|
// Build tensor of samples (nxm) (n+1 because of the class)
|
||||||
samples = torch::zeros({ static_cast<int>(input_data.size() + 1), static_cast<int>(input_data[0].size()) }, torch::kInt32);
|
samples = torch::zeros({ static_cast<int>(input_data.size() + 1), static_cast<int>(input_data[0].size()) }, torch::kInt32);
|
||||||
@@ -165,11 +168,12 @@ namespace bayesnet {
|
|||||||
samples.index_put_({ i, "..." }, torch::tensor(input_data[i], torch::kInt32));
|
samples.index_put_({ i, "..." }, torch::tensor(input_data[i], torch::kInt32));
|
||||||
}
|
}
|
||||||
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
|
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
|
||||||
completeFit(states);
|
completeFit(states, weights);
|
||||||
}
|
}
|
||||||
void Network::completeFit(const map<string, vector<int>>& states)
|
void Network::completeFit(const map<string, vector<int>>& states, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
setStates(states);
|
setStates(states);
|
||||||
|
laplaceSmoothing = 1.0 / samples.size(1); // To use in CPT computation
|
||||||
int maxThreadsRunning = static_cast<int>(std::thread::hardware_concurrency() * maxThreads);
|
int maxThreadsRunning = static_cast<int>(std::thread::hardware_concurrency() * maxThreads);
|
||||||
if (maxThreadsRunning < 1) {
|
if (maxThreadsRunning < 1) {
|
||||||
maxThreadsRunning = 1;
|
maxThreadsRunning = 1;
|
||||||
@@ -182,7 +186,7 @@ namespace bayesnet {
|
|||||||
while (nextNodeIndex < nodes.size()) {
|
while (nextNodeIndex < nodes.size()) {
|
||||||
unique_lock<mutex> lock(mtx);
|
unique_lock<mutex> lock(mtx);
|
||||||
cv.wait(lock, [&activeThreads, &maxThreadsRunning]() { return activeThreads < maxThreadsRunning; });
|
cv.wait(lock, [&activeThreads, &maxThreadsRunning]() { return activeThreads < maxThreadsRunning; });
|
||||||
threads.emplace_back([this, &nextNodeIndex, &mtx, &cv, &activeThreads]() {
|
threads.emplace_back([this, &nextNodeIndex, &mtx, &cv, &activeThreads, &weights]() {
|
||||||
while (true) {
|
while (true) {
|
||||||
unique_lock<mutex> lock(mtx);
|
unique_lock<mutex> lock(mtx);
|
||||||
if (nextNodeIndex >= nodes.size()) {
|
if (nextNodeIndex >= nodes.size()) {
|
||||||
@@ -191,7 +195,7 @@ namespace bayesnet {
|
|||||||
auto& pair = *std::next(nodes.begin(), nextNodeIndex);
|
auto& pair = *std::next(nodes.begin(), nextNodeIndex);
|
||||||
++nextNodeIndex;
|
++nextNodeIndex;
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
pair.second->computeCPT(samples, features, laplaceSmoothing);
|
pair.second->computeCPT(samples, features, laplaceSmoothing, weights);
|
||||||
lock.lock();
|
lock.lock();
|
||||||
nodes[pair.first] = std::move(pair.second);
|
nodes[pair.first] = std::move(pair.second);
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
@@ -343,7 +347,7 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
// Normalize result
|
// Normalize result
|
||||||
double sum = accumulate(result.begin(), result.end(), 0.0);
|
double sum = accumulate(result.begin(), result.end(), 0.0);
|
||||||
transform(result.begin(), result.end(), result.begin(), [sum](double& value) { return value / sum; });
|
transform(result.begin(), result.end(), result.begin(), [sum](const double& value) { return value / sum; });
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
vector<string> Network::show() const
|
vector<string> Network::show() const
|
||||||
@@ -431,6 +435,7 @@ namespace bayesnet {
|
|||||||
{
|
{
|
||||||
for (auto& node : nodes) {
|
for (auto& node : nodes) {
|
||||||
cout << "* " << node.first << ": (" << node.second->getNumStates() << ") : " << node.second->getCPT().sizes() << endl;
|
cout << "* " << node.first << ": (" << node.second->getNumStates() << ") : " << node.second->getCPT().sizes() << endl;
|
||||||
|
cout << node.second->getCPT() << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -13,19 +13,18 @@ namespace bayesnet {
|
|||||||
int classNumStates;
|
int classNumStates;
|
||||||
vector<string> features; // Including classname
|
vector<string> features; // Including classname
|
||||||
string className;
|
string className;
|
||||||
int laplaceSmoothing = 1;
|
double laplaceSmoothing;
|
||||||
torch::Tensor samples; // nxm tensor used to fit the model
|
torch::Tensor samples; // nxm tensor used to fit the model
|
||||||
bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&);
|
bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&);
|
||||||
vector<double> predict_sample(const vector<int>&);
|
vector<double> predict_sample(const vector<int>&);
|
||||||
vector<double> predict_sample(const torch::Tensor&);
|
vector<double> predict_sample(const torch::Tensor&);
|
||||||
vector<double> exactInference(map<string, int>&);
|
vector<double> exactInference(map<string, int>&);
|
||||||
double computeFactor(map<string, int>&);
|
double computeFactor(map<string, int>&);
|
||||||
void completeFit(const map<string, vector<int>>&);
|
void completeFit(const map<string, vector<int>>& states, const torch::Tensor& weights);
|
||||||
void checkFitData(int n_features, int n_samples, int n_samples_y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>&);
|
void checkFitData(int n_features, int n_samples, int n_samples_y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states, const torch::Tensor& weights);
|
||||||
void setStates(const map<string, vector<int>>&);
|
void setStates(const map<string, vector<int>>&);
|
||||||
public:
|
public:
|
||||||
Network();
|
Network();
|
||||||
explicit Network(float, int);
|
|
||||||
explicit Network(float);
|
explicit Network(float);
|
||||||
explicit Network(Network&);
|
explicit Network(Network&);
|
||||||
torch::Tensor& getSamples();
|
torch::Tensor& getSamples();
|
||||||
@@ -39,9 +38,9 @@ namespace bayesnet {
|
|||||||
int getNumEdges() const;
|
int getNumEdges() const;
|
||||||
int getClassNumStates() const;
|
int getClassNumStates() const;
|
||||||
string getClassName() const;
|
string getClassName() const;
|
||||||
void fit(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&, const map<string, vector<int>>&);
|
void fit(const vector<vector<int>>& input_data, const vector<int>& labels, const vector<float>& weights, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states);
|
||||||
void fit(const torch::Tensor&, const torch::Tensor&, const vector<string>&, const string&, const map<string, vector<int>>&);
|
void 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);
|
||||||
void fit(const torch::Tensor&, const vector<string>&, const string&, const map<string, vector<int>>&);
|
void fit(const torch::Tensor& samples, const torch::Tensor& weights, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states);
|
||||||
vector<int> predict(const vector<vector<int>>&); // Return mx1 vector of predictions
|
vector<int> predict(const vector<vector<int>>&); // Return mx1 vector of predictions
|
||||||
torch::Tensor predict(const torch::Tensor&); // Return mx1 tensor of predictions
|
torch::Tensor predict(const torch::Tensor&); // Return mx1 tensor of predictions
|
||||||
torch::Tensor predict_tensor(const torch::Tensor& samples, const bool proba);
|
torch::Tensor predict_tensor(const torch::Tensor& samples, const bool proba);
|
||||||
|
@@ -84,7 +84,7 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
void Node::computeCPT(const torch::Tensor& dataset, const vector<string>& features, const int laplaceSmoothing)
|
void Node::computeCPT(const torch::Tensor& dataset, const vector<string>& features, const double laplaceSmoothing, const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
dimensions.clear();
|
dimensions.clear();
|
||||||
// Get dimensions of the CPT
|
// Get dimensions of the CPT
|
||||||
@@ -111,7 +111,7 @@ namespace bayesnet {
|
|||||||
coordinates.push_back(dataset.index({ parent_index, n_sample }));
|
coordinates.push_back(dataset.index({ parent_index, n_sample }));
|
||||||
}
|
}
|
||||||
// Increment the count of the corresponding coordinate
|
// Increment the count of the corresponding coordinate
|
||||||
cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + 1);
|
cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + weights.index({ n_sample }).item<double>());
|
||||||
}
|
}
|
||||||
// Normalize the counts
|
// Normalize the counts
|
||||||
cpTable = cpTable / cpTable.sum(0);
|
cpTable = cpTable / cpTable.sum(0);
|
||||||
|
@@ -26,7 +26,7 @@ namespace bayesnet {
|
|||||||
vector<Node*>& getParents();
|
vector<Node*>& getParents();
|
||||||
vector<Node*>& getChildren();
|
vector<Node*>& getChildren();
|
||||||
torch::Tensor& getCPT();
|
torch::Tensor& getCPT();
|
||||||
void computeCPT(const torch::Tensor&, const vector<string>&, const int);
|
void computeCPT(const torch::Tensor& dataset, const vector<string>& features, const double laplaceSmoothing, const torch::Tensor& weights);
|
||||||
int getNumStates() const;
|
int getNumStates() const;
|
||||||
void setNumStates(int);
|
void setNumStates(int);
|
||||||
unsigned minFill();
|
unsigned minFill();
|
||||||
|
@@ -65,7 +65,8 @@ namespace bayesnet {
|
|||||||
//Update new states of the feature/node
|
//Update new states of the feature/node
|
||||||
states[pFeatures[index]] = xStates;
|
states[pFeatures[index]] = xStates;
|
||||||
}
|
}
|
||||||
model.fit(pDataset, pFeatures, pClassName, states);
|
const torch::Tensor weights = torch::full({ pDataset.size(1) }, 1.0 / pDataset.size(1), torch::kDouble);
|
||||||
|
model.fit(pDataset, weights, pFeatures, pClassName, states);
|
||||||
}
|
}
|
||||||
return states;
|
return states;
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,7 @@ namespace bayesnet {
|
|||||||
|
|
||||||
SPODE::SPODE(int root) : Classifier(Network()), root(root) {}
|
SPODE::SPODE(int root) : Classifier(Network()), root(root) {}
|
||||||
|
|
||||||
void SPODE::buildModel()
|
void SPODE::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
// 0. Add all nodes to the model
|
// 0. Add all nodes to the model
|
||||||
addNodes();
|
addNodes();
|
||||||
|
@@ -7,7 +7,7 @@ namespace bayesnet {
|
|||||||
private:
|
private:
|
||||||
int root;
|
int root;
|
||||||
protected:
|
protected:
|
||||||
void buildModel() override;
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
explicit SPODE(int root);
|
explicit SPODE(int root);
|
||||||
virtual ~SPODE() {};
|
virtual ~SPODE() {};
|
||||||
|
@@ -21,7 +21,6 @@ namespace bayesnet {
|
|||||||
SPODELd& SPODELd::fit(torch::Tensor& dataset, vector<string>& features_, string className_, map<string, vector<int>>& states_)
|
SPODELd& SPODELd::fit(torch::Tensor& dataset, vector<string>& features_, string className_, map<string, vector<int>>& states_)
|
||||||
{
|
{
|
||||||
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
|
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
|
||||||
cout << "Xf " << Xf.sizes() << " dtype: " << Xf.dtype() << endl;
|
|
||||||
y = dataset.index({ -1, "..." }).clone();
|
y = dataset.index({ -1, "..." }).clone();
|
||||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
||||||
features = features_;
|
features = features_;
|
||||||
|
@@ -5,7 +5,7 @@ namespace bayesnet {
|
|||||||
|
|
||||||
TAN::TAN() : Classifier(Network()) {}
|
TAN::TAN() : Classifier(Network()) {}
|
||||||
|
|
||||||
void TAN::buildModel()
|
void TAN::buildModel(const torch::Tensor& weights)
|
||||||
{
|
{
|
||||||
// 0. Add all nodes to the model
|
// 0. Add all nodes to the model
|
||||||
addNodes();
|
addNodes();
|
||||||
@@ -15,15 +15,15 @@ namespace bayesnet {
|
|||||||
Tensor class_dataset = dataset.index({ -1, "..." });
|
Tensor class_dataset = dataset.index({ -1, "..." });
|
||||||
for (int i = 0; i < static_cast<int>(features.size()); ++i) {
|
for (int i = 0; i < static_cast<int>(features.size()); ++i) {
|
||||||
Tensor feature_dataset = dataset.index({ i, "..." });
|
Tensor feature_dataset = dataset.index({ i, "..." });
|
||||||
auto mi_value = metrics.mutualInformation(class_dataset, feature_dataset);
|
auto mi_value = metrics.mutualInformation(class_dataset, feature_dataset, weights);
|
||||||
mi.push_back({ i, mi_value });
|
mi.push_back({ i, mi_value });
|
||||||
}
|
}
|
||||||
sort(mi.begin(), mi.end(), [](const auto& left, const auto& right) {return left.second < right.second;});
|
sort(mi.begin(), mi.end(), [](const auto& left, const auto& right) {return left.second < right.second;});
|
||||||
auto root = mi[mi.size() - 1].first;
|
auto root = mi[mi.size() - 1].first;
|
||||||
// 2. Compute mutual information between each feature and the class
|
// 2. Compute mutual information between each feature and the class
|
||||||
auto weights = metrics.conditionalEdge();
|
auto weights_matrix = metrics.conditionalEdge(weights);
|
||||||
// 3. Compute the maximum spanning tree
|
// 3. Compute the maximum spanning tree
|
||||||
auto mst = metrics.maximumSpanningTree(features, weights, root);
|
auto mst = metrics.maximumSpanningTree(features, weights_matrix, root);
|
||||||
// 4. Add edges from the maximum spanning tree to the model
|
// 4. Add edges from the maximum spanning tree to the model
|
||||||
for (auto i = 0; i < mst.size(); ++i) {
|
for (auto i = 0; i < mst.size(); ++i) {
|
||||||
auto [from, to] = mst[i];
|
auto [from, to] = mst[i];
|
||||||
|
@@ -7,7 +7,7 @@ namespace bayesnet {
|
|||||||
class TAN : public Classifier {
|
class TAN : public Classifier {
|
||||||
private:
|
private:
|
||||||
protected:
|
protected:
|
||||||
void buildModel() override;
|
void buildModel(const torch::Tensor& weights) override;
|
||||||
public:
|
public:
|
||||||
TAN();
|
TAN();
|
||||||
virtual ~TAN() {};
|
virtual ~TAN() {};
|
||||||
|
@@ -4,7 +4,7 @@ namespace bayesnet {
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace torch;
|
using namespace torch;
|
||||||
// Return the indices in descending order
|
// Return the indices in descending order
|
||||||
vector<int> argsort(vector<float>& nums)
|
vector<int> argsort(vector<double>& nums)
|
||||||
{
|
{
|
||||||
int n = nums.size();
|
int n = nums.size();
|
||||||
vector<int> indices(n);
|
vector<int> indices(n);
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace torch;
|
using namespace torch;
|
||||||
vector<int> argsort(vector<float>& nums);
|
vector<int> argsort(vector<double>& nums);
|
||||||
vector<vector<int>> tensorToVector(Tensor& tensor);
|
vector<vector<int>> tensorToVector(Tensor& tensor);
|
||||||
}
|
}
|
||||||
#endif //BAYESNET_UTILS_H
|
#endif //BAYESNET_UTILS_H
|
@@ -6,5 +6,7 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include)
|
|||||||
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
|
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
|
||||||
add_executable(main main.cc Folding.cc platformUtils.cc Experiment.cc Datasets.cc Models.cc Report.cc)
|
add_executable(main main.cc Folding.cc platformUtils.cc Experiment.cc Datasets.cc Models.cc Report.cc)
|
||||||
add_executable(manage manage.cc Results.cc Report.cc)
|
add_executable(manage manage.cc Results.cc Report.cc)
|
||||||
|
add_executable(list list.cc platformUtils Datasets.cc)
|
||||||
target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
|
target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
|
||||||
target_link_libraries(manage "${TORCH_LIBRARIES}")
|
target_link_libraries(manage "${TORCH_LIBRARIES}")
|
||||||
|
target_link_libraries(list ArffFiles mdlp "${TORCH_LIBRARIES}")
|
@@ -24,75 +24,110 @@ namespace platform {
|
|||||||
transform(datasets.begin(), datasets.end(), back_inserter(result), [](const auto& d) { return d.first; });
|
transform(datasets.begin(), datasets.end(), back_inserter(result), [](const auto& d) { return d.first; });
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
vector<string> Datasets::getFeatures(string name)
|
vector<string> Datasets::getFeatures(const string& name) const
|
||||||
{
|
{
|
||||||
if (datasets[name]->isLoaded()) {
|
if (datasets.at(name)->isLoaded()) {
|
||||||
return datasets[name]->getFeatures();
|
return datasets.at(name)->getFeatures();
|
||||||
} else {
|
} else {
|
||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
map<string, vector<int>> Datasets::getStates(string name)
|
map<string, vector<int>> Datasets::getStates(const string& name) const
|
||||||
{
|
{
|
||||||
if (datasets[name]->isLoaded()) {
|
if (datasets.at(name)->isLoaded()) {
|
||||||
return datasets[name]->getStates();
|
return datasets.at(name)->getStates();
|
||||||
} else {
|
} else {
|
||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
string Datasets::getClassName(string name)
|
void Datasets::loadDataset(const string& name) const
|
||||||
{
|
{
|
||||||
if (datasets[name]->isLoaded()) {
|
if (datasets.at(name)->isLoaded()) {
|
||||||
return datasets[name]->getClassName();
|
return;
|
||||||
|
} else {
|
||||||
|
datasets.at(name)->load();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
string Datasets::getClassName(const string& name) const
|
||||||
|
{
|
||||||
|
if (datasets.at(name)->isLoaded()) {
|
||||||
|
return datasets.at(name)->getClassName();
|
||||||
} else {
|
} else {
|
||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int Datasets::getNSamples(string name)
|
int Datasets::getNSamples(const string& name) const
|
||||||
{
|
{
|
||||||
if (datasets[name]->isLoaded()) {
|
if (datasets.at(name)->isLoaded()) {
|
||||||
return datasets[name]->getNSamples();
|
return datasets.at(name)->getNSamples();
|
||||||
} else {
|
} else {
|
||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pair<vector<vector<float>>&, vector<int>&> Datasets::getVectors(string name)
|
int Datasets::getNClasses(const string& name)
|
||||||
|
{
|
||||||
|
if (datasets.at(name)->isLoaded()) {
|
||||||
|
auto className = datasets.at(name)->getClassName();
|
||||||
|
if (discretize) {
|
||||||
|
auto states = getStates(name);
|
||||||
|
return states.at(className).size();
|
||||||
|
}
|
||||||
|
auto [Xv, yv] = getVectors(name);
|
||||||
|
return *max_element(yv.begin(), yv.end()) + 1;
|
||||||
|
} else {
|
||||||
|
throw invalid_argument("Dataset not loaded.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vector<int> Datasets::getClassesCounts(const string& name) const
|
||||||
|
{
|
||||||
|
if (datasets.at(name)->isLoaded()) {
|
||||||
|
auto [Xv, yv] = datasets.at(name)->getVectors();
|
||||||
|
vector<int> counts(*max_element(yv.begin(), yv.end()) + 1);
|
||||||
|
for (auto y : yv) {
|
||||||
|
counts[y]++;
|
||||||
|
}
|
||||||
|
return counts;
|
||||||
|
} else {
|
||||||
|
throw invalid_argument("Dataset not loaded.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pair<vector<vector<float>>&, vector<int>&> Datasets::getVectors(const string& name)
|
||||||
{
|
{
|
||||||
if (!datasets[name]->isLoaded()) {
|
if (!datasets[name]->isLoaded()) {
|
||||||
datasets[name]->load();
|
datasets[name]->load();
|
||||||
}
|
}
|
||||||
return datasets[name]->getVectors();
|
return datasets[name]->getVectors();
|
||||||
}
|
}
|
||||||
pair<vector<vector<int>>&, vector<int>&> Datasets::getVectorsDiscretized(string name)
|
pair<vector<vector<int>>&, vector<int>&> Datasets::getVectorsDiscretized(const string& name)
|
||||||
{
|
{
|
||||||
if (!datasets[name]->isLoaded()) {
|
if (!datasets[name]->isLoaded()) {
|
||||||
datasets[name]->load();
|
datasets[name]->load();
|
||||||
}
|
}
|
||||||
return datasets[name]->getVectorsDiscretized();
|
return datasets[name]->getVectorsDiscretized();
|
||||||
}
|
}
|
||||||
pair<torch::Tensor&, torch::Tensor&> Datasets::getTensors(string name)
|
pair<torch::Tensor&, torch::Tensor&> Datasets::getTensors(const string& name)
|
||||||
{
|
{
|
||||||
if (!datasets[name]->isLoaded()) {
|
if (!datasets[name]->isLoaded()) {
|
||||||
datasets[name]->load();
|
datasets[name]->load();
|
||||||
}
|
}
|
||||||
return datasets[name]->getTensors();
|
return datasets[name]->getTensors();
|
||||||
}
|
}
|
||||||
bool Datasets::isDataset(const string& name)
|
bool Datasets::isDataset(const string& name) const
|
||||||
{
|
{
|
||||||
return datasets.find(name) != datasets.end();
|
return datasets.find(name) != datasets.end();
|
||||||
}
|
}
|
||||||
Dataset::Dataset(const Dataset& dataset) : path(dataset.path), name(dataset.name), className(dataset.className), n_samples(dataset.n_samples), n_features(dataset.n_features), features(dataset.features), states(dataset.states), loaded(dataset.loaded), discretize(dataset.discretize), X(dataset.X), y(dataset.y), Xv(dataset.Xv), Xd(dataset.Xd), yv(dataset.yv), fileType(dataset.fileType)
|
Dataset::Dataset(const Dataset& dataset) : path(dataset.path), name(dataset.name), className(dataset.className), n_samples(dataset.n_samples), n_features(dataset.n_features), features(dataset.features), states(dataset.states), loaded(dataset.loaded), discretize(dataset.discretize), X(dataset.X), y(dataset.y), Xv(dataset.Xv), Xd(dataset.Xd), yv(dataset.yv), fileType(dataset.fileType)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
string Dataset::getName()
|
string Dataset::getName() const
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
string Dataset::getClassName()
|
string Dataset::getClassName() const
|
||||||
{
|
{
|
||||||
return className;
|
return className;
|
||||||
}
|
}
|
||||||
vector<string> Dataset::getFeatures()
|
vector<string> Dataset::getFeatures() const
|
||||||
{
|
{
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
return features;
|
return features;
|
||||||
@@ -100,7 +135,7 @@ namespace platform {
|
|||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int Dataset::getNFeatures()
|
int Dataset::getNFeatures() const
|
||||||
{
|
{
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
return n_features;
|
return n_features;
|
||||||
@@ -108,7 +143,7 @@ namespace platform {
|
|||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int Dataset::getNSamples()
|
int Dataset::getNSamples() const
|
||||||
{
|
{
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
return n_samples;
|
return n_samples;
|
||||||
@@ -116,7 +151,7 @@ namespace platform {
|
|||||||
throw invalid_argument("Dataset not loaded.");
|
throw invalid_argument("Dataset not loaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
map<string, vector<int>> Dataset::getStates()
|
map<string, vector<int>> Dataset::getStates() const
|
||||||
{
|
{
|
||||||
if (loaded) {
|
if (loaded) {
|
||||||
return states;
|
return states;
|
||||||
|
@@ -29,15 +29,15 @@ namespace platform {
|
|||||||
public:
|
public:
|
||||||
Dataset(const string& path, const string& name, const string& className, bool discretize, fileType_t fileType) : path(path), name(name), className(className), discretize(discretize), loaded(false), fileType(fileType) {};
|
Dataset(const string& path, const string& name, const string& className, bool discretize, fileType_t fileType) : path(path), name(name), className(className), discretize(discretize), loaded(false), fileType(fileType) {};
|
||||||
explicit Dataset(const Dataset&);
|
explicit Dataset(const Dataset&);
|
||||||
string getName();
|
string getName() const;
|
||||||
string getClassName();
|
string getClassName() const;
|
||||||
vector<string> getFeatures();
|
vector<string> getFeatures() const;
|
||||||
map<string, vector<int>> getStates();
|
map<string, vector<int>> getStates() const;
|
||||||
pair<vector<vector<float>>&, vector<int>&> getVectors();
|
pair<vector<vector<float>>&, vector<int>&> getVectors();
|
||||||
pair<vector<vector<int>>&, vector<int>&> getVectorsDiscretized();
|
pair<vector<vector<int>>&, vector<int>&> getVectorsDiscretized();
|
||||||
pair<torch::Tensor&, torch::Tensor&> getTensors();
|
pair<torch::Tensor&, torch::Tensor&> getTensors();
|
||||||
int getNFeatures();
|
int getNFeatures() const;
|
||||||
int getNSamples();
|
int getNSamples() const;
|
||||||
void load();
|
void load();
|
||||||
const bool inline isLoaded() const { return loaded; };
|
const bool inline isLoaded() const { return loaded; };
|
||||||
};
|
};
|
||||||
@@ -51,14 +51,17 @@ namespace platform {
|
|||||||
public:
|
public:
|
||||||
explicit Datasets(const string& path, bool discretize = false, fileType_t fileType = ARFF) : path(path), discretize(discretize), fileType(fileType) { load(); };
|
explicit Datasets(const string& path, bool discretize = false, fileType_t fileType = ARFF) : path(path), discretize(discretize), fileType(fileType) { load(); };
|
||||||
vector<string> getNames();
|
vector<string> getNames();
|
||||||
vector<string> getFeatures(string name);
|
vector<string> getFeatures(const string& name) const;
|
||||||
int getNSamples(string name);
|
int getNSamples(const string& name) const;
|
||||||
string getClassName(string name);
|
string getClassName(const string& name) const;
|
||||||
map<string, vector<int>> getStates(string name);
|
int getNClasses(const string& name);
|
||||||
pair<vector<vector<float>>&, vector<int>&> getVectors(string name);
|
vector<int> getClassesCounts(const string& name) const;
|
||||||
pair<vector<vector<int>>&, vector<int>&> getVectorsDiscretized(string name);
|
map<string, vector<int>> getStates(const string& name) const;
|
||||||
pair<torch::Tensor&, torch::Tensor&> getTensors(string name);
|
pair<vector<vector<float>>&, vector<int>&> getVectors(const string& name);
|
||||||
bool isDataset(const string& name);
|
pair<vector<vector<int>>&, vector<int>&> getVectorsDiscretized(const string& name);
|
||||||
|
pair<torch::Tensor&, torch::Tensor&> getTensors(const string& name);
|
||||||
|
bool isDataset(const string& name) const;
|
||||||
|
void loadDataset(const string& name) const;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -10,6 +10,7 @@
|
|||||||
#include "KDBLd.h"
|
#include "KDBLd.h"
|
||||||
#include "SPODELd.h"
|
#include "SPODELd.h"
|
||||||
#include "AODELd.h"
|
#include "AODELd.h"
|
||||||
|
#include "BoostAODE.h"
|
||||||
namespace platform {
|
namespace platform {
|
||||||
class Models {
|
class Models {
|
||||||
private:
|
private:
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#ifndef PATHS_H
|
#ifndef PATHS_H
|
||||||
#define PATHS_H
|
#define PATHS_H
|
||||||
|
#include <string>
|
||||||
namespace platform {
|
namespace platform {
|
||||||
class Paths {
|
class Paths {
|
||||||
public:
|
public:
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
|
#include <sstream>
|
||||||
|
#include <locale>
|
||||||
#include "Report.h"
|
#include "Report.h"
|
||||||
#include "BestResult.h"
|
#include "BestResult.h"
|
||||||
|
|
||||||
|
|
||||||
namespace platform {
|
namespace platform {
|
||||||
string headerLine(const string& text)
|
string headerLine(const string& text)
|
||||||
{
|
{
|
||||||
@@ -10,20 +13,27 @@ namespace platform {
|
|||||||
}
|
}
|
||||||
string Report::fromVector(const string& key)
|
string Report::fromVector(const string& key)
|
||||||
{
|
{
|
||||||
string result = "";
|
stringstream oss;
|
||||||
|
string sep = "";
|
||||||
|
oss << "[";
|
||||||
for (auto& item : data[key]) {
|
for (auto& item : data[key]) {
|
||||||
result += to_string(item) + ", ";
|
oss << sep << item.get<double>();
|
||||||
|
sep = ", ";
|
||||||
}
|
}
|
||||||
return "[" + result.substr(0, result.size() - 2) + "]";
|
oss << "]";
|
||||||
|
return oss.str();
|
||||||
}
|
}
|
||||||
string fVector(const json& data)
|
string fVector(const string& title, const json& data, const int width, const int precision)
|
||||||
{
|
{
|
||||||
string result = "";
|
stringstream oss;
|
||||||
|
string sep = "";
|
||||||
|
oss << title << "[";
|
||||||
for (const auto& item : data) {
|
for (const auto& item : data) {
|
||||||
result += to_string(item) + ", ";
|
oss << sep << fixed << setw(width) << setprecision(precision) << item.get<double>();
|
||||||
|
sep = ", ";
|
||||||
}
|
}
|
||||||
return "[" + result.substr(0, result.size() - 2) + "]";
|
oss << "]";
|
||||||
|
return oss.str();
|
||||||
}
|
}
|
||||||
void Report::show()
|
void Report::show()
|
||||||
{
|
{
|
||||||
@@ -31,21 +41,31 @@ namespace platform {
|
|||||||
body();
|
body();
|
||||||
footer();
|
footer();
|
||||||
}
|
}
|
||||||
|
struct separated : numpunct<char> {
|
||||||
|
char do_decimal_point() const { return ','; }
|
||||||
|
char do_thousands_sep() const { return '.'; }
|
||||||
|
string do_grouping() const { return "\03"; }
|
||||||
|
};
|
||||||
void Report::header()
|
void Report::header()
|
||||||
{
|
{
|
||||||
|
locale mylocale(cout.getloc(), new separated);
|
||||||
|
locale::global(mylocale);
|
||||||
|
cout.imbue(mylocale);
|
||||||
|
stringstream oss;
|
||||||
cout << Colors::MAGENTA() << string(MAXL, '*') << endl;
|
cout << Colors::MAGENTA() << string(MAXL, '*') << endl;
|
||||||
cout << headerLine("Report " + data["model"].get<string>() + " ver. " + data["version"].get<string>() + " with " + to_string(data["folds"].get<int>()) + " Folds cross validation and " + to_string(data["seeds"].size()) + " random seeds. " + data["date"].get<string>() + " " + data["time"].get<string>());
|
cout << headerLine("Report " + data["model"].get<string>() + " ver. " + data["version"].get<string>() + " with " + to_string(data["folds"].get<int>()) + " Folds cross validation and " + to_string(data["seeds"].size()) + " random seeds. " + data["date"].get<string>() + " " + data["time"].get<string>());
|
||||||
cout << headerLine(data["title"].get<string>());
|
cout << headerLine(data["title"].get<string>());
|
||||||
cout << headerLine("Random seeds: " + fromVector("seeds") + " Stratified: " + (data["stratified"].get<bool>() ? "True" : "False"));
|
cout << headerLine("Random seeds: " + fromVector("seeds") + " Stratified: " + (data["stratified"].get<bool>() ? "True" : "False"));
|
||||||
cout << headerLine("Execution took " + to_string(data["duration"].get<float>()) + " seconds, " + to_string(data["duration"].get<float>() / 3600) + " hours, on " + data["platform"].get<string>());
|
oss << "Execution took " << setprecision(2) << fixed << data["duration"].get<float>() << " seconds, " << data["duration"].get<float>() / 3600 << " hours, on " << data["platform"].get<string>();
|
||||||
|
cout << headerLine(oss.str());
|
||||||
cout << headerLine("Score is " + data["score_name"].get<string>());
|
cout << headerLine("Score is " + data["score_name"].get<string>());
|
||||||
cout << string(MAXL, '*') << endl;
|
cout << string(MAXL, '*') << endl;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
void Report::body()
|
void Report::body()
|
||||||
{
|
{
|
||||||
cout << Colors::GREEN() << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl;
|
cout << Colors::GREEN() << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl;
|
||||||
cout << "============================== ====== ===== === ======= ======= ======= =============== ================== ===============" << endl;
|
cout << "============================== ====== ===== === ========= ========= ========= =============== ================== ===============" << endl;
|
||||||
json lastResult;
|
json lastResult;
|
||||||
totalScore = 0;
|
totalScore = 0;
|
||||||
bool odd = true;
|
bool odd = true;
|
||||||
@@ -55,9 +75,9 @@ namespace platform {
|
|||||||
cout << setw(6) << right << r["samples"].get<int>() << " ";
|
cout << setw(6) << right << r["samples"].get<int>() << " ";
|
||||||
cout << setw(5) << right << r["features"].get<int>() << " ";
|
cout << setw(5) << right << r["features"].get<int>() << " ";
|
||||||
cout << setw(3) << right << r["classes"].get<int>() << " ";
|
cout << setw(3) << right << r["classes"].get<int>() << " ";
|
||||||
cout << setw(7) << setprecision(2) << fixed << r["nodes"].get<float>() << " ";
|
cout << setw(9) << setprecision(2) << fixed << r["nodes"].get<float>() << " ";
|
||||||
cout << setw(7) << setprecision(2) << fixed << r["leaves"].get<float>() << " ";
|
cout << setw(9) << setprecision(2) << fixed << r["leaves"].get<float>() << " ";
|
||||||
cout << setw(7) << setprecision(2) << fixed << r["depth"].get<float>() << " ";
|
cout << setw(9) << setprecision(2) << fixed << r["depth"].get<float>() << " ";
|
||||||
cout << setw(8) << right << setprecision(6) << fixed << r["score"].get<double>() << "±" << setw(6) << setprecision(4) << fixed << r["score_std"].get<double>() << " ";
|
cout << setw(8) << right << setprecision(6) << fixed << r["score"].get<double>() << "±" << setw(6) << setprecision(4) << fixed << r["score_std"].get<double>() << " ";
|
||||||
cout << setw(11) << right << setprecision(6) << fixed << r["time"].get<double>() << "±" << setw(6) << setprecision(4) << fixed << r["time_std"].get<double>() << " ";
|
cout << setw(11) << right << setprecision(6) << fixed << r["time"].get<double>() << "±" << setw(6) << setprecision(4) << fixed << r["time_std"].get<double>() << " ";
|
||||||
try {
|
try {
|
||||||
@@ -73,10 +93,10 @@ namespace platform {
|
|||||||
}
|
}
|
||||||
if (data["results"].size() == 1) {
|
if (data["results"].size() == 1) {
|
||||||
cout << string(MAXL, '*') << endl;
|
cout << string(MAXL, '*') << endl;
|
||||||
cout << headerLine("Train scores: " + fVector(lastResult["scores_train"]));
|
cout << headerLine(fVector("Train scores: ", lastResult["scores_train"], 14, 12));
|
||||||
cout << headerLine("Test scores: " + fVector(lastResult["scores_test"]));
|
cout << headerLine(fVector("Test scores: ", lastResult["scores_test"], 14, 12));
|
||||||
cout << headerLine("Train times: " + fVector(lastResult["times_train"]));
|
cout << headerLine(fVector("Train times: ", lastResult["times_train"], 10, 3));
|
||||||
cout << headerLine("Test times: " + fVector(lastResult["times_test"]));
|
cout << headerLine(fVector("Test times: ", lastResult["times_test"], 10, 3));
|
||||||
cout << string(MAXL, '*') << endl;
|
cout << string(MAXL, '*') << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,7 +105,9 @@ namespace platform {
|
|||||||
cout << Colors::MAGENTA() << string(MAXL, '*') << endl;
|
cout << Colors::MAGENTA() << string(MAXL, '*') << endl;
|
||||||
auto score = data["score_name"].get<string>();
|
auto score = data["score_name"].get<string>();
|
||||||
if (score == BestResult::scoreName()) {
|
if (score == BestResult::scoreName()) {
|
||||||
cout << headerLine(score + " compared to " + BestResult::title() + " .: " + to_string(totalScore / BestResult::score()));
|
stringstream oss;
|
||||||
|
oss << score << " compared to " << BestResult::title() << " .: " << totalScore / BestResult::score();
|
||||||
|
cout << headerLine(oss.str());
|
||||||
}
|
}
|
||||||
cout << string(MAXL, '*') << endl << Colors::RESET();
|
cout << string(MAXL, '*') << endl << Colors::RESET();
|
||||||
|
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
#include "Colors.h"
|
#include "Colors.h"
|
||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
const int MAXL = 121;
|
const int MAXL = 128;
|
||||||
namespace platform {
|
namespace platform {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
class Report {
|
class Report {
|
||||||
|
57
src/Platform/list.cc
Normal file
57
src/Platform/list.cc
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <locale>
|
||||||
|
#include "Paths.h"
|
||||||
|
#include "Colors.h"
|
||||||
|
#include "Datasets.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
const int BALANCE_LENGTH = 75;
|
||||||
|
|
||||||
|
struct separated : numpunct<char> {
|
||||||
|
char do_decimal_point() const { return ','; }
|
||||||
|
char do_thousands_sep() const { return '.'; }
|
||||||
|
string do_grouping() const { return "\03"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
void outputBalance(const string& balance)
|
||||||
|
{
|
||||||
|
auto temp = string(balance);
|
||||||
|
while (temp.size() > BALANCE_LENGTH - 1) {
|
||||||
|
auto part = temp.substr(0, BALANCE_LENGTH);
|
||||||
|
cout << part << endl;
|
||||||
|
cout << setw(48) << " ";
|
||||||
|
temp = temp.substr(BALANCE_LENGTH);
|
||||||
|
}
|
||||||
|
cout << temp << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
auto data = platform::Datasets(platform::Paths().datasets(), false);
|
||||||
|
locale mylocale(cout.getloc(), new separated);
|
||||||
|
locale::global(mylocale);
|
||||||
|
cout.imbue(mylocale);
|
||||||
|
cout << Colors::GREEN() << "Dataset Sampl. Feat. Cls. Balance" << endl;
|
||||||
|
string balanceBars = string(BALANCE_LENGTH, '=');
|
||||||
|
cout << "============================== ====== ===== === " << balanceBars << endl;
|
||||||
|
bool odd = true;
|
||||||
|
for (const auto& dataset : data.getNames()) {
|
||||||
|
auto color = odd ? Colors::CYAN() : Colors::BLUE();
|
||||||
|
cout << color << setw(30) << left << dataset << " ";
|
||||||
|
data.loadDataset(dataset);
|
||||||
|
auto nSamples = data.getNSamples(dataset);
|
||||||
|
cout << setw(6) << right << nSamples << " ";
|
||||||
|
cout << setw(5) << right << data.getFeatures(dataset).size() << " ";
|
||||||
|
cout << setw(3) << right << data.getNClasses(dataset) << " ";
|
||||||
|
stringstream oss;
|
||||||
|
string sep = "";
|
||||||
|
for (auto number : data.getClassesCounts(dataset)) {
|
||||||
|
oss << sep << setprecision(2) << fixed << (float)number / nSamples * 100.0 << "% (" << number << ")";
|
||||||
|
sep = " / ";
|
||||||
|
}
|
||||||
|
outputBalance(oss.str());
|
||||||
|
odd = !odd;
|
||||||
|
}
|
||||||
|
cout << Colors::RESET() << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
@@ -103,7 +103,7 @@ int main(int argc, char** argv)
|
|||||||
*/
|
*/
|
||||||
auto env = platform::DotEnv();
|
auto env = platform::DotEnv();
|
||||||
auto experiment = platform::Experiment();
|
auto experiment = platform::Experiment();
|
||||||
experiment.setTitle(title).setLanguage("cpp").setLanguageVersion("1.0.0");
|
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");
|
||||||
for (auto seed : seeds) {
|
for (auto seed : seeds) {
|
||||||
|
@@ -16,4 +16,6 @@ static platform::Registrar registrarA("AODE",
|
|||||||
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODE();});
|
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODE();});
|
||||||
static platform::Registrar registrarALD("AODELd",
|
static platform::Registrar registrarALD("AODELd",
|
||||||
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODELd();});
|
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODELd();});
|
||||||
|
static platform::Registrar registrarBA("BoostAODE",
|
||||||
|
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::BoostAODE();});
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user