From 405887f83393b842500e01000f8b886847cc94cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Sat, 12 Aug 2023 11:49:18 +0200 Subject: [PATCH] Solved Ld poor results --- .vscode/launch.json | 4 ++-- src/BayesNet/AODELd.cc | 2 +- src/BayesNet/KDBLd.cc | 2 +- src/BayesNet/Proposal.cc | 6 +++++- src/BayesNet/Proposal.h | 2 +- src/BayesNet/SPODELd.cc | 4 ++-- src/BayesNet/TANLd.cc | 2 +- 7 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index e3c35bb..ba01ca6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,12 +25,12 @@ "program": "${workspaceFolder}/build/src/Platform/main", "args": [ "-m", - "TANLd", + "SPODELd", "-p", "/Users/rmontanana/Code/discretizbench/datasets", "--stratified", "-d", - "vehicle" + "iris" ], "cwd": "/Users/rmontanana/Code/discretizbench", }, diff --git a/src/BayesNet/AODELd.cc b/src/BayesNet/AODELd.cc index fbfaefd..9f36ed2 100644 --- a/src/BayesNet/AODELd.cc +++ b/src/BayesNet/AODELd.cc @@ -12,7 +12,7 @@ namespace bayesnet { Xf = X_; y = y_; // Fills vectors Xv & yv with the data from tensors X_ (discretized) & y - fit_local_discretization(states, y); + states = fit_local_discretization(y); // We have discretized the input data // 1st we need to fit the model to build the normal TAN structure, TAN::fit initializes the base Bayesian network Ensemble::fit(dataset, features, className, states); diff --git a/src/BayesNet/KDBLd.cc b/src/BayesNet/KDBLd.cc index 4b8b91c..724a053 100644 --- a/src/BayesNet/KDBLd.cc +++ b/src/BayesNet/KDBLd.cc @@ -11,7 +11,7 @@ namespace bayesnet { Xf = X_; y = y_; // Fills vectors Xv & yv with the data from tensors X_ (discretized) & y - fit_local_discretization(states, y); + states = fit_local_discretization(y); // We have discretized the input data // 1st we need to fit the model to build the normal KDB structure, KDB::fit initializes the base Bayesian network KDB::fit(dataset, features, className, states); diff --git a/src/BayesNet/Proposal.cc b/src/BayesNet/Proposal.cc index 78d5225..d53094d 100644 --- a/src/BayesNet/Proposal.cc +++ b/src/BayesNet/Proposal.cc @@ -67,10 +67,12 @@ namespace bayesnet { model.fit(pDataset, pFeatures, pClassName, states); } } - void Proposal::fit_local_discretization(map>& states, torch::Tensor& y) + map> Proposal::fit_local_discretization(torch::Tensor& y) { + // Discretize the continuous input data and build pDataset (Classifier::dataset) int m = Xf.size(1); int n = Xf.size(0); + map> states; pDataset = torch::zeros({ n + 1, m }, kInt32); auto yv = vector(y.data_ptr(), y.data_ptr() + y.size(0)); // discretize input data by feature(row) @@ -89,6 +91,8 @@ namespace bayesnet { auto yStates = vector(n_classes); iota(yStates.begin(), yStates.end(), 0); states[pClassName] = yStates; + pDataset.index_put_({ n, "..." }, y); + return states; } torch::Tensor Proposal::prepareX(torch::Tensor& X) { diff --git a/src/BayesNet/Proposal.h b/src/BayesNet/Proposal.h index 06d9dd6..10814c2 100644 --- a/src/BayesNet/Proposal.h +++ b/src/BayesNet/Proposal.h @@ -15,7 +15,7 @@ namespace bayesnet { protected: torch::Tensor prepareX(torch::Tensor& X); void localDiscretizationProposal(map>& states, Network& model); - void fit_local_discretization(map>& states, torch::Tensor& y); + map> fit_local_discretization(torch::Tensor& y); torch::Tensor Xf; // X continuous nxm tensor torch::Tensor y; // y discrete nx1 tensor map discretizers; diff --git a/src/BayesNet/SPODELd.cc b/src/BayesNet/SPODELd.cc index 0e58775..9683b7e 100644 --- a/src/BayesNet/SPODELd.cc +++ b/src/BayesNet/SPODELd.cc @@ -11,7 +11,7 @@ namespace bayesnet { Xf = X_; y = y_; // Fills vectors Xv & yv with the data from tensors X_ (discretized) & y - fit_local_discretization(states, y); + states = fit_local_discretization(y); // 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(dataset, features, className, states); @@ -27,7 +27,7 @@ namespace bayesnet { features = features_; className = className_; // Fills vectors Xv & yv with the data from tensors X_ (discretized) & y - fit_local_discretization(states, y); + states = fit_local_discretization(y); // 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(dataset, features, className, states); diff --git a/src/BayesNet/TANLd.cc b/src/BayesNet/TANLd.cc index dba803c..a30cba8 100644 --- a/src/BayesNet/TANLd.cc +++ b/src/BayesNet/TANLd.cc @@ -11,7 +11,7 @@ namespace bayesnet { Xf = X_; y = y_; // Fills vectors Xv & yv with the data from tensors X_ (discretized) & y - fit_local_discretization(states, y); + states = fit_local_discretization(y); // We have discretized the input data // 1st we need to fit the model to build the normal TAN structure, TAN::fit initializes the base Bayesian network TAN::fit(dataset, features, className, states);