Exception if hyperparameters not valid
This commit is contained in:
parent
86ffdfd6f3
commit
1c6af619b5
19
.clang-uml
Normal file
19
.clang-uml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
compilation_database_dir: build
|
||||||
|
output_directory: puml
|
||||||
|
diagrams:
|
||||||
|
myproject_class:
|
||||||
|
type: class
|
||||||
|
glob:
|
||||||
|
- src/bayesnet/*.cc
|
||||||
|
- src/platform/*.cc
|
||||||
|
using_namespace: bayesnet
|
||||||
|
include:
|
||||||
|
namespaces:
|
||||||
|
- bayesnet
|
||||||
|
- platform
|
||||||
|
exclude:
|
||||||
|
namespaces:
|
||||||
|
- myproject::detail
|
||||||
|
plantuml:
|
||||||
|
after:
|
||||||
|
- 'note left of {{ alias("MyProjectMain") }}: Main class of myproject library.'
|
@ -10,7 +10,6 @@ namespace bayesnet {
|
|||||||
AODE();
|
AODE();
|
||||||
virtual ~AODE() {};
|
virtual ~AODE() {};
|
||||||
vector<string> graph(const string& title = "AODE") const override;
|
vector<string> graph(const string& title = "AODE") const override;
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -6,7 +6,7 @@ namespace bayesnet {
|
|||||||
AODELd::AODELd() : Ensemble(), Proposal(dataset, features, className) {}
|
AODELd::AODELd() : Ensemble(), Proposal(dataset, features, className) {}
|
||||||
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||||
{
|
{
|
||||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
checkInput(X_, y_);
|
||||||
features = features_;
|
features = features_;
|
||||||
className = className_;
|
className = className_;
|
||||||
Xf = X_;
|
Xf = X_;
|
||||||
|
@ -14,9 +14,8 @@ namespace bayesnet {
|
|||||||
AODELd();
|
AODELd();
|
||||||
AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_) override;
|
AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_) override;
|
||||||
virtual ~AODELd() = default;
|
virtual ~AODELd() = default;
|
||||||
vector<string> graph(const string& name = "AODE") const override;
|
vector<string> graph(const string& name = "AODELd") const override;
|
||||||
static inline string version() { return "0.0.1"; };
|
static inline string version() { return "0.0.1"; };
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif // !AODELD_H
|
#endif // !AODELD_H
|
@ -71,6 +71,9 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
void Classifier::checkFitParameters()
|
void Classifier::checkFitParameters()
|
||||||
{
|
{
|
||||||
|
if (torch::is_floating_point(dataset)) {
|
||||||
|
throw invalid_argument("dataset (X, y) must be of type Integer");
|
||||||
|
}
|
||||||
if (n != features.size()) {
|
if (n != features.size()) {
|
||||||
throw invalid_argument("X " + to_string(n) + " and features " + to_string(features.size()) + " must have the same number of features");
|
throw invalid_argument("X " + to_string(n) + " and features " + to_string(features.size()) + " must have the same number of features");
|
||||||
}
|
}
|
||||||
@ -160,4 +163,10 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void Classifier::setHyperparameters(nlohmann::json& hyperparameters)
|
||||||
|
{
|
||||||
|
// Check if hyperparameters are valid, default is no hyperparameters
|
||||||
|
const vector<string> validKeys = { };
|
||||||
|
checkHyperparameters(validKeys, hyperparameters);
|
||||||
|
}
|
||||||
}
|
}
|
@ -43,6 +43,7 @@ namespace bayesnet {
|
|||||||
vector<string> show() const override;
|
vector<string> show() const override;
|
||||||
vector<string> topological_order() override;
|
vector<string> topological_order() override;
|
||||||
void dump_cpt() const override;
|
void dump_cpt() const override;
|
||||||
|
void setHyperparameters(nlohmann::json& hyperparameters) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -5,7 +5,7 @@ namespace bayesnet {
|
|||||||
KDBLd::KDBLd(int k) : KDB(k), Proposal(dataset, features, className) {}
|
KDBLd::KDBLd(int k) : KDB(k), Proposal(dataset, features, className) {}
|
||||||
KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||||
{
|
{
|
||||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
checkInput(X_, y_);
|
||||||
features = features_;
|
features = features_;
|
||||||
className = className_;
|
className = className_;
|
||||||
Xf = X_;
|
Xf = X_;
|
||||||
|
@ -13,7 +13,6 @@ namespace bayesnet {
|
|||||||
KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||||
vector<string> graph(const string& name = "KDB") const override;
|
vector<string> graph(const string& name = "KDB") const override;
|
||||||
Tensor predict(Tensor& X) override;
|
Tensor predict(Tensor& X) override;
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
static inline string version() { return "0.0.1"; };
|
static inline string version() { return "0.0.1"; };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,15 @@ namespace bayesnet {
|
|||||||
delete value;
|
delete value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void Proposal::checkInput(const torch::Tensor& X, const torch::Tensor& y)
|
||||||
|
{
|
||||||
|
if (!torch::is_floating_point(X)) {
|
||||||
|
throw std::invalid_argument("X must be a floating point tensor");
|
||||||
|
}
|
||||||
|
if (torch::is_floating_point(y)) {
|
||||||
|
throw std::invalid_argument("y must be an integer tensor");
|
||||||
|
}
|
||||||
|
}
|
||||||
map<string, vector<int>> Proposal::localDiscretizationProposal(const map<string, vector<int>>& oldStates, Network& model)
|
map<string, vector<int>> Proposal::localDiscretizationProposal(const map<string, vector<int>>& oldStates, Network& model)
|
||||||
{
|
{
|
||||||
// order of local discretization is important. no good 0, 1, 2...
|
// order of local discretization is important. no good 0, 1, 2...
|
||||||
|
@ -13,6 +13,7 @@ namespace bayesnet {
|
|||||||
Proposal(torch::Tensor& pDataset, vector<string>& features_, string& className_);
|
Proposal(torch::Tensor& pDataset, vector<string>& features_, string& className_);
|
||||||
virtual ~Proposal();
|
virtual ~Proposal();
|
||||||
protected:
|
protected:
|
||||||
|
void checkInput(const torch::Tensor& X, const torch::Tensor& y);
|
||||||
torch::Tensor prepareX(torch::Tensor& X);
|
torch::Tensor prepareX(torch::Tensor& X);
|
||||||
map<string, vector<int>> localDiscretizationProposal(const map<string, vector<int>>& states, Network& model);
|
map<string, vector<int>> localDiscretizationProposal(const map<string, vector<int>>& states, Network& model);
|
||||||
map<string, vector<int>> fit_local_discretization(const torch::Tensor& y);
|
map<string, vector<int>> fit_local_discretization(const torch::Tensor& y);
|
||||||
|
@ -12,7 +12,6 @@ namespace bayesnet {
|
|||||||
explicit SPODE(int root);
|
explicit SPODE(int root);
|
||||||
virtual ~SPODE() {};
|
virtual ~SPODE() {};
|
||||||
vector<string> graph(const string& name = "SPODE") const override;
|
vector<string> graph(const string& name = "SPODE") const override;
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -5,7 +5,7 @@ namespace bayesnet {
|
|||||||
SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {}
|
SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {}
|
||||||
SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||||
{
|
{
|
||||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
checkInput(X_, y_);
|
||||||
features = features_;
|
features = features_;
|
||||||
className = className_;
|
className = className_;
|
||||||
Xf = X_;
|
Xf = X_;
|
||||||
@ -20,9 +20,11 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
SPODELd& SPODELd::fit(torch::Tensor& dataset, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
SPODELd& SPODELd::fit(torch::Tensor& dataset, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||||
{
|
{
|
||||||
|
if (!torch::is_floating_point(dataset)) {
|
||||||
|
throw std::runtime_error("Dataset must be a floating point tensor");
|
||||||
|
}
|
||||||
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
|
Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
|
||||||
y = dataset.index({ -1, "..." }).clone();
|
y = dataset.index({ -1, "..." }).clone();
|
||||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
|
||||||
features = features_;
|
features = features_;
|
||||||
className = className_;
|
className = className_;
|
||||||
// Fills vectors Xv & yv with the data from tensors X_ (discretized) & y
|
// Fills vectors Xv & yv with the data from tensors X_ (discretized) & y
|
||||||
|
@ -13,7 +13,6 @@ namespace bayesnet {
|
|||||||
SPODELd& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
SPODELd& fit(torch::Tensor& dataset, const vector<string>& features, const string& className, map<string, vector<int>>& states) override;
|
||||||
vector<string> graph(const string& name = "SPODE") const override;
|
vector<string> graph(const string& name = "SPODE") const override;
|
||||||
Tensor predict(Tensor& X) override;
|
Tensor predict(Tensor& X) override;
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
static inline string version() { return "0.0.1"; };
|
static inline string version() { return "0.0.1"; };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ namespace bayesnet {
|
|||||||
TAN();
|
TAN();
|
||||||
virtual ~TAN() {};
|
virtual ~TAN() {};
|
||||||
vector<string> graph(const string& name = "TAN") const override;
|
vector<string> graph(const string& name = "TAN") const override;
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -5,7 +5,7 @@ namespace bayesnet {
|
|||||||
TANLd::TANLd() : TAN(), Proposal(dataset, features, className) {}
|
TANLd::TANLd() : TAN(), Proposal(dataset, features, className) {}
|
||||||
TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, const vector<string>& features_, const string& className_, map<string, vector<int>>& states_)
|
||||||
{
|
{
|
||||||
// This first part should go in a Classifier method called fit_local_discretization o fit_float...
|
checkInput(X_, y_);
|
||||||
features = features_;
|
features = features_;
|
||||||
className = className_;
|
className = className_;
|
||||||
Xf = X_;
|
Xf = X_;
|
||||||
|
@ -14,7 +14,6 @@ namespace bayesnet {
|
|||||||
vector<string> graph(const string& name = "TAN") const override;
|
vector<string> graph(const string& name = "TAN") const override;
|
||||||
Tensor predict(Tensor& X) override;
|
Tensor predict(Tensor& X) override;
|
||||||
static inline string version() { return "0.0.1"; };
|
static inline string version() { return "0.0.1"; };
|
||||||
void setHyperparameters(nlohmann::json& hyperparameters) override {};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif // !TANLD_H
|
#endif // !TANLD_H
|
Loading…
Reference in New Issue
Block a user