Redo pass states to Network Fit needed in crossval

fix mistake in headerline (report)
This commit is contained in:
Ricardo Montañana Gómez 2023-08-12 11:10:53 +02:00
parent 0ad5505c16
commit 3a85481a5a
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
6 changed files with 31 additions and 33 deletions

4
.vscode/launch.json vendored
View File

@ -25,12 +25,12 @@
"program": "${workspaceFolder}/build/src/Platform/main", "program": "${workspaceFolder}/build/src/Platform/main",
"args": [ "args": [
"-m", "-m",
"AODELd", "TANLd",
"-p", "-p",
"/Users/rmontanana/Code/discretizbench/datasets", "/Users/rmontanana/Code/discretizbench/datasets",
"--stratified", "--stratified",
"-d", "-d",
"iris" "vehicle"
], ],
"cwd": "/Users/rmontanana/Code/discretizbench", "cwd": "/Users/rmontanana/Code/discretizbench",
}, },

View File

@ -37,7 +37,7 @@ namespace bayesnet {
} }
void Classifier::trainModel() void Classifier::trainModel()
{ {
model.fit(dataset, features, className); model.fit(dataset, features, className, states);
} }
// X is nxm where n is the number of features and m the number of samples // X is nxm where n is the number of features and m the number of samples
Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states) Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, vector<string>& features, string className, map<string, vector<int>>& states)

View File

