diff --git a/src/experimental_clfs/ExpClf.cpp b/src/experimental_clfs/ExpClf.cpp index 2477a76..d3ed03d 100644 --- a/src/experimental_clfs/ExpClf.cpp +++ b/src/experimental_clfs/ExpClf.cpp @@ -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 diff --git a/src/experimental_clfs/ExpClf.h b/src/experimental_clfs/ExpClf.h index 65d3172..fa6d65e 100644 --- a/src/experimental_clfs/ExpClf.h +++ b/src/experimental_clfs/ExpClf.h @@ -6,7 +6,6 @@ #ifndef EXPCLF_H #define EXPCLF_H -#include #include #include #include @@ -18,16 +17,10 @@ #include "Xaode.hpp" namespace platform { - class ExpClf : public bayesnet::Boost { public: ExpClf(); virtual ~ExpClf() = default; - ExpClf& fit(std::vector>& X, std::vector& y, const std::vector& features, const std::string& className, std::map>& 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& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) { return *this; }; - ExpClf& fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) { return *this; }; - ExpClf& fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& states, const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) { return *this; }; std::vector predict(std::vector>& 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 getNotes() const override { return notes; } std::vector graph(const std::string& title = "") const override { return {}; } - void setHyperparameters(const nlohmann::json& hyperparameters) override; - void set_active_parents(const std::vector active_parents) { for (const auto& parent : active_parents) aode_.add_active_parent(parent); } + void set_active_parents(const std::vector& 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: diff --git a/src/experimental_clfs/XA1DE.cpp b/src/experimental_clfs/XA1DE.cpp index c201017..b3f99cc 100644 --- a/src/experimental_clfs/XA1DE.cpp +++ b/src/experimental_clfs/XA1DE.cpp @@ -8,35 +8,13 @@ #include "TensorUtils.hpp" namespace platform { - XA1DE& XA1DE::fit(std::vector>& X, std::vector& y, const std::vector& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) + void XA1DE::trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) { - std::vector> 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(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& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) - { - auto X_ = TensorUtils::to_matrix(X); - auto y_ = TensorUtils::to_vector(y); - return fit(X_, y_, features, className, states, smoothing); - } - XA1DE& XA1DE::fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& 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& features, const std::string& className, std::map>& states, const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) - { - weights_ = weights; - return fit(dataset, features, className, states, smoothing); } } \ No newline at end of file diff --git a/src/experimental_clfs/XA1DE.h b/src/experimental_clfs/XA1DE.h index aa9bcfa..cede388 100644 --- a/src/experimental_clfs/XA1DE.h +++ b/src/experimental_clfs/XA1DE.h @@ -6,12 +6,6 @@ #ifndef XA1DE_H #define XA1DE_H -#include -#include -#include -#include -#include -#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>& X, std::vector& y, const std::vector& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) override; - XA1DE& fit(torch::Tensor& X, torch::Tensor& y, const std::vector& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) override; - XA1DE& fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& states, const bayesnet::Smoothing_t smoothing) override; - XA1DE& fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& 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"; }; diff --git a/src/experimental_clfs/XBAODE.cpp b/src/experimental_clfs/XBAODE.cpp index c0dd4e6..7f09359 100644 --- a/src/experimental_clfs/XBAODE.cpp +++ b/src/experimental_clfs/XBAODE.cpp @@ -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 featuresUsed; aode_.fit(X_train_, y_train_, features, className, states, weights_, false); diff --git a/src/experimental_clfs/XBAODE.h b/src/experimental_clfs/XBAODE.h index 9239c97..13951ac 100644 --- a/src/experimental_clfs/XBAODE.h +++ b/src/experimental_clfs/XBAODE.h @@ -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;