Begin Ensemble
This commit is contained in:
parent
e8b8fa29c8
commit
6a8aad5911
@ -80,16 +80,22 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
Tensor BaseClassifier::predict(Tensor& X)
|
Tensor BaseClassifier::predict(Tensor& X)
|
||||||
{
|
{
|
||||||
auto m_ = X.size(0);
|
auto n_models = models.size();
|
||||||
auto n_ = X.size(1);
|
Tensor y_pred = torch::zeros({ X.size(0), n_models }, torch::kInt64);
|
||||||
vector<vector<int>> Xd(n_, vector<int>(m_, 0));
|
for (auto i = 0; i < n_models; ++i) {
|
||||||
for (auto i = 0; i < n_; i++) {
|
y_pred.index_put_({ "...", i }, models[i].predict(X));
|
||||||
auto temp = X.index({ "...", i });
|
|
||||||
Xd[i] = vector<int>(temp.data_ptr<int>(), temp.data_ptr<int>() + m_);
|
|
||||||
}
|
}
|
||||||
auto yp = model.predict(Xd);
|
auto y_pred_ = y_pred.accessor<int64_t, 2>();
|
||||||
auto ypred = torch::tensor(yp, torch::kInt64);
|
vector<int> y_pred_final;
|
||||||
return ypred;
|
for (int i = 0; i < y_pred.size(0); ++i) {
|
||||||
|
vector<float> votes(states[className].size(), 0);
|
||||||
|
for (int j = 0; j < y_pred.size(1); ++j) {
|
||||||
|
votes[y_pred_[i][j]] += 1;
|
||||||
|
}
|
||||||
|
auto indices = argsort(votes);
|
||||||
|
y_pred_final.push_back(indices[0]);
|
||||||
|
}
|
||||||
|
return torch::tensor(y_pred_final, torch::kInt64);
|
||||||
}
|
}
|
||||||
float BaseClassifier::score(Tensor& X, Tensor& y)
|
float BaseClassifier::score(Tensor& X, Tensor& y)
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#ifndef CLASSIFIERS_H
|
#ifndef CLASSIFIERS_H
|
||||||
|
#define CLASSIFIERS_H
|
||||||
#include <torch/torch.h>
|
#include <torch/torch.h>
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "Metrics.hpp"
|
#include "Metrics.hpp"
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
add_library(BayesNet Network.cc Node.cc Metrics.cc BaseClassifier.cc KDB.cc TAN.cc SPODE.cc)
|
add_library(BayesNet Network.cc Node.cc Metrics.cc BaseClassifier.cc KDB.cc TAN.cc SPODE.cc Ensemble.cc)
|
||||||
target_link_libraries(BayesNet "${TORCH_LIBRARIES}")
|
target_link_libraries(BayesNet "${TORCH_LIBRARIES}")
|
61
src/Ensemble.cc
Normal file
61
src/Ensemble.cc
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include "Ensemble.h"
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
using namespace std;
|
||||||
|
using namespace torch;
|
||||||
|
|
||||||
|
Ensemble::Ensemble(BaseClassifier& model) : model(model), models(vector<BaseClassifier>()), m(0), n(0), metrics(Metrics()) {}
|
||||||
|
Ensemble& Ensemble::build(vector<string>& features, string className, map<string, vector<int>>& states)
|
||||||
|
{
|
||||||
|
|
||||||
|
dataset = torch::cat({ X, y.view({y.size(0), 1}) }, 1);
|
||||||
|
this->features = features;
|
||||||
|
this->className = className;
|
||||||
|
this->states = states;
|
||||||
|
auto n_classes = states[className].size();
|
||||||
|
metrics = Metrics(dataset, features, className, n_classes);
|
||||||
|
train();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Ensemble& Ensemble::fit(Tensor& X, Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states)
|
||||||
|
{
|
||||||
|
this->X = X;
|
||||||
|
this->y = y;
|
||||||
|
auto sizes = X.sizes();
|
||||||
|
m = sizes[0];
|
||||||
|
n = sizes[1];
|
||||||
|
return build(features, className, states);
|
||||||
|
}
|
||||||
|
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<int64_t>(X[0].size()), static_cast<int64_t>(X.size()) }, kInt64);
|
||||||
|
for (int i = 0; i < X.size(); ++i) {
|
||||||
|
this->X.index_put_({ "...", i }, torch::tensor(X[i], kInt64));
|
||||||
|
}
|
||||||
|
this->y = torch::tensor(y, kInt64);
|
||||||
|
return build(features, className, states);
|
||||||
|
}
|
||||||
|
Tensor Ensemble::predict(Tensor& X)
|
||||||
|
{
|
||||||
|
auto m_ = X.size(0);
|
||||||
|
auto n_ = X.size(1);
|
||||||
|
vector<vector<int>> Xd(n_, vector<int>(m_, 0));
|
||||||
|
for (auto i = 0; i < n_; i++) {
|
||||||
|
auto temp = X.index({ "...", i });
|
||||||
|
Xd[i] = vector<int>(temp.data_ptr<int>(), temp.data_ptr<int>() + m_);
|
||||||
|
}
|
||||||
|
auto yp = model.predict(Xd);
|
||||||
|
auto ypred = torch::tensor(yp, torch::kInt64);
|
||||||
|
return ypred;
|
||||||
|
}
|
||||||
|
float Ensemble::score(Tensor& X, Tensor& y)
|
||||||
|
{
|
||||||
|
Tensor y_pred = predict(X);
|
||||||
|
return (y_pred == y).sum().item<float>() / y.size(0);
|
||||||
|
}
|
||||||
|
vector<string> Ensemble::show()
|
||||||
|
{
|
||||||
|
return model.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
src/Ensemble.h
Normal file
34
src/Ensemble.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#ifndef ENSEMBLE_H
|
||||||
|
#define ENSEMBLE_H
|
||||||
|
#include <torch/torch.h>
|
||||||
|
#include "BaseClassifier.h"
|
||||||
|
#include "Metrics.hpp"
|
||||||
|
using namespace std;
|
||||||
|
using namespace torch;
|
||||||
|
|
||||||
|
namespace bayesnet {
|
||||||
|
class Ensemble {
|
||||||
|
private:
|
||||||
|
Ensemble& build(vector<string>& features, string className, map<string, vector<int>>& states);
|
||||||
|
protected:
|
||||||
|
BaseClassifier& model;
|
||||||
|
vector<BaseClassifier> models;
|
||||||
|
int m, n; // m: number of samples, n: number of features
|
||||||
|
Tensor X;
|
||||||
|
Tensor y;
|
||||||
|
Tensor dataset;
|
||||||
|
Metrics metrics;
|
||||||
|
vector<string> features;
|
||||||
|
string className;
|
||||||
|
map<string, vector<int>> states;
|
||||||
|
void virtual train() = 0;
|
||||||
|
public:
|
||||||
|
Ensemble(BaseClassifier& model);
|
||||||
|
Ensemble& fit(Tensor& X, Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states);
|
||||||
|
Ensemble& fit(vector<vector<int>>& X, vector<int>& y, vector<string>& features, string className, map<string, vector<int>>& states);
|
||||||
|
Tensor predict(Tensor& X);
|
||||||
|
float score(Tensor& X, Tensor& y);
|
||||||
|
vector<string> show();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user