Complete AODELd

This commit is contained in:
Ricardo Montañana Gómez 2023-08-06 11:31:44 +02:00
parent 9e94f4e140
commit 14ea51648a
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
10 changed files with 92 additions and 25 deletions

4
.vscode/launch.json vendored
View File

@ -25,14 +25,14 @@
"program": "${workspaceFolder}/build/src/Platform/main",
"args": [
"-m",
"TANNew",
"AODELd",
"-p",
"/Users/rmontanana/Code/discretizbench/datasets",
"--stratified",
"-d",
"iris"
],
"cwd": "${workspaceFolder}/build/src/Platform",
"cwd": "/Users/rmontanana/Code/discretizbench",
},
{
"name": "Build & debug active file",

View File

@ -17,6 +17,11 @@ dependency: ## Create a dependency graph diagram of the project (build/dependenc
build: ## Build the main and BayesNetSample
cmake --build build -t main -t BayesNetSample -j 32
clean: ## Clean the debug info
@echo ">>> Cleaning Debug BayesNet ...";
find . -name "*.gcda" -print0 | xargs -0 rm
@echo ">>> Done";
debug: ## Build a debug version of the project
@echo ">>> Building Debug BayesNet ...";
@if [ -d ./build ]; then rm -rf ./build; fi

34
src/BayesNet/AODELd.cc Normal file
View File

@ -0,0 +1,34 @@
#include "AODELd.h"
namespace bayesnet {
using namespace std;
AODELd::AODELd() : Ensemble(), Proposal(Ensemble::Xv, Ensemble::yv, features, className) {}
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, vector<string>& features_, string className_, map<string, vector<int>>& states_)
{
features = features_;
className = className_;
states = states_;
train();
for (const auto& model : models) {
model->fit(X_, y_, features_, className_, states_);
}
n_models = models.size();
fitted = true;
return *this;
}
void AODELd::train()
{
models.clear();
for (int i = 0; i < features.size(); ++i) {
models.push_back(std::make_unique<SPODELd>(i));
}
}
Tensor AODELd::predict(Tensor& X)
{
return Ensemble::predict(X);
}
vector<string> AODELd::graph(const string& name)
{
return Ensemble::graph(name);
}
}

20
src/BayesNet/AODELd.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef AODELD_H
#define AODELD_H
#include "Ensemble.h"
#include "Proposal.h"
#include "SPODELd.h"
namespace bayesnet {
using namespace std;
class AODELd : public Ensemble, public Proposal {
public:
AODELd();
virtual ~AODELd() = default;
AODELd& fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) override;
vector<string> graph(const string& name = "AODE") override;
Tensor predict(Tensor& X) override;
void train() override;
static inline string version() { return "0.0.1"; };
};
}
#endif // !AODELD_H

View File

@ -1,5 +1,5 @@
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
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 Mst.cc Proposal.cc)
KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc Mst.cc Proposal.cc)
target_link_libraries(BayesNet mdlp ArffFiles "${TORCH_LIBRARIES}")

View File

