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-07-22 21:07:56 +00:00
|
|
|
#ifndef CLASSIFIER_H
|
|
|
|
#define CLASSIFIER_H
|
|
|
|
#include <torch/torch.h>
|
2024-03-08 21:20:54 +00:00
|
|
|
#include "bayesnet/utils/BayesMetrics.h"
|
|
|
|
#include "bayesnet/network/Network.h"
|
|
|
|
#include "bayesnet/BaseClassifier.h"
|
2023-07-22 21:07:56 +00:00
|
|
|
|
|
|
|
namespace bayesnet {
|
|
|
|
class Classifier : public BaseClassifier {
|
|
|
|
public:
|
|
|
|
Classifier(Network model);
|
|
|
|
virtual ~Classifier() = default;
|
2023-11-08 17:45:35 +00:00
|
|
|
Classifier& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
|
|
|
|
Classifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
|
|
|
|
Classifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
|
|
|
|
Classifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights) override;
|
2023-07-22 21:07:56 +00:00
|
|
|
void addNodes();
|
2023-08-07 23:53:41 +00:00
|
|
|
int getNumberOfNodes() const override;
|
|
|
|
int getNumberOfEdges() const override;
|
|
|
|
int getNumberOfStates() const override;
|
2024-02-22 10:45:40 +00:00
|
|
|
int getClassNumStates() const override;
|
2023-11-08 17:45:35 +00:00
|
|
|
torch::Tensor predict(torch::Tensor& X) override;
|
2024-02-22 10:45:40 +00:00
|
|
|
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
|
|
|
torch::Tensor predict_proba(torch::Tensor& X) override;
|
|
|
|
std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X) override;
|
2023-09-05 11:39:43 +00:00
|
|
|
status_t getStatus() const override { return status; }
|
2024-02-12 09:58:20 +00:00
|
|
|
std::string getVersion() override { return { project_version.begin(), project_version.end() }; };
|
2023-11-08 17:45:35 +00:00
|
|
|
float score(torch::Tensor& X, torch::Tensor& y) override;
|
|
|
|
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
|
|
|
|
std::vector<std::string> show() const override;
|
|
|
|
std::vector<std::string> topological_order() override;
|
2024-02-09 09:57:19 +00:00
|
|
|
std::vector<std::string> getNotes() const override { return notes; }
|
2024-04-07 23:25:14 +00:00
|
|
|
std::string dump_cpt() const override;
|
2023-11-19 21:36:27 +00:00
|
|
|
void setHyperparameters(const nlohmann::json& hyperparameters) override; //For classifiers that don't have hyperparameters
|
2024-02-22 10:45:40 +00:00
|
|
|
protected:
|
|
|
|
bool fitted;
|
2024-02-23 19:36:11 +00:00
|
|
|
unsigned int m, n; // m: number of samples, n: number of features
|
2024-02-22 10:45:40 +00:00
|
|
|
Network model;
|
|
|
|
Metrics metrics;
|
|
|
|
std::vector<std::string> features;
|
|
|
|
std::string className;
|
|
|
|
std::map<std::string, std::vector<int>> states;
|
|
|
|
torch::Tensor dataset; // (n+1)xm tensor
|
|
|
|
status_t status = NORMAL;
|
|
|
|
std::vector<std::string> notes; // Used to store messages occurred during the fit process
|
|
|
|
void checkFitParameters();
|
|
|
|
virtual void buildModel(const torch::Tensor& weights) = 0;
|
|
|
|
void trainModel(const torch::Tensor& weights) override;
|
|
|
|
void buildDataset(torch::Tensor& y);
|
|
|
|
private:
|
|
|
|
Classifier& build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
|
2023-07-22 21:07:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|