Fix XA1DE integration
This commit is contained in:
@@ -10,12 +10,7 @@
|
||||
namespace platform {
|
||||
ExpClf::ExpClf() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false)
|
||||
{
|
||||
}
|
||||
void ExpClf::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||
{
|
||||
if (!hyperparameters.empty()) {
|
||||
throw std::invalid_argument("Invalid hyperparameters" + hyperparameters.dump());
|
||||
}
|
||||
validHyperparameters = {};
|
||||
}
|
||||
//
|
||||
// Predict
|
||||
|
@@ -6,7 +6,6 @@
|
||||
|
||||
#ifndef EXPCLF_H
|
||||
#define EXPCLF_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
@@ -18,16 +17,10 @@
|
||||
#include "Xaode.hpp"
|
||||
|
||||
namespace platform {
|
||||
|
||||
class ExpClf : public bayesnet::Boost {
|
||||
public:
|
||||
ExpClf();
|
||||
virtual ~ExpClf() = default;
|
||||
ExpClf& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) { return *this; };
|
||||
// X is nxm tensor, y is nx1 tensor
|
||||
ExpClf& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) { return *this; };
|
||||
ExpClf& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) { return *this; };
|
||||
ExpClf& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) { return *this; };
|
||||
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||
torch::Tensor predict(torch::Tensor& X) override;
|
||||
torch::Tensor predict_proba(torch::Tensor& X) override;
|
||||
@@ -46,8 +39,7 @@ namespace platform {
|
||||
bayesnet::status_t getStatus() const override { return status; }
|
||||
std::vector<std::string> getNotes() const override { return notes; }
|
||||
std::vector<std::string> graph(const std::string& title = "") const override { return {}; }
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
void set_active_parents(const std::vector<int> active_parents) { for (const auto& parent : active_parents) aode_.add_active_parent(parent); }
|
||||
void set_active_parents(const std::vector<int>& active_parents) { for (const auto& parent : active_parents) aode_.add_active_parent(parent); }
|
||||
void add_active_parent(int parent) { aode_.add_active_parent(parent); }
|
||||
void remove_last_parent() { aode_.remove_last_parent(); }
|
||||
protected:
|
||||
|
@@ -8,35 +8,13 @@
|
||||
#include "TensorUtils.hpp"
|
||||
|
||||
namespace platform {
|
||||
XA1DE& XA1DE::fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing)
|
||||
void XA1DE::trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing)
|
||||
{
|
||||
std::vector<std::vector<int>> instances = X;
|
||||
instances.push_back(y);
|
||||
int num_instances = instances[0].size();
|
||||
int num_attributes = instances.size();
|
||||
auto X = TensorUtils::to_matrix(dataset.slice(0, 0, dataset.size(0) - 1));
|
||||
auto y = TensorUtils::to_vector<int>(dataset.index({ -1, "..." }));
|
||||
int num_instances = X[0].size();
|
||||
weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
|
||||
normalize_weights(num_instances);
|
||||
aode_.fit(X, y, features, className, states, weights_, true);
|
||||
fitted = true;
|
||||
return *this;
|
||||
}
|
||||
//
|
||||
// Fit
|
||||
//
|
||||
XA1DE& XA1DE::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing)
|
||||
{
|
||||
auto X_ = TensorUtils::to_matrix(X);
|
||||
auto y_ = TensorUtils::to_vector<int>(y);
|
||||
return fit(X_, y_, features, className, states, smoothing);
|
||||
}
|
||||
XA1DE& XA1DE::fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing)
|
||||
{
|
||||
torch::Tensor y = dataset[dataset.size(0) - 1];
|
||||
torch::Tensor X = dataset.slice(0, 0, dataset.size(0) - 1);
|
||||
return fit(X, y, features, className, states, smoothing);
|
||||
}
|
||||
XA1DE& XA1DE::fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing)
|
||||
{
|
||||
weights_ = weights;
|
||||
return fit(dataset, features, className, states, smoothing);
|
||||
}
|
||||
}
|
@@ -6,12 +6,6 @@
|
||||
|
||||
#ifndef XA1DE_H
|
||||
#define XA1DE_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include "common/Timer.hpp"
|
||||
#include "Xaode.hpp"
|
||||
#include "ExpClf.h"
|
||||
|
||||
@@ -19,14 +13,11 @@ namespace platform {
|
||||
class XA1DE : public ExpClf {
|
||||
public:
|
||||
XA1DE() = default;
|
||||
virtual ~XA1DE() = default;
|
||||
XA1DE& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) override;
|
||||
XA1DE& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) override;
|
||||
XA1DE& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) override;
|
||||
XA1DE& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override;
|
||||
virtual ~XA1DE() override = default;
|
||||
std::string getVersion() override { return version; };
|
||||
protected:
|
||||
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override {};
|
||||
void buildModel(const torch::Tensor& weights) override {};
|
||||
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override;
|
||||
private:
|
||||
std::string version = "1.0.0";
|
||||
};
|
||||
|
@@ -36,7 +36,7 @@ namespace platform {
|
||||
// Algorithm based on the adaboost algorithm for classification
|
||||
// as explained in Ensemble methods (Zhi-Hua Zhou, 2012)
|
||||
double alpha_t = 0;
|
||||
torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
|
||||
weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
|
||||
bool finished = false;
|
||||
std::vector<int> featuresUsed;
|
||||
aode_.fit(X_train_, y_train_, features, className, states, weights_, false);
|
||||
|
@@ -18,7 +18,7 @@ namespace platform {
|
||||
class XBAODE : public ExpClf {
|
||||
public:
|
||||
XBAODE();
|
||||
virtual ~XBAODE() = default;
|
||||
virtual ~XBAODE() override = default;
|
||||
std::string getVersion() override { return version; };
|
||||
protected:
|
||||
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override;
|
||||
|
Reference in New Issue
Block a user