Fix Xspode
This commit is contained in:
@@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
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) {}
|
||||||
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
|
|
||||||
Classifier& Classifier::build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const Smoothing_t smoothing)
|
Classifier& Classifier::build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const Smoothing_t smoothing)
|
||||||
{
|
{
|
||||||
this->features = features;
|
this->features = features;
|
||||||
@@ -22,11 +21,8 @@ namespace bayesnet {
|
|||||||
auto n_classes = states.at(className).size();
|
auto n_classes = states.at(className).size();
|
||||||
metrics = Metrics(dataset, features, className, n_classes);
|
metrics = Metrics(dataset, features, className, n_classes);
|
||||||
model.initialize();
|
model.initialize();
|
||||||
std::cout << "Ahora buildmodel"<< std::endl;
|
|
||||||
buildModel(weights);
|
buildModel(weights);
|
||||||
std::cout << "Ahora trainmodel"<< std::endl;
|
|
||||||
trainModel(weights, smoothing);
|
trainModel(weights, smoothing);
|
||||||
std::cout << "Después de trainmodel"<< std::endl;
|
|
||||||
fitted = true;
|
fitted = true;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@@ -50,6 +50,7 @@ namespace bayesnet {
|
|||||||
virtual void buildModel(const torch::Tensor& weights) = 0;
|
virtual void buildModel(const torch::Tensor& weights) = 0;
|
||||||
void trainModel(const torch::Tensor& weights, const Smoothing_t smoothing) override;
|
void trainModel(const torch::Tensor& weights, const Smoothing_t smoothing) override;
|
||||||
void buildDataset(torch::Tensor& y);
|
void buildDataset(torch::Tensor& y);
|
||||||
|
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
|
||||||
private:
|
private:
|
||||||
Classifier& build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const Smoothing_t smoothing);
|
Classifier& build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const Smoothing_t smoothing);
|
||||||
};
|
};
|
||||||
|
@@ -110,7 +110,6 @@ namespace bayesnet {
|
|||||||
instance[nFeatures_] = dataset[-1][i].item<int>();
|
instance[nFeatures_] = dataset[-1][i].item<int>();
|
||||||
addSample(instance, weights[i].item<double>());
|
addSample(instance, weights[i].item<double>());
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (smoothing) {
|
switch (smoothing) {
|
||||||
case bayesnet::Smoothing_t::ORIGINAL:
|
case bayesnet::Smoothing_t::ORIGINAL:
|
||||||
alpha_ = 1.0 / m;
|
alpha_ = 1.0 / m;
|
||||||
@@ -414,9 +413,6 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
float XSpode::score(std::vector<std::vector<int>>& X, std::vector<int>& y)
|
float XSpode::score(std::vector<std::vector<int>>& X, std::vector<int>& y)
|
||||||
{
|
{
|
||||||
if (!fitted) {
|
|
||||||
throw std::logic_error(CLASSIFIER_NOT_FITTED);
|
|
||||||
}
|
|
||||||
auto y_pred = this->predict(X);
|
auto y_pred = this->predict(X);
|
||||||
int correct = 0;
|
int correct = 0;
|
||||||
for (int i = 0; i < y_pred.size(); ++i) {
|
for (int i = 0; i < y_pred.size(); ++i) {
|
||||||
|
@@ -51,8 +51,6 @@ namespace bayesnet {
|
|||||||
int statesClass_;
|
int statesClass_;
|
||||||
std::vector<int> states_; // [states_feat0, ..., states_feat(N-1)] (class not included in this array)
|
std::vector<int> states_; // [states_feat0, ..., states_feat(N-1)] (class not included in this array)
|
||||||
|
|
||||||
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
|
|
||||||
|
|
||||||
// Class counts
|
// Class counts
|
||||||
std::vector<double> classCounts_; // [c], accumulative
|
std::vector<double> classCounts_; // [c], accumulative
|
||||||
std::vector<double> classPriors_; // [c], after normalization
|
std::vector<double> classPriors_; // [c], after normalization
|
||||||
|
@@ -58,10 +58,12 @@ TEST_CASE("Test Bayesian Classifiers score & version", "[Models]")
|
|||||||
auto raw = RawDatasets(file_name, discretize);
|
auto raw = RawDatasets(file_name, discretize);
|
||||||
if (name == "XSPODE") {
|
if (name == "XSPODE") {
|
||||||
std::cout << "Fitting XSPODE" << std::endl;
|
std::cout << "Fitting XSPODE" << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "Fitting something else [" << name << "]" << std::endl;
|
||||||
}
|
}
|
||||||
clf->fit(raw.Xt, raw.yt, raw.features, raw.className, raw.states, raw.smoothing);
|
clf->fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
|
||||||
auto score = clf->score(raw.Xt, raw.yt);
|
auto score = clf->score(raw.Xt, raw.yt);
|
||||||
std::cout << "Classifier: " << name << " File: " << file_name << " Score: " << score << " expected = " << scores[{file_name, name}] << std::endl;
|
std::cout << "Classifier: " << name << " File: " << file_name << " Score: " << score << " expected = " << scores[{file_name, name}] << std::endl;
|
||||||
INFO("Classifier: " << name << " File: " << file_name);
|
INFO("Classifier: " << name << " File: " << file_name);
|
||||||
REQUIRE(score == Catch::Approx(scores[{file_name, name}]).epsilon(raw.epsilon));
|
REQUIRE(score == Catch::Approx(scores[{file_name, name}]).epsilon(raw.epsilon));
|
||||||
REQUIRE(clf->getStatus() == bayesnet::NORMAL);
|
REQUIRE(clf->getStatus() == bayesnet::NORMAL);
|
||||||
|
Reference in New Issue
Block a user