Refactor constructor

This commit is contained in:
Ricardo Montañana Gómez 2023-07-12 03:07:10 +02:00
parent a60b06e2f2
commit 5793c31bc4
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
3 changed files with 16 additions and 14 deletions

View File

@ -231,6 +231,7 @@ int main(int argc, char** argv)
cout << "BayesNet version: " << network.version() << endl; cout << "BayesNet version: " << network.version() << endl;
unsigned int nthreads = std::thread::hardware_concurrency(); unsigned int nthreads = std::thread::hardware_concurrency();
cout << "Computer has " << nthreads << " cores." << endl; cout << "Computer has " << nthreads << " cores." << endl;
cout << "****************** First ******************" << endl;
auto metrics = bayesnet::Metrics(network.getSamples(), features, className, network.getClassNumStates()); auto metrics = bayesnet::Metrics(network.getSamples(), features, className, network.getClassNumStates());
cout << "conditionalEdgeWeight " << endl; cout << "conditionalEdgeWeight " << endl;
auto conditional = metrics.conditionalEdgeWeights(); auto conditional = metrics.conditionalEdgeWeights();
@ -238,5 +239,13 @@ int main(int argc, char** argv)
long m = features.size() + 1; long m = features.size() + 1;
auto matrix = torch::from_blob(conditional.data(), { m, m }); auto matrix = torch::from_blob(conditional.data(), { m, m });
cout << matrix << endl; cout << matrix << endl;
cout << "****************** Second ******************" << endl;
auto metrics2 = bayesnet::Metrics(Xd, y, features, className, network.getClassNumStates());
cout << "conditionalEdgeWeight " << endl;
auto conditional2 = metrics2.conditionalEdgeWeights();
cout << conditional2 << endl;
long m2 = features.size() + 1;
auto matrix2 = torch::from_blob(conditional2.data(), { m, m });
cout << matrix2 << endl;
return 0; return 0;
} }

View File

@ -1,30 +1,23 @@
#include "Metrics.hpp" #include "Metrics.hpp"
using namespace std; using namespace std;
namespace bayesnet { namespace bayesnet {
vector<int> linearize(const vector<vector<int>>& vec_vec)
{
vector<int> vec;
for (const auto& v : vec_vec) {
for (auto d : v) {
vec.push_back(d);
}
}
return vec;
}
Metrics::Metrics(torch::Tensor& samples, vector<string>& features, string& className, int classNumStates) Metrics::Metrics(torch::Tensor& samples, vector<string>& features, string& className, int classNumStates)
: samples(samples) : samples(samples)
, features(features) , features(features)
, className(className) , className(className)
, classNumStates(classNumStates) , classNumStates(classNumStates)
{ {
} }
Metrics::Metrics(vector<vector<int>>& vsamples, int m, int n, vector<string>& features, string& className, int classNumStates) Metrics::Metrics(vector<vector<int>>& vsamples, vector<int>& labels, vector<string>& features, string& className, int classNumStates)
: features(features) : features(features)
, className(className) , className(className)
, classNumStates(classNumStates) , classNumStates(classNumStates)
{ {
samples = torch::from_blob(linearize(vsamples).data(), { m, n }); samples = torch::zeros({ static_cast<int64_t>(vsamples[0].size()), static_cast<int64_t>(vsamples.size() + 1) }, torch::kInt64);
for (int i = 0; i < vsamples.size(); ++i) {
samples.index_put_({ "...", i }, torch::tensor(vsamples[i], torch::kInt64));
}
samples.index_put_({ "...", -1 }, torch::tensor(labels, torch::kInt64));
} }
vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source) vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source)
{ {

View File

@ -17,7 +17,7 @@ namespace bayesnet {
double mutualInformation(torch::Tensor&, torch::Tensor&); double mutualInformation(torch::Tensor&, torch::Tensor&);
public: public:
Metrics(torch::Tensor&, vector<string>&, string&, int); Metrics(torch::Tensor&, vector<string>&, string&, int);
Metrics(vector<vector<int>>&, int, int, vector<string>&, string&, int); Metrics(vector<vector<int>>&, vector<int>&, vector<string>&, string&, int);
vector<float> conditionalEdgeWeights(); vector<float> conditionalEdgeWeights();
}; };
} }