BayesNet/bayesnet/classifiers/SPODELd.cc

50 lines
2.1 KiB
C++
Raw Permalink Normal View History

2024-04-11 16:02:49 +00:00
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include "SPODELd.h"
2023-08-05 21:11:36 +00:00
namespace bayesnet {
2023-08-10 00:06:18 +00:00
SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {}
2023-11-08 17:45:35 +00:00
SPODELd& SPODELd::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-05 21:11:36 +00:00
{
2023-08-24 10:09:35 +00:00
checkInput(X_, y_);
2023-08-05 21:11:36 +00:00
Xf = X_;
y = y_;
2024-04-07 00:08:37 +00:00
return commonFit(features_, className_, states_);
2023-08-05 21:11:36 +00:00
}
2024-04-07 00:08:37 +00:00
2023-11-08 17:45:35 +00:00
SPODELd& SPODELd::fit(torch::Tensor& dataset, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
2023-08-10 00:06:18 +00:00
{
2023-08-24 10:09:35 +00:00
if (!torch::is_floating_point(dataset)) {
throw std::runtime_error("Dataset must be a floating point tensor");
}
2023-08-10 00:06:18 +00:00
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
2024-04-07 00:08:37 +00:00
y = dataset.index({ -1, "..." }).clone().to(torch::kInt32);
return commonFit(features_, className_, states_);
}
SPODELd& SPODELd::commonFit(const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
{
2023-08-10 00:06:18 +00:00
features = features_;
className = className_;
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-10 00:06:18 +00:00
// 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);
states = localDiscretizationProposal(states, model);
2023-08-10 00:06:18 +00:00
return *this;
}
2023-11-08 17:45:35 +00:00
torch::Tensor SPODELd::predict(torch::Tensor& X)
2023-08-05 21:11:36 +00:00
{
auto Xt = prepareX(X);
return SPODE::predict(Xt);
}
2023-11-08 17:45:35 +00:00
std::vector<std::string> SPODELd::graph(const std::string& name) const
2023-08-05 21:11:36 +00:00
{
return SPODE::graph(name);
}
}