2024-04-11 16:02:49 +00:00
|
|
|
// ***************************************************************
|
|
|
|
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
|
|
|
|
// SPDX-FileType: SOURCE
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// ***************************************************************
|
|
|
|
|
2023-08-06 09:31:44 +00:00
|
|
|
#include "AODELd.h"
|
|
|
|
|
|
|
|
namespace bayesnet {
|
2024-02-24 17:36:09 +00:00
|
|
|
AODELd::AODELd(bool predict_voting) : Ensemble(predict_voting), Proposal(dataset, features, className)
|
|
|
|
{
|
|
|
|
}
|
2023-11-08 17:45:35 +00:00
|
|
|
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
|
2023-08-06 09:31:44 +00:00
|
|
|
{
|
2023-08-24 10:09:35 +00:00
|
|
|
checkInput(X_, y_);
|
2023-08-06 09:31:44 +00:00
|
|
|
features = features_;
|
|
|
|
className = className_;
|
2023-08-07 23:53:41 +00:00
|
|
|
Xf = X_;
|
|
|
|
y = y_;
|
2023-11-08 17:45:35 +00:00
|
|
|
// Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
|
2023-08-12 09:49:18 +00:00
|
|
|
states = fit_local_discretization(y);
|
2023-08-07 23:53:41 +00:00
|
|
|
// 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);
|
2023-08-06 09:31:44 +00:00
|
|
|
return *this;
|
2023-08-07 23:53:41 +00:00
|
|
|
|
2023-08-06 09:31:44 +00:00
|
|
|
}
|
2023-08-15 13:04:56 +00:00
|
|
|
void AODELd::buildModel(const torch::Tensor& weights)
|
2023-08-06 09:31:44 +00:00
|
|
|
{
|
|
|
|
models.clear();
|
|
|
|
for (int i = 0; i < features.size(); ++i) {
|
2023-08-10 00:06:18 +00:00
|
|
|
models.push_back(std::make_unique<SPODELd>(i));
|
2023-08-06 09:31:44 +00:00
|
|
|
}
|
2023-08-07 23:53:41 +00:00
|
|
|
n_models = models.size();
|
2023-11-08 17:45:35 +00:00
|
|
|
significanceModels = std::vector<double>(n_models, 1.0);
|
2023-08-06 09:31:44 +00:00
|
|
|
}
|
2023-08-15 13:04:56 +00:00
|
|
|
void AODELd::trainModel(const torch::Tensor& weights)
|
2023-08-07 10:49:37 +00:00
|
|
|
{
|
|
|
|
for (const auto& model : models) {
|
2023-08-10 00:06:18 +00:00
|
|
|
model->fit(Xf, y, features, className, states);
|
2023-08-07 10:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-08 17:45:35 +00:00
|
|
|
std::vector<std::string> AODELd::graph(const std::string& name) const
|
2023-08-06 09:31:44 +00:00
|
|
|
{
|
|
|
|
return Ensemble::graph(name);
|
|
|
|
}
|
|
|
|
}
|