diff --git a/src/BayesNet/CMakeLists.txt b/src/BayesNet/CMakeLists.txt index 86d9660..5ed617e 100644 --- a/src/BayesNet/CMakeLists.txt +++ b/src/BayesNet/CMakeLists.txt @@ -1,4 +1,5 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp) include_directories(${BayesNet_SOURCE_DIR}/lib/Files) -add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANNew.cc KDBNew.cc Mst.cc Proposal.cc) +add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc + KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANNew.cc KDBNew.cc SPODENew.cc Mst.cc Proposal.cc) target_link_libraries(BayesNet mdlp ArffFiles "${TORCH_LIBRARIES}") \ No newline at end of file diff --git a/src/BayesNet/KDBNew.h b/src/BayesNet/KDBNew.h index 63ae9e9..6b2e0b1 100644 --- a/src/BayesNet/KDBNew.h +++ b/src/BayesNet/KDBNew.h @@ -8,7 +8,7 @@ namespace bayesnet { class KDBNew : public KDB, public Proposal { private: public: - KDBNew(int k); + explicit KDBNew(int k); virtual ~KDBNew() = default; KDBNew& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) override; vector graph(const string& name = "KDB") override; diff --git a/src/BayesNet/SPODENew.cc b/src/BayesNet/SPODENew.cc new file mode 100644 index 0000000..597b077 --- /dev/null +++ b/src/BayesNet/SPODENew.cc @@ -0,0 +1,35 @@ +#include "SPODENew.h" + +namespace bayesnet { + using namespace std; + SPODENew::SPODENew(int root) : SPODE(root), Proposal(SPODE::Xv, SPODE::yv, features, className) {} + SPODENew& SPODENew::fit(torch::Tensor& X_, torch::Tensor& y_, vector& features_, string className_, map>& states_) + { + // This first part should go in a Classifier method called fit_local_discretization o fit_float... + features = features_; + className = className_; + Xf = X_; + y = y_; + // Fills vectors Xv & yv with the data from tensors X_ (discretized) & y + fit_local_discretization(states, y); + generateTensorXFromVector(); + // We have discretized the input data + // 1st we need to fit the model to build the normal SPODE structure, SPODE::fit initializes the base Bayesian network + SPODE::fit(SPODE::Xv, SPODE::yv, features, className, states); + localDiscretizationProposal(states, model); + generateTensorXFromVector(); + Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1); + samples = torch::cat({ X, ytmp }, 0); + model.fit(SPODE::Xv, SPODE::yv, features, className); + return *this; + } + Tensor SPODENew::predict(Tensor& X) + { + auto Xt = prepareX(X); + return SPODE::predict(Xt); + } + vector SPODENew::graph(const string& name) + { + return SPODE::graph(name); + } +} \ No newline at end of file diff --git a/src/BayesNet/SPODENew.h b/src/BayesNet/SPODENew.h new file mode 100644 index 0000000..6d6d008 --- /dev/null +++ b/src/BayesNet/SPODENew.h @@ -0,0 +1,19 @@ +#ifndef SPODENEW_H +#define SPODENEW_H +#include "SPODE.h" +#include "Proposal.h" + +namespace bayesnet { + using namespace std; + class SPODENew : public SPODE, public Proposal { + private: + public: + explicit SPODENew(int root); + virtual ~SPODENew() = default; + SPODENew& fit(torch::Tensor& X, torch::Tensor& y, vector& features, string className, map>& states) override; + vector graph(const string& name = "SPODE") override; + Tensor predict(Tensor& X) override; + static inline string version() { return "0.0.1"; }; + }; +} +#endif // !SPODENew_H \ No newline at end of file diff --git a/src/Platform/Models.h b/src/Platform/Models.h index 9616734..0c1648d 100644 --- a/src/Platform/Models.h +++ b/src/Platform/Models.h @@ -8,6 +8,7 @@ #include "SPODE.h" #include "TANNew.h" #include "KDBNew.h" +#include "SPODENew.h" namespace platform { class Models { private: diff --git a/src/Platform/Report.cc b/src/Platform/Report.cc index ecff1a6..90aad2b 100644 --- a/src/Platform/Report.cc +++ b/src/Platform/Report.cc @@ -48,9 +48,9 @@ namespace platform { cout << setw(6) << right << r["samples"].get() << " "; cout << setw(5) << right << r["features"].get() << " "; cout << setw(3) << right << r["classes"].get() << " "; - cout << setw(7) << right << r["nodes"].get() << " "; - cout << setw(7) << right << r["leaves"].get() << " "; - cout << setw(7) << right << r["depth"].get() << " "; + cout << setw(7) << setprecision(2) << fixed << r["nodes"].get() << " "; + cout << setw(7) << setprecision(2) << fixed << r["leaves"].get() << " "; + cout << setw(7) << setprecision(2) << fixed << r["depth"].get() << " "; cout << setw(8) << right << setprecision(6) << fixed << r["score_test"].get() << "±" << setw(6) << setprecision(4) << fixed << r["score_test_std"].get() << " "; cout << setw(10) << right << setprecision(6) << fixed << r["test_time"].get() << "±" << setw(6) << setprecision(4) << fixed << r["test_time_std"].get() << " "; cout << " " << r["hyperparameters"].get(); diff --git a/src/Platform/modelRegister.h b/src/Platform/modelRegister.h index b3f4bd0..ca0d517 100644 --- a/src/Platform/modelRegister.h +++ b/src/Platform/modelRegister.h @@ -6,6 +6,8 @@ static platform::Registrar registrarTN("TANNew", [](void) -> bayesnet::BaseClassifier* { return new bayesnet::TANNew();}); static platform::Registrar registrarS("SPODE", [](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODE(2);}); +static platform::Registrar registrarSN("SPODENew", + [](void) -> bayesnet::BaseClassifier* { return new bayesnet::SPODENew(2);}); static platform::Registrar registrarK("KDB", [](void) -> bayesnet::BaseClassifier* { return new bayesnet::KDB(2);}); static platform::Registrar registrarKN("KDBNew",