BayesNet 1.0.7.
Bayesian Network and basic classifiers Library.
Loading...
Searching...
No Matches
XSP2DE.h
1// ***************************************************************
2// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
3// SPDX-FileType: SOURCE
4// SPDX-License-Identifier: MIT
5// ***************************************************************
6
7#ifndef XSP2DE_H
8#define XSP2DE_H
9
10#include "Classifier.h"
11#include "bayesnet/utils/CountingSemaphore.h"
12#include <torch/torch.h>
13#include <vector>
14
15namespace bayesnet {
16
17class XSp2de : public Classifier {
18 public:
19 XSp2de(int spIndex1, int spIndex2);
20 void setHyperparameters(const nlohmann::json &hyperparameters_) override;
21 void fitx(torch::Tensor &X, torch::Tensor &y, torch::Tensor &weights_, const Smoothing_t smoothing);
22 std::vector<double> predict_proba(const std::vector<int> &instance) const;
23 std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>> &test_data) override;
24 int predict(const std::vector<int> &instance) const;
25 std::vector<int> predict(std::vector<std::vector<int>> &test_data) override;
26 torch::Tensor predict(torch::Tensor &X) override;
27 torch::Tensor predict_proba(torch::Tensor &X) override;
28
29 float score(torch::Tensor &X, torch::Tensor &y) override;
30 float score(std::vector<std::vector<int>> &X, std::vector<int> &y) override;
31 std::string to_string() const;
32 std::vector<std::string> graph(const std::string &title) const override {
33 return std::vector<std::string>({title});
34 }
35
36 int getNumberOfNodes() const override;
37 int getNumberOfEdges() const override;
38 int getNFeatures() const;
39 int getClassNumStates() const override;
40 int getNumberOfStates() const override;
41
42 protected:
43 void buildModel(const torch::Tensor &weights) override;
44 void trainModel(const torch::Tensor &weights, const bayesnet::Smoothing_t smoothing) override;
45
46 private:
47 void addSample(const std::vector<int> &instance, double weight);
48 void normalize(std::vector<double> &v) const;
49 void computeProbabilities();
50
51 int superParent1_;
52 int superParent2_;
53 int nFeatures_;
54 int statesClass_;
55 double alpha_;
56 double initializer_;
57
58 std::vector<int> states_;
59 std::vector<double> classCounts_;
60 std::vector<double> classPriors_;
61 std::vector<double> sp1FeatureCounts_, sp1FeatureProbs_;
62 std::vector<double> sp2FeatureCounts_, sp2FeatureProbs_;
63 // childOffsets_[f] will be the offset into childCounts_ for feature f.
64 // If f is either superParent1 or superParent2, childOffsets_[f] = -1
65 std::vector<int> childOffsets_;
66 // For each child f, we store p(x_f | c, sp1Val, sp2Val). We'll store the raw
67 // counts in childCounts_, and the probabilities in childProbs_, with a
68 // dimension block of size: states_[f]* statesClass_* states_[sp1]* states_[sp2].
69 std::vector<double> childCounts_;
70 std::vector<double> childProbs_;
71 CountingSemaphore &semaphore_;
72};
73
74} // namespace bayesnet
75#endif // XSP2DE_H