refactor to use in python

This commit is contained in:
Ricardo Montañana Gómez 2023-07-12 01:05:24 +02:00
parent d1eaab6408
commit a60b06e2f2
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
4 changed files with 33 additions and 5 deletions

View File

@ -232,6 +232,11 @@ int main(int argc, char** argv)
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;
auto metrics = bayesnet::Metrics(network.getSamples(), features, className, network.getClassNumStates()); auto metrics = bayesnet::Metrics(network.getSamples(), features, className, network.getClassNumStates());
cout << "conditionalEdgeWeight " << endl << metrics.conditionalEdgeWeight() << endl; cout << "conditionalEdgeWeight " << endl;
auto conditional = metrics.conditionalEdgeWeights();
cout << conditional << endl;
long m = features.size() + 1;
auto matrix = torch::from_blob(conditional.data(), { m, m });
cout << matrix << endl;
return 0; return 0;
} }

View File

@ -167,6 +167,9 @@ int main()
} }
// Print the resulting 3x3 tensor // Print the resulting 3x3 tensor
std::cout << tensor_3x3 << std::endl; std::cout << tensor_3x3 << std::endl;
vector<int> v = { 1,2,3,4,5 };
torch::Tensor t = torch::tensor(v);
cout << t << endl;

View File

@ -1,12 +1,30 @@
#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)
: features(features)
, className(className)
, classNumStates(classNumStates)
{
samples = torch::from_blob(linearize(vsamples).data(), { m, n });
} }
vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source) vector<pair<string, string>> Metrics::doCombinations(const vector<string>& source)
{ {
@ -19,7 +37,7 @@ namespace bayesnet {
} }
return result; return result;
} }
torch::Tensor Metrics::conditionalEdgeWeight() vector<float> Metrics::conditionalEdgeWeights()
{ {
auto result = vector<double>(); auto result = vector<double>();
auto source = vector<string>(features); auto source = vector<string>(features);
@ -54,7 +72,8 @@ namespace bayesnet {
matrix[x][y] = result[i]; matrix[x][y] = result[i];
matrix[y][x] = result[i]; matrix[y][x] = result[i];
} }
return matrix; std::vector<float> v(matrix.data_ptr<float>(), matrix.data_ptr<float>() + matrix.numel());
return v;
} }
double Metrics::entropy(torch::Tensor& feature) double Metrics::entropy(torch::Tensor& feature)
{ {

View File

@ -7,7 +7,7 @@ using namespace std;
namespace bayesnet { namespace bayesnet {
class Metrics { class Metrics {
private: private:
torch::Tensor& samples; torch::Tensor samples;
vector<string>& features; vector<string>& features;
string& className; string& className;
int classNumStates; int classNumStates;
@ -17,7 +17,8 @@ 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);
torch::Tensor conditionalEdgeWeight(); Metrics(vector<vector<int>>&, int, int, vector<string>&, string&, int);
vector<float> conditionalEdgeWeights();
}; };
} }
#endif #endif