@ -104,7 +104,7 @@ namespace bayesnet {
{ {
return nodes; return nodes;
} }
void Network::checkFitData(int n_samples, int n_features, int n_samples_y, const vector<string>& featureNames, const string& className) void Network::checkFitData(int n_samples, int n_features, int n_samples_y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
{ {
if (n_samples != n_samples_y) { if (n_samples != n_samples_y) {
throw invalid_argument("X and y must have the same number of samples in Network::fit (" + to_string(n_samples) + " != " + to_string(n_samples_y) + ")"); throw invalid_argument("X and y must have the same number of samples in Network::fit (" + to_string(n_samples) + " != " + to_string(n_samples_y) + ")");
@ -122,39 +122,42 @@ namespace bayesnet {
if (find(features.begin(), features.end(), feature) == features.end()) { if (find(features.begin(), features.end(), feature) == features.end()) {
throw invalid_argument("Feature " + feature + " not found in Network::features"); throw invalid_argument("Feature " + feature + " not found in Network::features");
} }
if (states.find(feature) == states.end()) {
throw invalid_argument("Feature " + feature + " not found in states");
} }
} }
void Network::setStates() }
void Network::setStates(const map<string, vector<int>>& states)
{ {
// Set states to every Node in the network // Set states to every Node in the network
for (int i = 0; i < features.size(); ++i) { for (int i = 0; i < features.size(); ++i) {
nodes[features[i]]->setNumStates(static_cast<int>(torch::max(samples.index({ i, "..." })).item<int>()) + 1); nodes[features[i]]->setNumStates(states.at(features[i]).size());
} }
classNumStates = nodes[className]->getNumStates(); classNumStates = nodes[className]->getNumStates();
} }
// X comes in nxm, where n is the number of features and m the number of samples // X comes in nxm, where n is the number of features and m the number of samples
void Network::fit(const torch::Tensor& X, const torch::Tensor& y, const vector<string>& featureNames, const string& className) void Network::fit(const torch::Tensor& X, const torch::Tensor& y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
{ {
checkFitData(X.size(1), X.size(0), y.size(0), featureNames, className); checkFitData(X.size(1), X.size(0), y.size(0), featureNames, className, states);
this->className = className; this->className = className;
Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1); Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
samples = torch::cat({ X , ytmp }, 0); samples = torch::cat({ X , ytmp }, 0);
for (int i = 0; i < featureNames.size(); ++i) { for (int i = 0; i < featureNames.size(); ++i) {
auto row_feature = X.index({ i, "..." }); auto row_feature = X.index({ i, "..." });
} }
completeFit(); completeFit(states);
} }
void Network::fit(const torch::Tensor& samples, const vector<string>& featureNames, const string& className) void Network::fit(const torch::Tensor& samples, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
{ {
checkFitData(samples.size(1), samples.size(0) - 1, samples.size(1), featureNames, className); checkFitData(samples.size(1), samples.size(0) - 1, samples.size(1), featureNames, className, states);
this->className = className; this->className = className;
this->samples = samples; this->samples = samples;
completeFit(); completeFit(states);
} }
// input_data comes in nxm, where n is the number of features and m the number of samples // input_data comes in nxm, where n is the number of features and m the number of samples
void Network::fit(const vector<vector<int>>& input_data, const vector<int>& labels, const vector<string>& featureNames, const string& className) void Network::fit(const vector<vector<int>>& input_data, const vector<int>& labels, const vector<string>& featureNames, const string& className, const map<string, vector<int>>& states)
{ {
checkFitData(input_data[0].size(), input_data.size(), labels.size(), featureNames, className); checkFitData(input_data[0].size(), input_data.size(), labels.size(), featureNames, className, states);
this->className = className; this->className = className;
// Build tensor of samples (nxm) (n+1 because of the class) // Build tensor of samples (nxm) (n+1 because of the class)
samples = torch::zeros({ static_cast<int>(input_data.size() + 1), static_cast<int>(input_data[0].size()) }, torch::kInt32); samples = torch::zeros({ static_cast<int>(input_data.size() + 1), static_cast<int>(input_data[0].size()) }, torch::kInt32);
@ -162,11 +165,11 @@ namespace bayesnet {
samples.index_put_({ i, "..." }, torch::tensor(input_data[i], torch::kInt32)); samples.index_put_({ i, "..." }, torch::tensor(input_data[i], torch::kInt32));
} }
samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32)); samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
completeFit(); completeFit(states);
} }
void Network::completeFit() void Network::completeFit(const map<string, vector<int>>& states)
{ {
setStates(); setStates(states);
int maxThreadsRunning = static_cast<int>(std::thread::hardware_concurrency() * maxThreads); int maxThreadsRunning = static_cast<int>(std::thread::hardware_concurrency() * maxThreads);
if (maxThreadsRunning < 1) { if (maxThreadsRunning < 1) {
maxThreadsRunning = 1; maxThreadsRunning = 1;
@ -212,7 +215,7 @@ namespace bayesnet {
torch::Tensor result; torch::Tensor result;
result = torch::zeros({ samples.size(1), classNumStates }, torch::kFloat64); result = torch::zeros({ samples.size(1), classNumStates }, torch::kFloat64);
for (int i = 0; i < samples.size(1); ++i) { for (int i = 0; i < samples.size(1); ++i) {
auto sample = samples.index({ "...", i }); const Tensor sample = samples.index({ "...", i });
auto psample = predict_sample(sample); auto psample = predict_sample(sample);
auto temp = torch::tensor(psample, torch::kFloat64); auto temp = torch::tensor(psample, torch::kFloat64);
// result.index_put_({ i, "..." }, torch::tensor(predict_sample(sample), torch::kFloat64)); // result.index_put_({ i, "..." }, torch::tensor(predict_sample(sample), torch::kFloat64));

View File

@ -20,13 +20,9 @@ namespace bayesnet {
vector<double> predict_sample(const torch::Tensor&); vector<double> predict_sample(const torch::Tensor&);
vector<double> exactInference(map<string, int>&); vector<double> exactInference(map<string, int>&);
double computeFactor(map<string, int>&); double computeFactor(map<string, int>&);
double mutual_info(torch::Tensor&, torch::Tensor&); void completeFit(const map<string, vector<int>>&);
double entropy(torch::Tensor&); void checkFitData(int n_features, int n_samples, int n_samples_y, const vector<string>& featureNames, const string& className, const map<string, vector<int>>&);
double conditionalEntropy(torch::Tensor&, torch::Tensor&); void setStates(const map<string, vector<int>>&);
double mutualInformation(torch::Tensor&, torch::Tensor&);
void completeFit();
void checkFitData(int n_features, int n_samples, int n_samples_y, const vector<string>& featureNames, const string& className);
void setStates();
public: public:
Network(); Network();
explicit Network(float, int); explicit Network(float, int);
@ -43,13 +39,11 @@ namespace bayesnet {
int getNumEdges() const; int getNumEdges() const;
int getClassNumStates() const; int getClassNumStates() const;
string getClassName() const; string getClassName() const;
void fit(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&); void fit(const vector<vector<int>>&, const vector<int>&, const vector<string>&, const string&, const map<string, vector<int>>&);
void fit(const torch::Tensor&, const torch::Tensor&, const vector<string>&, const string&); void fit(const torch::Tensor&, const torch::Tensor&, const vector<string>&, const string&, const map<string, vector<int>>&);
void fit(const torch::Tensor&, const vector<string>&, const string&); void fit(const torch::Tensor&, const vector<string>&, const string&, const map<string, vector<int>>&);
vector<int> predict(const vector<vector<int>>&); // Return mx1 vector of predictions vector<int> predict(const vector<vector<int>>&); // Return mx1 vector of predictions
torch::Tensor predict(const torch::Tensor&); // Return mx1 tensor of predictions torch::Tensor predict(const torch::Tensor&); // Return mx1 tensor of predictions
//Computes the conditional edge weight of variable index u and v conditioned on class_node
torch::Tensor conditionalEdgeWeight();
torch::Tensor predict_tensor(const torch::Tensor& samples, const bool proba); torch::Tensor predict_tensor(const torch::Tensor& samples, const bool proba);
vector<vector<double>> predict_proba(const vector<vector<int>>&); // Return mxn vector of probabilities vector<vector<double>> predict_proba(const vector<vector<int>>&); // Return mxn vector of probabilities
torch::Tensor predict_proba(const torch::Tensor&); // Return mxn tensor of probabilities torch::Tensor predict_proba(const torch::Tensor&); // Return mxn tensor of probabilities

View File

@ -64,7 +64,7 @@ namespace bayesnet {
//Update new states of the feature/node //Update new states of the feature/node
states[pFeatures[index]] = xStates; states[pFeatures[index]] = xStates;
} }
model.fit(pDataset, pFeatures, pClassName); model.fit(pDataset, pFeatures, pClassName, states);
} }
} }
void Proposal::fit_local_discretization(map<string, vector<int>>& states, torch::Tensor& y) void Proposal::fit_local_discretization(map<string, vector<int>>& states, torch::Tensor& y)

View File

@ -4,6 +4,7 @@ namespace platform {
string headerLine(const string& text) string headerLine(const string& text)
{ {
int n = MAXL - text.length() - 3; int n = MAXL - text.length() - 3;
n = n < 0 ? 0 : n;
return "* " + text + string(n, ' ') + "*\n"; return "* " + text + string(n, ' ') + "*\n";
} }
string Report::fromVector(const string& key) string Report::fromVector(const string& key)
@ -13,7 +14,7 @@ namespace platform {
for (auto& item : data[key]) { for (auto& item : data[key]) {
result += to_string(item) + ", "; result += to_string(item) + ", ";
} }
return "[" + result.substr(0, result.length() - 2) + "]"; return "[" + result.substr(0, result.size() - 2) + "]";
} }
string fVector(const json& data) string fVector(const json& data)
{ {
@ -21,7 +22,7 @@ namespace platform {
for (const auto& item : data) { for (const auto& item : data) {
result += to_string(item) + ", "; result += to_string(item) + ", ";
} }
return "[" + result.substr(0, result.length() - 2) + "]"; return "[" + result.substr(0, result.size() - 2) + "]";
} }
void Report::show() void Report::show()
{ {