BayesNet 1.0.7.
Bayesian Network and basic classifiers Library.
Loading...
Searching...
No Matches
Proposal.cc
1// ***************************************************************
2// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
3// SPDX-FileType: SOURCE
4// SPDX-License-Identifier: MIT
5// ***************************************************************
6
7#include "Proposal.h"
8
9namespace bayesnet {
10 Proposal::Proposal(torch::Tensor& dataset_, std::vector<std::string>& features_, std::string& className_) : pDataset(dataset_), pFeatures(features_), pClassName(className_) {}
11 Proposal::~Proposal()
12 {
13 for (auto& [key, value] : discretizers) {
14 delete value;
15 }
16 }
17 void Proposal::checkInput(const torch::Tensor& X, const torch::Tensor& y)
18 {
19 if (!torch::is_floating_point(X)) {
20 throw std::invalid_argument("X must be a floating point tensor");
21 }
22 if (torch::is_floating_point(y)) {
23 throw std::invalid_argument("y must be an integer tensor");
24 }
25 }
26 map<std::string, std::vector<int>> Proposal::localDiscretizationProposal(const map<std::string, std::vector<int>>& oldStates, Network& model)
27 {
28 // order of local discretization is important. no good 0, 1, 2...
29 // although we rediscretize features after the local discretization of every feature
30 auto order = model.topological_sort();
31 auto& nodes = model.getNodes();
32 map<std::string, std::vector<int>> states = oldStates;
33 std::vector<int> indicesToReDiscretize;
34 bool upgrade = false; // Flag to check if we need to upgrade the model
35 for (auto feature : order) {
36 auto nodeParents = nodes[feature]->getParents();
37 if (nodeParents.size() < 2) continue; // Only has class as parent
38 upgrade = true;
39 int index = find(pFeatures.begin(), pFeatures.end(), feature) - pFeatures.begin();
40 indicesToReDiscretize.push_back(index); // We need to re-discretize this feature
41 std::vector<std::string> parents;
42 transform(nodeParents.begin(), nodeParents.end(), back_inserter(parents), [](const auto& p) { return p->getName(); });
43 // Remove class as parent as it will be added later
44 parents.erase(remove(parents.begin(), parents.end(), pClassName), parents.end());
45 // Get the indices of the parents
46 std::vector<int> indices;
47 indices.push_back(-1); // Add class index
48 transform(parents.begin(), parents.end(), back_inserter(indices), [&](const auto& p) {return find(pFeatures.begin(), pFeatures.end(), p) - pFeatures.begin(); });
49 // Now we fit the discretizer of the feature, conditioned on its parents and the class i.e. discretizer.fit(X[index], X[indices] + y)
50 std::vector<std::string> yJoinParents(Xf.size(1));
51 for (auto idx : indices) {
52 for (int i = 0; i < Xf.size(1); ++i) {
53 yJoinParents[i] += to_string(pDataset.index({ idx, i }).item<int>());
54 }
55 }
56 auto yxv = factorize(yJoinParents);
57 auto xvf_ptr = Xf.index({ index }).data_ptr<float>();
58 auto xvf = std::vector<mdlp::precision_t>(xvf_ptr, xvf_ptr + Xf.size(1));
59 discretizers[feature]->fit(xvf, yxv);
60 }
61 if (upgrade) {
62 // Discretize again X (only the affected indices) with the new fitted discretizers
63 for (auto index : indicesToReDiscretize) {
64 auto Xt_ptr = Xf.index({ index }).data_ptr<float>();
65 auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
66 pDataset.index_put_({ index, "..." }, torch::tensor(discretizers[pFeatures[index]]->transform(Xt)));
67 auto xStates = std::vector<int>(discretizers[pFeatures[index]]->getCutPoints().size() + 1);
68 iota(xStates.begin(), xStates.end(), 0);
69 //Update new states of the feature/node
70 states[pFeatures[index]] = xStates;
71 }
72 const torch::Tensor weights = torch::full({ pDataset.size(1) }, 1.0 / pDataset.size(1), torch::kDouble);
73 model.fit(pDataset, weights, pFeatures, pClassName, states, Smoothing_t::ORIGINAL);
74 }
75 return states;
76 }
77 map<std::string, std::vector<int>> Proposal::fit_local_discretization(const torch::Tensor& y)
78 {
79 // Discretize the continuous input data and build pDataset (Classifier::dataset)
80 int m = Xf.size(1);
81 int n = Xf.size(0);
82 map<std::string, std::vector<int>> states;
83 pDataset = torch::zeros({ n + 1, m }, torch::kInt32);
84 auto yv = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + y.size(0));
85 // discretize input data by feature(row)
86 for (auto i = 0; i < pFeatures.size(); ++i) {
87 auto* discretizer = new mdlp::CPPFImdlp();
88 auto Xt_ptr = Xf.index({ i }).data_ptr<float>();
89 auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
90 discretizer->fit(Xt, yv);
91 pDataset.index_put_({ i, "..." }, torch::tensor(discretizer->transform(Xt)));
92 auto xStates = std::vector<int>(discretizer->getCutPoints().size() + 1);
93 iota(xStates.begin(), xStates.end(), 0);
94 states[pFeatures[i]] = xStates;
95 discretizers[pFeatures[i]] = discretizer;
96 }
97 int n_classes = torch::max(y).item<int>() + 1;
98 auto yStates = std::vector<int>(n_classes);
99 iota(yStates.begin(), yStates.end(), 0);
100 states[pClassName] = yStates;
101 pDataset.index_put_({ n, "..." }, y);
102 return states;
103 }
104 torch::Tensor Proposal::prepareX(torch::Tensor& X)
105 {
106 auto Xtd = torch::zeros_like(X, torch::kInt32);
107 for (int i = 0; i < X.size(0); ++i) {
108 auto Xt = std::vector<float>(X[i].data_ptr<float>(), X[i].data_ptr<float>() + X.size(1));
109 auto Xd = discretizers[pFeatures[i]]->transform(Xt);
110 Xtd.index_put_({ i }, torch::tensor(Xd, torch::kInt32));
111 }
112 return Xtd;
113 }
114 std::vector<int> Proposal::factorize(const std::vector<std::string>& labels_t)
115 {
116 std::vector<int> yy;
117 yy.reserve(labels_t.size());
118 std::map<std::string, int> labelMap;
119 int i = 0;
120 for (const std::string& label : labels_t) {
121 if (labelMap.find(label) == labelMap.end()) {
122 labelMap[label] = i++;
123 bool allDigits = std::all_of(label.begin(), label.end(), ::isdigit);
124 }
125 yy.push_back(labelMap[label]);
126 }
127 return yy;
128 }
129}