2024-04-11 16:02:49 +00:00
|
|
|
// ***************************************************************
|
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
|
|
|
// SPDX-FileType: SOURCE
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// ***************************************************************
|
|
|
|
|
2024-03-08 21:20:54 +00:00
|
|
|
#include "Proposal.h"
|
2023-08-04 11:05:12 +00:00
|
|
|
|
|
|
|
namespace bayesnet {
|
2023-11-08 17:45:35 +00:00
|
|
|
Proposal::Proposal(torch::Tensor& dataset_, std::vector<std::string>& features_, std::string& className_) : pDataset(dataset_), pFeatures(features_), pClassName(className_) {}
|
2023-08-04 17:42:18 +00:00
|
|
|
Proposal::~Proposal()
|
|
|
|
{
|
|
|
|
for (auto& [key, value] : discretizers) {
|
|
|
|
delete value;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 10:09:35 +00:00
|
|
|
void Proposal::checkInput(const torch::Tensor& X, const torch::Tensor& y)
|
|
|
|
{
|
|
|
|
if (!torch::is_floating_point(X)) {
|
|
|
|
throw std::invalid_argument("X must be a floating point tensor");
|
|
|
|
}
|
|
|
|
if (torch::is_floating_point(y)) {
|
|
|
|
throw std::invalid_argument("y must be an integer tensor");
|
|
|
|
}
|
|
|
|
}
|
2023-11-08 17:45:35 +00:00
|
|
|
map<std::string, std::vector<int>> Proposal::localDiscretizationProposal(const map<std::string, std::vector<int>>& oldStates, Network& model)
|
2023-08-04 11:05:12 +00:00
|
|
|
{
|
|
|
|
// order of local discretization is important. no good 0, 1, 2...
|
2023-08-04 18:11:22 +00:00
|
|
|
// although we rediscretize features after the local discretization of every feature
|
2023-08-04 11:05:12 +00:00
|
|
|
auto order = model.topological_sort();
|
|
|
|
auto& nodes = model.getNodes();
|
2023-11-08 17:45:35 +00:00
|
|
|
map<std::string, std::vector<int>> states = oldStates;
|
|
|
|
std::vector<int> indicesToReDiscretize;
|
2023-08-04 11:05:12 +00:00
|
|
|
bool upgrade = false; // Flag to check if we need to upgrade the model
|
|
|
|
for (auto feature : order) {
|
|
|
|
auto nodeParents = nodes[feature]->getParents();
|
2023-08-04 17:42:18 +00:00
|
|
|
if (nodeParents.size() < 2) continue; // Only has class as parent
|
2023-08-04 11:05:12 +00:00
|
|
|
upgrade = true;
|
2023-08-04 17:42:18 +00:00
|
|
|
int index = find(pFeatures.begin(), pFeatures.end(), feature) - pFeatures.begin();
|
2023-08-04 18:11:22 +00:00
|
|
|
indicesToReDiscretize.push_back(index); // We need to re-discretize this feature
|
2023-11-08 17:45:35 +00:00
|
|
|
std::vector<std::string> parents;
|
2023-08-04 17:42:18 +00:00
|
|
|
transform(nodeParents.begin(), nodeParents.end(), back_inserter(parents), [](const auto& p) { return p->getName(); });
|
2023-08-04 11:05:12 +00:00
|
|
|
// Remove class as parent as it will be added later
|
2023-08-04 17:42:18 +00:00
|
|
|
parents.erase(remove(parents.begin(), parents.end(), pClassName), parents.end());
|
2023-08-04 11:05:12 +00:00
|
|
|
// Get the indices of the parents
|
2023-11-08 17:45:35 +00:00
|
|
|
std::vector<int> indices;
|
2023-08-07 10:49:37 +00:00
|
|
|
indices.push_back(-1); // Add class index
|
2023-08-04 17:42:18 +00:00
|
|
|
transform(parents.begin(), parents.end(), back_inserter(indices), [&](const auto& p) {return find(pFeatures.begin(), pFeatures.end(), p) - pFeatures.begin(); });
|
2023-08-04 18:11:22 +00:00
|
|
|
// Now we fit the discretizer of the feature, conditioned on its parents and the class i.e. discretizer.fit(X[index], X[indices] + y)
|
2023-11-08 17:45:35 +00:00
|
|
|
std::vector<std::string> yJoinParents(Xf.size(1));
|
2023-08-04 11:05:12 +00:00
|
|
|
for (auto idx : indices) {
|
2023-08-07 23:53:41 +00:00
|
|
|
for (int i = 0; i < Xf.size(1); ++i) {
|
2023-08-07 10:49:37 +00:00
|
|
|
yJoinParents[i] += to_string(pDataset.index({ idx, i }).item<int>());
|
2023-08-04 11:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-21 11:50:19 +00:00
|
|
|
auto yxv = factorize(yJoinParents);
|
2023-08-04 11:05:12 +00:00
|
|
|
auto xvf_ptr = Xf.index({ index }).data_ptr<float>();
|
2023-11-08 17:45:35 +00:00
|
|
|
auto xvf = std::vector<mdlp::precision_t>(xvf_ptr, xvf_ptr + Xf.size(1));
|
2023-08-04 11:05:12 +00:00
|
|
|
discretizers[feature]->fit(xvf, yxv);
|
2023-08-05 16:39:48 +00:00
|
|
|
}
|
|
|
|
if (upgrade) {
|
|
|
|
// Discretize again X (only the affected indices) with the new fitted discretizers
|
|
|
|
for (auto index : indicesToReDiscretize) {
|
|
|
|
auto Xt_ptr = Xf.index({ index }).data_ptr<float>();
|
2023-11-08 17:45:35 +00:00
|
|
|
auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
|
2023-08-07 10:49:37 +00:00
|
|
|
pDataset.index_put_({ index, "..." }, torch::tensor(discretizers[pFeatures[index]]->transform(Xt)));
|
2023-11-08 17:45:35 +00:00
|
|
|
auto xStates = std::vector<int>(discretizers[pFeatures[index]]->getCutPoints().size() + 1);
|
2023-08-05 16:39:48 +00:00
|
|
|
iota(xStates.begin(), xStates.end(), 0);
|
|
|
|
//Update new states of the feature/node
|
|
|
|
states[pFeatures[index]] = xStates;
|
|
|
|
}
|
2023-08-16 10:46:09 +00:00
|
|
|
const torch::Tensor weights = torch::full({ pDataset.size(1) }, 1.0 / pDataset.size(1), torch::kDouble);
|
2024-06-13 13:04:15 +00:00
|
|
|
model.fit(pDataset, weights, pFeatures, pClassName, states, Smoothing_t::ORIGINAL);
|
2023-08-04 11:05:12 +00:00
|
|
|
}
|
2023-08-12 14:16:17 +00:00
|
|
|
return states;
|
2023-08-04 11:05:12 +00:00
|
|
|
}
|
2023-11-08 17:45:35 +00:00
|
|
|
map<std::string, std::vector<int>> Proposal::fit_local_discretization(const torch::Tensor& y)
|
2023-08-04 11:05:12 +00:00
|
|
|
{
|
2023-08-12 09:49:18 +00:00
|
|
|
// Discretize the continuous input data and build pDataset (Classifier::dataset)
|
2023-08-07 23:53:41 +00:00
|
|
|
int m = Xf.size(1);
|
|
|
|
int n = Xf.size(0);
|
2023-11-08 17:45:35 +00:00
|
|
|
map<std::string, std::vector<int>> states;
|
|
|
|
pDataset = torch::zeros({ n + 1, m }, torch::kInt32);
|
|
|
|
auto yv = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + y.size(0));
|
2023-08-04 11:05:12 +00:00
|
|
|
// discretize input data by feature(row)
|
2023-08-07 10:49:37 +00:00
|
|
|
for (auto i = 0; i < pFeatures.size(); ++i) {
|
2023-08-04 11:05:12 +00:00
|
|
|
auto* discretizer = new mdlp::CPPFImdlp();
|
|
|
|
auto Xt_ptr = Xf.index({ i }).data_ptr<float>();
|
2023-11-08 17:45:35 +00:00
|
|
|
auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
|
2023-08-04 11:05:12 +00:00
|
|
|
discretizer->fit(Xt, yv);
|
2023-08-07 10:49:37 +00:00
|
|
|
pDataset.index_put_({ i, "..." }, torch::tensor(discretizer->transform(Xt)));
|
2023-11-08 17:45:35 +00:00
|
|
|
auto xStates = std::vector<int>(discretizer->getCutPoints().size() + 1);
|
2023-08-04 11:05:12 +00:00
|
|
|
iota(xStates.begin(), xStates.end(), 0);
|
2023-08-04 17:42:18 +00:00
|
|
|
states[pFeatures[i]] = xStates;
|
|
|
|
discretizers[pFeatures[i]] = discretizer;
|
2023-08-04 11:05:12 +00:00
|
|
|
}
|
|
|
|
int n_classes = torch::max(y).item<int>() + 1;
|
2023-11-08 17:45:35 +00:00
|
|
|
auto yStates = std::vector<int>(n_classes);
|
2023-08-04 11:05:12 +00:00
|
|
|
iota(yStates.begin(), yStates.end(), 0);
|
2023-08-04 17:42:18 +00:00
|
|
|
states[pClassName] = yStates;
|
2023-08-12 09:49:18 +00:00
|
|
|
pDataset.index_put_({ n, "..." }, y);
|
|
|
|
return states;
|
2023-08-04 11:05:12 +00:00
|
|
|
}
|
2023-08-05 16:39:48 +00:00
|
|
|
torch::Tensor Proposal::prepareX(torch::Tensor& X)
|
|
|
|
{
|
|
|
|
auto Xtd = torch::zeros_like(X, torch::kInt32);
|
|
|
|
for (int i = 0; i < X.size(0); ++i) {
|
2023-11-08 17:45:35 +00:00
|
|
|
auto Xt = std::vector<float>(X[i].data_ptr<float>(), X[i].data_ptr<float>() + X.size(1));
|
2023-08-05 16:39:48 +00:00
|
|
|
auto Xd = discretizers[pFeatures[i]]->transform(Xt);
|
|
|
|
Xtd.index_put_({ i }, torch::tensor(Xd, torch::kInt32));
|
|
|
|
}
|
|
|
|
return Xtd;
|
|
|
|
}
|
2024-05-21 11:50:19 +00:00
|
|
|
std::vector<int> Proposal::factorize(const std::vector<std::string>& labels_t)
|
|
|
|
{
|
|
|
|
std::vector<int> yy;
|
|
|
|
yy.reserve(labels_t.size());
|
|
|
|
std::map<std::string, int> labelMap;
|
|
|
|
int i = 0;
|
|
|
|
for (const std::string& label : labels_t) {
|
|
|
|
if (labelMap.find(label) == labelMap.end()) {
|
|
|
|
labelMap[label] = i++;
|
|
|
|
bool allDigits = std::all_of(label.begin(), label.end(), ::isdigit);
|
|
|
|
}
|
|
|
|
yy.push_back(labelMap[label]);
|
|
|
|
}
|
|
|
|
return yy;
|
|
|
|
}
|
2023-08-04 11:05:12 +00:00
|
|
|
}
|