Fit PyWrap into BayesNet
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
#ifndef CLASSIFIER_H
|
||||
#define CLASSIFIER_H
|
||||
#include <torch/torch.h>
|
||||
#include "BaseClassifier.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace pywrap {
|
||||
class Classifier : bayesnet::BaseClassifier {
|
||||
public:
|
||||
Classifier() = default;
|
||||
virtual ~Classifier() = default;
|
||||
virtual Classifier& fit(torch::Tensor& X, torch::Tensor& y) = 0;
|
||||
virtual std::string version() = 0;
|
||||
virtual std::string sklearnVersion() = 0;
|
||||
protected:
|
||||
virtual void checkHyperparameters(const std::vector<std::string>& validKeys, const nlohmann::json& hyperparameters) = 0;
|
||||
};
|
||||
} /* namespace pywrap */
|
||||
#endif /* CLASSIFIER_H */
|
@@ -5,7 +5,7 @@ namespace pywrap {
|
||||
{
|
||||
return callMethodString("graph");
|
||||
}
|
||||
void ODTE::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||
void ODTE::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid
|
||||
const std::vector<std::string> validKeys = { "n_jobs", "n_estimators", "random_state" };
|
||||
|
@@ -9,7 +9,7 @@ namespace pywrap {
|
||||
ODTE() : PyClassifier("odte", "Odte") {};
|
||||
~ODTE() = default;
|
||||
std::string graph();
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||
};
|
||||
} /* namespace pywrap */
|
||||
#endif /* ODTE_H */
|
@@ -74,15 +74,15 @@ namespace pywrap {
|
||||
Py_XDECREF(incoming);
|
||||
return resultTensor;
|
||||
}
|
||||
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||
float PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||
{
|
||||
auto [Xn, yn] = tensors2numpy(X, y);
|
||||
CPyObject Xp = bp::incref(bp::object(Xn).ptr());
|
||||
CPyObject yp = bp::incref(bp::object(yn).ptr());
|
||||
auto result = pyWrap->score(id, Xp, yp);
|
||||
float result = pyWrap->score(id, Xp, yp);
|
||||
return result;
|
||||
}
|
||||
void PyClassifier::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||
void PyClassifier::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid, default is no hyperparameters
|
||||
const std::vector<std::string> validKeys = { };
|
||||
|
@@ -13,21 +13,37 @@
|
||||
#include "TypeId.h"
|
||||
|
||||
namespace pywrap {
|
||||
class PyClassifier : public Classifier {
|
||||
class PyClassifier : public bayesnet::BaseClassifier {
|
||||
public:
|
||||
PyClassifier(const std::string& module, const std::string& className);
|
||||
virtual ~PyClassifier();
|
||||
PyClassifier& 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 { return *this; };
|
||||
// X is nxm tensor, y is nx1 tensor
|
||||
PyClassifier& 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;
|
||||
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y) override;
|
||||
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y);
|
||||
PyClassifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override { return *this; };
|
||||
PyClassifier& 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) { return *this; };
|
||||
torch::Tensor predict(torch::Tensor& X) override;
|
||||
double score(torch::Tensor& X, torch::Tensor& y) override;
|
||||
std::string version() override;
|
||||
std::string sklearnVersion() override;
|
||||
std::vector<int> predict(std::vector<std::vector<int >>& X) override { return std::vector<int>(); };
|
||||
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override { return 0.0; };
|
||||
float score(torch::Tensor& X, torch::Tensor& y) override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||
std::string version();
|
||||
std::string sklearnVersion();
|
||||
std::string callMethodString(const std::string& method);
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
std::string getVersion() override { return this->version(); };
|
||||
int getNumberOfNodes()const override { return 0; };
|
||||
int getNumberOfEdges()const override { return 0; };
|
||||
int getNumberOfStates() const override { return 0; };
|
||||
std::vector<std::string> show() const override { return std::vector<std::string>(); }
|
||||
std::vector<std::string> graph(const std::string& title = "") const override { return std::vector<std::string>(); }
|
||||
bayesnet::status_t getStatus() const override { return bayesnet::NORMAL; };
|
||||
std::vector<std::string> topological_order() override { return std::vector<std::string>(); }
|
||||
void dump_cpt() const override {};
|
||||
protected:
|
||||
void checkHyperparameters(const std::vector<std::string>& validKeys, const nlohmann::json& hyperparameters) override;
|
||||
void checkHyperparameters(const std::vector<std::string>& validKeys, const nlohmann::json& hyperparameters);
|
||||
nlohmann::json hyperparameters;
|
||||
void trainModel(const torch::Tensor& weights) override {};
|
||||
private:
|
||||
PyWrap* pyWrap;
|
||||
std::string module;
|
||||
|
@@ -5,7 +5,7 @@ namespace pywrap {
|
||||
{
|
||||
return callMethodString("graph");
|
||||
}
|
||||
void STree::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||
void STree::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid
|
||||
const std::vector<std::string> validKeys = { "C", "n_jobs", "kernel", "max_iter", "max_depth", "random_state", "multiclass_strategy" };
|
||||
|
@@ -9,7 +9,7 @@ namespace pywrap {
|
||||
STree() : PyClassifier("stree", "Stree") {};
|
||||
~STree() = default;
|
||||
std::string graph();
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||
};
|
||||
} /* namespace pywrap */
|
||||
#endif /* STREE_H */
|
@@ -5,7 +5,7 @@ namespace pywrap {
|
||||
{
|
||||
return sklearnVersion();
|
||||
}
|
||||
void SVC::setHyperparameters(const nlohmann::json& hyperparameters)
|
||||
void SVC::setHyperparameters(nlohmann::json& hyperparameters)
|
||||
{
|
||||
// Check if hyperparameters are valid
|
||||
const std::vector<std::string> validKeys = { "C", "gamma", "kernel", "random_state" };
|
||||
|
@@ -8,7 +8,7 @@ namespace pywrap {
|
||||
SVC() : PyClassifier("sklearn.svm", "SVC") {};
|
||||
~SVC() = default;
|
||||
std::string version();
|
||||
void setHyperparameters(const nlohmann::json& hyperparameters) override;
|
||||
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||
};
|
||||
|
||||
} /* namespace pywrap */
|
||||
|
Reference in New Issue
Block a user