Add entropy, conditionalEntropy, mutualInformation and conditionalEdgeWeight methods
This commit is contained in:
@@ -138,6 +138,7 @@ pair<string, string> get_options(int argc, char** argv)
|
||||
{
|
||||
map<string, bool> datasets = {
|
||||
{"diabetes", true},
|
||||
{"ecoli", true},
|
||||
{"glass", true},
|
||||
{"iris", true},
|
||||
{"kdd_JapaneseVowels", false},
|
||||
@@ -229,5 +230,6 @@ int main(int argc, char** argv)
|
||||
cout << "BayesNet version: " << network.version() << endl;
|
||||
unsigned int nthreads = std::thread::hardware_concurrency();
|
||||
cout << "Computer has " << nthreads << " cores." << endl;
|
||||
cout << "conditionalEdgeWeight " << endl << network.conditionalEdgeWeight() << endl;
|
||||
return 0;
|
||||
}
|
166
sample/test.cc
166
sample/test.cc
@@ -25,36 +25,154 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
double entropy(torch::Tensor feature)
|
||||
{
|
||||
torch::Tensor counts = feature.bincount();
|
||||
int totalWeight = counts.sum().item<int>();
|
||||
torch::Tensor probs = counts.to(torch::kFloat) / totalWeight;
|
||||
torch::Tensor logProbs = torch::log2(probs);
|
||||
torch::Tensor entropy = -probs * logProbs;
|
||||
return entropy.sum().item<double>();
|
||||
}
|
||||
// H(Y|X) = sum_{x in X} p(x) H(Y|X=x)
|
||||
double conditionalEntropy(torch::Tensor firstFeature, torch::Tensor secondFeature)
|
||||
{
|
||||
int numSamples = firstFeature.sizes()[0];
|
||||
torch::Tensor featureCounts = secondFeature.bincount();
|
||||
unordered_map<int, unordered_map<int, double>> jointCounts;
|
||||
double totalWeight = 0;
|
||||
for (auto i = 0; i < numSamples; i++) {
|
||||
jointCounts[secondFeature[i].item<int>()][firstFeature[i].item<int>()] += 1;
|
||||
totalWeight += 1;
|
||||
}
|
||||
if (totalWeight == 0)
|
||||
throw invalid_argument("Total weight should not be zero");
|
||||
double entropy = 0;
|
||||
for (int value = 0; value < featureCounts.sizes()[0]; ++value) {
|
||||
double p_f = featureCounts[value].item<double>() / totalWeight;
|
||||
double entropy_f = 0;
|
||||
for (auto& [label, jointCount] : jointCounts[value]) {
|
||||
double p_l_f = jointCount / featureCounts[value].item<double>();
|
||||
if (p_l_f > 0) {
|
||||
entropy_f -= p_l_f * log2(p_l_f);
|
||||
} else {
|
||||
entropy_f = 0;
|
||||
}
|
||||
}
|
||||
entropy += p_f * entropy_f;
|
||||
}
|
||||
return entropy;
|
||||
}
|
||||
|
||||
// I(X;Y) = H(Y) - H(Y|X)
|
||||
double mutualInformation(torch::Tensor firstFeature, torch::Tensor secondFeature)
|
||||
{
|
||||
return entropy(firstFeature) - conditionalEntropy(firstFeature, secondFeature);
|
||||
}
|
||||
double entropy2(torch::Tensor feature)
|
||||
{
|
||||
return torch::special::entr(feature).sum().item<double>();
|
||||
}
|
||||
int main()
|
||||
{
|
||||
//int i = 3, j = 1, k = 2; // Indices for the cell you want to update
|
||||
// Print original tensor
|
||||
// torch::Tensor t = torch::tensor({ {1, 2, 3}, {4, 5, 6} }); // 3D tensor for this example
|
||||
auto variables = vector<string>{ "A", "B" };
|
||||
auto cardinalities = vector<int>{ 5, 4 };
|
||||
torch::Tensor values = torch::rand({ 5, 4 });
|
||||
auto candidate = "B";
|
||||
vector<string> newVariables;
|
||||
vector<int> newCardinalities;
|
||||
for (int i = 0; i < variables.size(); i++) {
|
||||
if (variables[i] != candidate) {
|
||||
newVariables.push_back(variables[i]);
|
||||
newCardinalities.push_back(cardinalities[i]);
|
||||
}
|
||||
}
|
||||
torch::Tensor newValues = values.sum(1);
|
||||
cout << "original values" << endl;
|
||||
cout << values << endl;
|
||||
cout << "newValues" << endl;
|
||||
cout << newValues << endl;
|
||||
cout << "newVariables" << endl;
|
||||
for (auto& variable : newVariables) {
|
||||
cout << variable << endl;
|
||||
}
|
||||
cout << "newCardinalities" << endl;
|
||||
for (auto& cardinality : newCardinalities) {
|
||||
cout << cardinality << endl;
|
||||
// auto variables = vector<string>{ "A", "B" };
|
||||
// auto cardinalities = vector<int>{ 5, 4 };
|
||||
// torch::Tensor values = torch::rand({ 5, 4 });
|
||||
// auto candidate = "B";
|
||||
// vector<string> newVariables;
|
||||
// vector<int> newCardinalities;
|
||||
// for (int i = 0; i < variables.size(); i++) {
|
||||
// if (variables[i] != candidate) {
|
||||
// newVariables.push_back(variables[i]);
|
||||
// newCardinalities.push_back(cardinalities[i]);
|
||||
// }
|
||||
// }
|
||||
// torch::Tensor newValues = values.sum(1);
|
||||
// cout << "original values" << endl;
|
||||
// cout << values << endl;
|
||||
// cout << "newValues" << endl;
|
||||
// cout << newValues << endl;
|
||||
// cout << "newVariables" << endl;
|
||||
// for (auto& variable : newVariables) {
|
||||
// cout << variable << endl;
|
||||
// }
|
||||
// cout << "newCardinalities" << endl;
|
||||
// for (auto& cardinality : newCardinalities) {
|
||||
// cout << cardinality << endl;
|
||||
// }
|
||||
// auto row2 = values.index({ torch::tensor(1) }); //
|
||||
// cout << "row2" << endl;
|
||||
// cout << row2 << endl;
|
||||
// auto col2 = values.index({ "...", 1 });
|
||||
// cout << "col2" << endl;
|
||||
// cout << col2 << endl;
|
||||
// auto col_last = values.index({ "...", -1 });
|
||||
// cout << "col_last" << endl;
|
||||
// cout << col_last << endl;
|
||||
// values.index_put_({ "...", -1 }, torch::tensor({ 1,2,3,4,5 }));
|
||||
// cout << "col_last" << endl;
|
||||
// cout << col_last << endl;
|
||||
// auto slice2 = values.index({ torch::indexing::Slice(1, torch::indexing::None) });
|
||||
// cout << "slice2" << endl;
|
||||
// cout << slice2 << endl;
|
||||
// auto mask = values.index({ "...", -1 }) % 2 == 0;
|
||||
// auto filter = values.index({ mask, 2 }); // Filter values
|
||||
// cout << "filter" << endl;
|
||||
// cout << filter << endl;
|
||||
// torch::Tensor dataset = torch::tensor({ {1,0,0,1},{1,1,1,2},{0,0,0,1},{1,0,2,0},{0,0,3,0} });
|
||||
// cout << "dataset" << endl;
|
||||
// cout << dataset << endl;
|
||||
// cout << "entropy(dataset.indices('...', 2))" << endl;
|
||||
// cout << dataset.index({ "...", 2 }) << endl;
|
||||
// cout << "*********************************" << endl;
|
||||
// for (int i = 0; i < 4; i++) {
|
||||
// cout << "datset(" << i << ")" << endl;
|
||||
// cout << dataset.index({ "...", i }) << endl;
|
||||
// cout << "entropy(" << i << ")" << endl;
|
||||
// cout << entropy(dataset.index({ "...", i })) << endl;
|
||||
// }
|
||||
// cout << "......................................" << endl;
|
||||
// //cout << entropy2(dataset.index({ "...", 2 }));
|
||||
// cout << "conditional entropy 0 2" << endl;
|
||||
// cout << conditionalEntropy(dataset.index({ "...", 0 }), dataset.index({ "...", 2 })) << endl;
|
||||
// cout << "mutualInformation(dataset.index({ '...', 0 }), dataset.index({ '...', 2 }))" << endl;
|
||||
// cout << mutualInformation(dataset.index({ "...", 0 }), dataset.index({ "...", 2 })) << endl;
|
||||
// auto test = torch::tensor({ .1, .2, .3 }, torch::kFloat);
|
||||
// auto result = torch::zeros({ 3, 3 }, torch::kFloat);
|
||||
// result.index_put_({ indices }, test);
|
||||
// cout << "indices" << endl;
|
||||
// cout << indices << endl;
|
||||
// cout << "result" << endl;
|
||||
// cout << result << endl;
|
||||
// cout << "Test" << endl;
|
||||
// cout << torch::triu(test.reshape(3, 3), torch::kFloat)) << endl;
|
||||
|
||||
|
||||
// Create a 3x3 tensor with zeros
|
||||
torch::Tensor tensor_3x3 = torch::zeros({ 3, 3 }, torch::kFloat);
|
||||
|
||||
// Create a 1D tensor with the three elements you want to set in the upper corner
|
||||
torch::Tensor tensor_1d = torch::tensor({ 10, 11, 12 }, torch::kFloat);
|
||||
|
||||
// Set the upper corner of the 3x3 tensor
|
||||
auto indices = torch::triu_indices(3, 3, 1);
|
||||
for (auto i = 0; i < tensor_1d.sizes()[0]; ++i) {
|
||||
auto x = indices[0][i];
|
||||
auto y = indices[1][i];
|
||||
tensor_3x3[x][y] = tensor_1d[i];
|
||||
tensor_3x3[y][x] = tensor_1d[i];
|
||||
}
|
||||
// Print the resulting 3x3 tensor
|
||||
std::cout << tensor_3x3 << std::endl;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// std::cout << t << std::endl;
|
||||
// std::cout << "sum(0)" << std::endl;
|
||||
// std::cout << t.sum(0) << std::endl;
|
||||
|
Reference in New Issue
Block a user