@ -3,24 +3,24 @@
namespace bayesnet {
using namespace torch;
Ensemble::Ensemble() : m(0), n(0), n_models(0), metrics(Metrics()), fitted(false) {}
Ensemble::Ensemble() : n_models(0), metrics(Metrics()), fitted(false) {}
Ensemble& Ensemble::build(vector<string>& features, string className, map<string, vector<int>>& states)
{
dataset = cat({ X, y.view({y.size(0), 1}) }, 1);
Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
samples = torch::cat({ X, ytmp }, 0);
this->features = features;
this->className = className;
this->states = states;
auto n_classes = states[className].size();
metrics = Metrics(dataset, features, className, n_classes);
metrics = Metrics(samples, features, className, n_classes);
// Build models
train();
// Train models
n_models = models.size();
auto Xt = torch::transpose(X, 0, 1);
for (auto i = 0; i < n_models; ++i) {
if (Xv == vector<vector<int>>()) {
if (Xv.empty()) {
// fit with tensors
models[i]->fit(Xt, y, features, className, states);
models[i]->fit(X, y, features, className, states);
} else {
// fit with vectors
models[i]->fit(Xv, yv, features, className, states);
@ -29,9 +29,16 @@ namespace bayesnet {
fitted = true;
return *this;
}
void Ensemble::generateTensorXFromVector()
{
X = torch::zeros({ static_cast<int>(Xv.size()), static_cast<int>(Xv[0].size()) }, kInt32);
for (int i = 0; i < Xv.size(); ++i) {
X.index_put_({ i, "..." }, torch::tensor(Xv[i], kInt32));
}
}
Ensemble& Ensemble::fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states)
{
this->X = torch::transpose(X, 0, 1);
this->X = X;
this->y = y;
Xv = vector<vector<int>>();
yv = vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + y.size(0));
@ -39,11 +46,8 @@ namespace bayesnet {
}
Ensemble& Ensemble::fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states)
{
this->X = torch::zeros({ static_cast<int>(X[0].size()), static_cast<int>(X.size()) }, kInt32);
Xv = X;
for (int i = 0; i < X.size(); ++i) {
this->X.index_put_({ "...", i }, torch::tensor(X[i], kInt32));
}
generateTensorXFromVector();
this->y = torch::tensor(y, kInt32);
yv = y;
return build(features, className, states);
@ -53,10 +57,11 @@ namespace bayesnet {
auto y_pred_ = y_pred.accessor<int, 2>();
vector<int> y_pred_final;
for (int i = 0; i < y_pred.size(0); ++i) {
vector<float> votes(states[className].size(), 0);
vector<float> votes(y_pred.size(1), 0);
for (int j = 0; j < y_pred.size(1); ++j) {
votes[y_pred_[i][j]] += 1;
}
// argsort in descending order
auto indices = argsort(votes);
y_pred_final.push_back(indices[0]);
}
@ -70,13 +75,12 @@ namespace bayesnet {
Tensor y_pred = torch::zeros({ X.size(1), n_models }, kInt32);
//Create a threadpool
auto threads{ vector<thread>() };
auto lock = mutex();
mutex mtx;
for (auto i = 0; i < n_models; ++i) {
threads.push_back(thread([&, i]() {
auto ypredict = models[i]->predict(X);
lock.lock();
lock_guard<mutex> lock(mtx);
y_pred.index_put_({ "...", i }, ypredict);
lock.unlock();
}));
}
for (auto& thread : threads) {

View File

@ -10,23 +10,23 @@ using namespace torch;
namespace bayesnet {
class Ensemble : public BaseClassifier {
private:
bool fitted;
long n_models;
Ensemble& build(vector<string>& features, string className, map<string, vector<int>>& states);
protected:
unsigned n_models;
bool fitted;
vector<unique_ptr<Classifier>> models;
int m, n; // m: number of samples, n: number of features
Tensor X;
vector<vector<int>> Xv;
Tensor y;
vector<int> yv;
Tensor dataset;
Tensor samples;
Metrics metrics;
vector<string> features;
string className;
map<string, vector<int>> states;
void virtual train() = 0;
vector<int> voting(Tensor& y_pred);
void generateTensorXFromVector();
public:
Ensemble();
virtual ~Ensemble() = default;

View File

@ -3,6 +3,7 @@
namespace bayesnet {
using namespace std;
using namespace torch;
// Return the indices in descending order
vector<int> argsort(vector<float>& nums)
{
int n = nums.size();

View File

@ -9,6 +9,7 @@
#include "TANLd.h"
#include "KDBLd.h"
#include "SPODELd.h"
#include "AODELd.h"
namespace platform {
class Models {
private:

View File

@ -2,16 +2,18 @@
#define MODEL_REGISTER_H
static platform::Registrar registrarT("TAN",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::TAN();});
static platform::Registrar registrarTN("TANLd",
static platform::Registrar registrarTLD("TANLd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::TANLd();});
static platform::Registrar registrarS("SPODE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODE(2);});
static platform::Registrar registrarSN("SPODELd",
static platform::Registrar registrarSLD("SPODELd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODELd(2);});
static platform::Registrar registrarK("KDB",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDB(2);});
static platform::Registrar registrarKN("KDBLd",
static platform::Registrar registrarKLD("KDBLd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDBLd(2);});
static platform::Registrar registrarA("AODE",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODE();});
static platform::Registrar registrarALD("AODELd",
[](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODELd();});
#endif