Line data Source code
1 : // ***************************************************************
2 : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
3 : // SPDX-FileType: SOURCE
4 : // SPDX-License-Identifier: MIT
5 : // ***************************************************************
6 :
7 : #include "SPODELd.h"
8 :
9 : namespace bayesnet {
10 55 : SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {}
11 42 : 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_)
12 : {
13 42 : checkInput(X_, y_);
14 42 : Xf = X_;
15 42 : y = y_;
16 42 : return commonFit(features_, className_, states_);
17 : }
18 :
19 2 : SPODELd& SPODELd::fit(torch::Tensor& dataset, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
20 : {
21 2 : if (!torch::is_floating_point(dataset)) {
22 1 : throw std::runtime_error("Dataset must be a floating point tensor");
23 : }
24 4 : Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
25 3 : y = dataset.index({ -1, "..." }).clone().to(torch::kInt32);
26 1 : return commonFit(features_, className_, states_);
27 3 : }
28 :
29 43 : SPODELd& SPODELd::commonFit(const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
30 : {
31 43 : features = features_;
32 43 : className = className_;
33 : // Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
34 43 : states = fit_local_discretization(y);
35 : // We have discretized the input data
36 : // 1st we need to fit the model to build the normal SPODE structure, SPODE::fit initializes the base Bayesian network
37 43 : SPODE::fit(dataset, features, className, states);
38 43 : states = localDiscretizationProposal(states, model);
39 43 : return *this;
40 : }
41 34 : torch::Tensor SPODELd::predict(torch::Tensor& X)
42 : {
43 34 : auto Xt = prepareX(X);
44 68 : return SPODE::predict(Xt);
45 34 : }
46 9 : std::vector<std::string> SPODELd::graph(const std::string& name) const
47 : {
48 9 : return SPODE::graph(name);
49 : }
50 : }
|