diff --git a/src/BayesNet/BaseClassifier.h b/src/BayesNet/BaseClassifier.h index a4be506..ca1a5b6 100644 --- a/src/BayesNet/BaseClassifier.h +++ b/src/BayesNet/BaseClassifier.h @@ -26,7 +26,7 @@ namespace bayesnet { int virtual getNumberOfStates() const = 0; std::vector virtual show() const = 0; std::vector virtual graph(const std::string& title = "") const = 0; - const std::string inline getVersion() const { return "0.2.0"; }; + virtual std::string getVersion() = 0; std::vector virtual topological_order() = 0; void virtual dump_cpt()const = 0; virtual void setHyperparameters(nlohmann::json& hyperparameters) = 0; diff --git a/src/BayesNet/CMakeLists.txt b/src/BayesNet/CMakeLists.txt index cc0f5a5..c14e625 100644 --- a/src/BayesNet/CMakeLists.txt +++ b/src/BayesNet/CMakeLists.txt @@ -3,6 +3,9 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/Files) include_directories(${BayesNet_SOURCE_DIR}/lib/json/include) include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet) include_directories(${BayesNet_SOURCE_DIR}/src/Platform) +include_directories(${BayesNet_SOURCE_DIR}/src/PyClassifiers) +include_directories(${Python3_INCLUDE_DIRS}) + 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 AODELd.cc BoostAODE.cc Mst.cc Proposal.cc CFS.cc FCBF.cc IWSS.cc FeatureSelect.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc) diff --git a/src/BayesNet/Classifier.h b/src/BayesNet/Classifier.h index 418589e..d4a9c42 100644 --- a/src/BayesNet/Classifier.h +++ b/src/BayesNet/Classifier.h @@ -37,6 +37,7 @@ namespace bayesnet { int getNumberOfStates() const override; torch::Tensor predict(torch::Tensor& X) override; status_t getStatus() const override { return status; } + std::string getVersion() override { return "0.2.0"; }; std::vector predict(std::vector>& X) override; float score(torch::Tensor& X, torch::Tensor& y) override; float score(std::vector>& X, std::vector& y) override; diff --git a/src/Platform/CMakeLists.txt b/src/Platform/CMakeLists.txt index 99f0afc..24311cf 100644 --- a/src/Platform/CMakeLists.txt +++ b/src/Platform/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp) include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include) include_directories(${BayesNet_SOURCE_DIR}/lib/json/include) include_directories(${BayesNet_SOURCE_DIR}/lib/libxlsxwriter/include) +include_directories(${Python3_INCLUDE_DIRS}) add_executable(b_main b_main.cc Folding.cc Experiment.cc Datasets.cc Dataset.cc Models.cc ReportConsole.cc ReportBase.cc) add_executable(b_manage b_manage.cc Results.cc ManageResults.cc CommandParser.cc Result.cc ReportConsole.cc ReportExcel.cc ReportBase.cc Datasets.cc Dataset.cc ExcelFile.cc) diff --git a/src/Platform/Models.h b/src/Platform/Models.h index 8e250d5..a985c32 100644 --- a/src/Platform/Models.h +++ b/src/Platform/Models.h @@ -12,6 +12,9 @@ #include "AODELd.h" #include "BoostAODE.h" #include "STree.h" +#include "ODTE.h" +#include "SVC.h" +#include "RandomForest.h" namespace platform { class Models { private: diff --git a/src/Platform/modelRegister.h b/src/Platform/modelRegister.h index f8a0e67..7f58401 100644 --- a/src/Platform/modelRegister.h +++ b/src/Platform/modelRegister.h @@ -18,6 +18,12 @@ static platform::Registrar registrarALD("AODELd", [](void) -> bayesnet::BaseClassifier* { return new bayesnet::AODELd();}); static platform::Registrar registrarBA("BoostAODE", [](void) -> bayesnet::BaseClassifier* { return new bayesnet::BoostAODE();}); -static pywrap::Registrar registrarSt("STree", +static platform::Registrar registrarSt("STree", [](void) -> bayesnet::BaseClassifier* { return new pywrap::STree();}); +static platform::Registrar registrarOdte("Odte", + [](void) -> bayesnet::BaseClassifier* { return new pywrap::ODTE();}); +static platform::Registrar registrarSvc("SVC", + [](void) -> bayesnet::BaseClassifier* { return new pywrap::SVC();}); +static platform::Registrar registrarRaF("RandomForest", + [](void) -> bayesnet::BaseClassifier* { return new pywrap::RandomForest();}); #endif \ No newline at end of file diff --git a/src/PyClassifiers/Classifier.h b/src/PyClassifiers/Classifier.h deleted file mode 100644 index 083496f..0000000 --- a/src/PyClassifiers/Classifier.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef CLASSIFIER_H -#define CLASSIFIER_H -#include -#include "BaseClassifier.h" -#include -#include -#include -#include - -namespace pywrap { - class Classifier : bayesnet::BaseClassifier { - public: - Classifier() = default; - virtual ~Classifier() = default; - virtual Classifier& fit(torch::Tensor& X, torch::Tensor& y) = 0; - virtual std::string version() = 0; - virtual std::string sklearnVersion() = 0; - protected: - virtual void checkHyperparameters(const std::vector& validKeys, const nlohmann::json& hyperparameters) = 0; - }; -} /* namespace pywrap */ -#endif /* CLASSIFIER_H */ \ No newline at end of file diff --git a/src/PyClassifiers/ODTE.cc b/src/PyClassifiers/ODTE.cc index f168f43..1083699 100644 --- a/src/PyClassifiers/ODTE.cc +++ b/src/PyClassifiers/ODTE.cc @@ -5,7 +5,7 @@ namespace pywrap { { return callMethodString("graph"); } - void ODTE::setHyperparameters(const nlohmann::json& hyperparameters) + void ODTE::setHyperparameters(nlohmann::json& hyperparameters) { // Check if hyperparameters are valid const std::vector validKeys = { "n_jobs", "n_estimators", "random_state" }; diff --git a/src/PyClassifiers/ODTE.h b/src/PyClassifiers/ODTE.h index 1c90951..a3bbd05 100644 --- a/src/PyClassifiers/ODTE.h +++ b/src/PyClassifiers/ODTE.h @@ -9,7 +9,7 @@ namespace pywrap { ODTE() : PyClassifier("odte", "Odte") {}; ~ODTE() = default; std::string graph(); - void setHyperparameters(const nlohmann::json& hyperparameters) override; + void setHyperparameters(nlohmann::json& hyperparameters) override; }; } /* namespace pywrap */ #endif /* ODTE_H */ \ No newline at end of file diff --git a/src/PyClassifiers/PyClassifier.cc b/src/PyClassifiers/PyClassifier.cc index 9da85ab..779c64f 100644 --- a/src/PyClassifiers/PyClassifier.cc +++ b/src/PyClassifiers/PyClassifier.cc @@ -74,15 +74,15 @@ namespace pywrap { Py_XDECREF(incoming); return resultTensor; } - double PyClassifier::score(torch::Tensor& X, torch::Tensor& y) + float PyClassifier::score(torch::Tensor& X, torch::Tensor& y) { auto [Xn, yn] = tensors2numpy(X, y); CPyObject Xp = bp::incref(bp::object(Xn).ptr()); CPyObject yp = bp::incref(bp::object(yn).ptr()); - auto result = pyWrap->score(id, Xp, yp); + float result = pyWrap->score(id, Xp, yp); return result; } - void PyClassifier::setHyperparameters(const nlohmann::json& hyperparameters) + void PyClassifier::setHyperparameters(nlohmann::json& hyperparameters) { // Check if hyperparameters are valid, default is no hyperparameters const std::vector validKeys = { }; diff --git a/src/PyClassifiers/PyClassifier.h b/src/PyClassifiers/PyClassifier.h index b243f68..32f2b3d 100644 --- a/src/PyClassifiers/PyClassifier.h +++ b/src/PyClassifiers/PyClassifier.h @@ -13,21 +13,37 @@ #include "TypeId.h" namespace pywrap { - class PyClassifier : public Classifier { + class PyClassifier : public bayesnet::BaseClassifier { public: PyClassifier(const std::string& module, const std::string& className); virtual ~PyClassifier(); + PyClassifier& fit(std::vector>& X, std::vector& y, const std::vector& features, const std::string& className, std::map>& states) override { return *this; }; + // X is nxm tensor, y is nx1 tensor PyClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector& features, const std::string& className, std::map>& states) override; - PyClassifier& fit(torch::Tensor& X, torch::Tensor& y) override; + PyClassifier& fit(torch::Tensor& X, torch::Tensor& y); + PyClassifier& fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& states) override { return *this; }; + PyClassifier& fit(torch::Tensor& dataset, const std::vector& features, const std::string& className, std::map>& states, const torch::Tensor& weights) { return *this; }; torch::Tensor predict(torch::Tensor& X) override; - double score(torch::Tensor& X, torch::Tensor& y) override; - std::string version() override; - std::string sklearnVersion() override; + std::vector predict(std::vector>& X) override { return std::vector(); }; + float score(std::vector>& X, std::vector& y) override { return 0.0; }; + float score(torch::Tensor& X, torch::Tensor& y) override; + void setHyperparameters(nlohmann::json& hyperparameters) override; + std::string version(); + std::string sklearnVersion(); std::string callMethodString(const std::string& method); - void setHyperparameters(const nlohmann::json& hyperparameters) override; + std::string getVersion() override { return this->version(); }; + int getNumberOfNodes()const override { return 0; }; + int getNumberOfEdges()const override { return 0; }; + int getNumberOfStates() const override { return 0; }; + std::vector show() const override { return std::vector(); } + std::vector graph(const std::string& title = "") const override { return std::vector(); } + bayesnet::status_t getStatus() const override { return bayesnet::NORMAL; }; + std::vector topological_order() override { return std::vector(); } + void dump_cpt() const override {}; protected: - void checkHyperparameters(const std::vector& validKeys, const nlohmann::json& hyperparameters) override; + void checkHyperparameters(const std::vector& validKeys, const nlohmann::json& hyperparameters); nlohmann::json hyperparameters; + void trainModel(const torch::Tensor& weights) override {}; private: PyWrap* pyWrap; std::string module; diff --git a/src/PyClassifiers/STree.cc b/src/PyClassifiers/STree.cc index 01b3f19..1405ed7 100644 --- a/src/PyClassifiers/STree.cc +++ b/src/PyClassifiers/STree.cc @@ -5,7 +5,7 @@ namespace pywrap { { return callMethodString("graph"); } - void STree::setHyperparameters(const nlohmann::json& hyperparameters) + void STree::setHyperparameters(nlohmann::json& hyperparameters) { // Check if hyperparameters are valid const std::vector validKeys = { "C", "n_jobs", "kernel", "max_iter", "max_depth", "random_state", "multiclass_strategy" }; diff --git a/src/PyClassifiers/STree.h b/src/PyClassifiers/STree.h index a803e71..4691fc0 100644 --- a/src/PyClassifiers/STree.h +++ b/src/PyClassifiers/STree.h @@ -9,7 +9,7 @@ namespace pywrap { STree() : PyClassifier("stree", "Stree") {}; ~STree() = default; std::string graph(); - void setHyperparameters(const nlohmann::json& hyperparameters) override; + void setHyperparameters(nlohmann::json& hyperparameters) override; }; } /* namespace pywrap */ #endif /* STREE_H */ \ No newline at end of file diff --git a/src/PyClassifiers/SVC.cc b/src/PyClassifiers/SVC.cc index 3245903..5734700 100644 --- a/src/PyClassifiers/SVC.cc +++ b/src/PyClassifiers/SVC.cc @@ -5,7 +5,7 @@ namespace pywrap { { return sklearnVersion(); } - void SVC::setHyperparameters(const nlohmann::json& hyperparameters) + void SVC::setHyperparameters(nlohmann::json& hyperparameters) { // Check if hyperparameters are valid const std::vector validKeys = { "C", "gamma", "kernel", "random_state" }; diff --git a/src/PyClassifiers/SVC.h b/src/PyClassifiers/SVC.h index 1c755d9..1d6ac42 100644 --- a/src/PyClassifiers/SVC.h +++ b/src/PyClassifiers/SVC.h @@ -8,7 +8,7 @@ namespace pywrap { SVC() : PyClassifier("sklearn.svm", "SVC") {}; ~SVC() = default; std::string version(); - void setHyperparameters(const nlohmann::json& hyperparameters) override; + void setHyperparameters(nlohmann::json& hyperparameters) override; }; } /* namespace pywrap */