Inference working
This commit is contained in:
parent
5db4d1189a
commit
ba08b8dd3d
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -86,7 +86,9 @@
|
|||||||
"*.tcc": "cpp",
|
"*.tcc": "cpp",
|
||||||
"functional": "cpp",
|
"functional": "cpp",
|
||||||
"iterator": "cpp",
|
"iterator": "cpp",
|
||||||
"memory_resource": "cpp"
|
"memory_resource": "cpp",
|
||||||
|
"format": "cpp",
|
||||||
|
"valarray": "cpp"
|
||||||
},
|
},
|
||||||
"cmake.configureOnOpen": false,
|
"cmake.configureOnOpen": false,
|
||||||
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
|
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
|
||||||
|
@ -2,4 +2,4 @@ class sepallength
|
|||||||
class sepalwidth
|
class sepalwidth
|
||||||
class petallength
|
class petallength
|
||||||
class petalwidth
|
class petalwidth
|
||||||
# petalwidth petallength
|
petalwidth petallength
|
@ -21,20 +21,47 @@
|
|||||||
// std::cout << t << std::endl;
|
// std::cout << t << std::endl;
|
||||||
// }
|
// }
|
||||||
#include <torch/torch.h>
|
#include <torch/torch.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
//torch::Tensor t = torch::rand({ 5, 4, 3 }); // 3D tensor for this example
|
|
||||||
//int i = 3, j = 1, k = 2; // Indices for the cell you want to update
|
//int i = 3, j = 1, k = 2; // Indices for the cell you want to update
|
||||||
// Print original tensor
|
// Print original tensor
|
||||||
torch::Tensor t = torch::tensor({ {1, 2, 3}, {4, 5, 6} }); // 3D tensor for this example
|
// torch::Tensor t = torch::tensor({ {1, 2, 3}, {4, 5, 6} }); // 3D tensor for this example
|
||||||
std::cout << t << std::endl;
|
auto variables = vector<string>{ "A", "B" };
|
||||||
std::cout << "sum(0)" << std::endl;
|
auto cardinalities = vector<int>{ 5, 4 };
|
||||||
std::cout << t.sum(0) << std::endl;
|
torch::Tensor values = torch::rand({ 5, 4 });
|
||||||
std::cout << "sum(1)" << std::endl;
|
auto candidate = "B";
|
||||||
std::cout << t.sum(1) << std::endl;
|
vector<string> newVariables;
|
||||||
std::cout << "Normalized" << std::endl;
|
vector<int> newCardinalities;
|
||||||
std::cout << t / t.sum(0) << std::endl;
|
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;
|
||||||
|
}
|
||||||
|
// std::cout << t << std::endl;
|
||||||
|
// std::cout << "sum(0)" << std::endl;
|
||||||
|
// std::cout << t.sum(0) << std::endl;
|
||||||
|
// std::cout << "sum(1)" << std::endl;
|
||||||
|
// std::cout << t.sum(1) << std::endl;
|
||||||
|
// std::cout << "Normalized" << std::endl;
|
||||||
|
// std::cout << t / t.sum(0) << std::endl;
|
||||||
|
|
||||||
// New value
|
// New value
|
||||||
// torch::Tensor new_val = torch::tensor(10.0f);
|
// torch::Tensor new_val = torch::tensor(10.0f);
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
add_library(BayesNet Network.cc Node.cc ExactInference.cc Factor.cc)
|
add_library(BayesNet Network.cc Node.cc ExactInference.cc)
|
||||||
target_link_libraries(BayesNet "${TORCH_LIBRARIES}")
|
target_link_libraries(BayesNet "${TORCH_LIBRARIES}")
|
@ -1,65 +1,30 @@
|
|||||||
#include "ExactInference.h"
|
#include "ExactInference.h"
|
||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
ExactInference::ExactInference(Network& net) : network(net), evidence(map<string, int>()), candidates(net.getFeatures()) {}
|
ExactInference::ExactInference(Network& net) : network(net) {}
|
||||||
void ExactInference::setEvidence(const map<string, int>& evidence)
|
double ExactInference::computeFactor(map<string, int>& completeEvidence)
|
||||||
{
|
|
||||||
this->evidence = evidence;
|
|
||||||
}
|
|
||||||
ExactInference::~ExactInference()
|
|
||||||
{
|
|
||||||
for (auto& factor : factors) {
|
|
||||||
delete factor;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void ExactInference::buildFactors()
|
|
||||||
{
|
{
|
||||||
|
double result = 1.0;
|
||||||
for (auto node : network.getNodes()) {
|
for (auto node : network.getNodes()) {
|
||||||
factors.push_back(node.second->toFactor());
|
result *= node.second->getFactorValue(completeEvidence);
|
||||||
}
|
|
||||||
}
|
|
||||||
string ExactInference::nextCandidate()
|
|
||||||
{
|
|
||||||
string result = "";
|
|
||||||
map<string, Node*> nodes = network.getNodes();
|
|
||||||
int minFill = INT_MAX;
|
|
||||||
for (auto candidate : candidates) {
|
|
||||||
unsigned fill = nodes[candidate]->minFill();
|
|
||||||
if (fill < minFill) {
|
|
||||||
minFill = fill;
|
|
||||||
result = candidate;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
vector<double> ExactInference::variableElimination()
|
vector<double> ExactInference::variableElimination(map<string, int>& evidence)
|
||||||
{
|
{
|
||||||
vector<double> result;
|
vector<double> result;
|
||||||
string candidate;
|
string candidate;
|
||||||
buildFactors();
|
int classNumStates = network.getClassNumStates();
|
||||||
// Eliminate evidence
|
for (int i = 0; i < classNumStates; ++i) {
|
||||||
while ((candidate = nextCandidate()) != "") {
|
result.push_back(1.0);
|
||||||
// Erase candidate from candidates (Erase–remove idiom)
|
auto complete_evidence = map<string, int>(evidence);
|
||||||
candidates.erase(remove(candidates.begin(), candidates.end(), candidate), candidates.end());
|
complete_evidence[network.getClassName()] = i;
|
||||||
// sum-product variable elimination algorithm as explained in the book probabilistic graphical models
|
result[i] = computeFactor(complete_evidence);
|
||||||
// 1. Multiply all factors containing the variable
|
}
|
||||||
vector<Factor*> factorsToMultiply;
|
// Normalize result
|
||||||
for (auto factor : factors) {
|
auto sum = accumulate(result.begin(), result.end(), 0.0);
|
||||||
if (factor->contains(candidate)) {
|
for (int i = 0; i < result.size(); ++i) {
|
||||||
factorsToMultiply.push_back(factor);
|
result[i] /= sum;
|
||||||
}
|
|
||||||
}
|
|
||||||
Factor* product = Factor::product(factorsToMultiply);
|
|
||||||
// 2. Sum out the variable
|
|
||||||
Factor* sum = product->sumOut(candidate);
|
|
||||||
// 3. Remove factors containing the variable
|
|
||||||
for (auto factor : factorsToMultiply) {
|
|
||||||
factors.erase(remove(factors.begin(), factors.end(), factor), factors.end());
|
|
||||||
delete factor;
|
|
||||||
}
|
|
||||||
// 4. Add the resulting factor to the list of factors
|
|
||||||
factors.push_back(sum);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#ifndef EXACTINFERENCE_H
|
#ifndef EXACTINFERENCE_H
|
||||||
#define EXACTINFERENCE_H
|
#define EXACTINFERENCE_H
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "Factor.h"
|
|
||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -12,16 +11,10 @@ namespace bayesnet {
|
|||||||
class ExactInference {
|
class ExactInference {
|
||||||
private:
|
private:
|
||||||
Network network;
|
Network network;
|
||||||
map<string, int> evidence;
|
double computeFactor(map<string, int>&);
|
||||||
vector<Factor*> factors;
|
|
||||||
vector<string> candidates; // variables to be removed
|
|
||||||
void buildFactors();
|
|
||||||
string nextCandidate(); // Return the next variable to eliminate using MinFill criterion
|
|
||||||
public:
|
public:
|
||||||
ExactInference(Network&);
|
ExactInference(Network&);
|
||||||
~ExactInference();
|
vector<double> variableElimination(map<string, int>&);
|
||||||
void setEvidence(const map<string, int>&);
|
|
||||||
vector<double> variableElimination();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
@ -1,87 +0,0 @@
|
|||||||
#include "Factor.h"
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
Factor::Factor(vector<string>& variables, vector<int>& cardinalities, torch::Tensor& values) : variables(variables), cardinalities(cardinalities), values(values) {}
|
|
||||||
Factor::~Factor() = default;
|
|
||||||
Factor::Factor(const Factor& other) : variables(other.variables), cardinalities(other.cardinalities), values(other.values) {}
|
|
||||||
Factor& Factor::operator=(const Factor& other)
|
|
||||||
{
|
|
||||||
if (this != &other) {
|
|
||||||
variables = other.variables;
|
|
||||||
cardinalities = other.cardinalities;
|
|
||||||
values = other.values;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
void Factor::setVariables(vector<string>& variables)
|
|
||||||
{
|
|
||||||
this->variables = variables;
|
|
||||||
}
|
|
||||||
void Factor::setCardinalities(vector<int>& cardinalities)
|
|
||||||
{
|
|
||||||
this->cardinalities = cardinalities;
|
|
||||||
}
|
|
||||||
void Factor::setValues(torch::Tensor& values)
|
|
||||||
{
|
|
||||||
this->values = values;
|
|
||||||
}
|
|
||||||
vector<string>& Factor::getVariables()
|
|
||||||
{
|
|
||||||
return variables;
|
|
||||||
}
|
|
||||||
vector<int>& Factor::getCardinalities()
|
|
||||||
{
|
|
||||||
return cardinalities;
|
|
||||||
}
|
|
||||||
torch::Tensor& Factor::getValues()
|
|
||||||
{
|
|
||||||
return values;
|
|
||||||
}
|
|
||||||
bool Factor::contains(string& variable)
|
|
||||||
{
|
|
||||||
for (int i = 0; i < variables.size(); i++) {
|
|
||||||
if (variables[i] == variable) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Factor* Factor::sumOut(string& candidate)
|
|
||||||
{
|
|
||||||
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(0);
|
|
||||||
return new Factor(newVariables, newCardinalities, newValues);
|
|
||||||
}
|
|
||||||
Factor* Factor::product(vector<Factor*>& factors)
|
|
||||||
{
|
|
||||||
vector<string> newVariables;
|
|
||||||
vector<int> newCardinalities;
|
|
||||||
for (auto factor : factors) {
|
|
||||||
vector<string> variables = factor->getVariables();
|
|
||||||
for (auto idx = 0; idx < variables.size(); ++idx) {
|
|
||||||
string variable = variables[idx];
|
|
||||||
if (find(newVariables.begin(), newVariables.end(), variable) == newVariables.end()) {
|
|
||||||
newVariables.push_back(variable);
|
|
||||||
newCardinalities.push_back(factor->getCardinalities()[idx]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
torch::Tensor newValues = factors[0]->getValues();
|
|
||||||
for (int i = 1; i < factors.size(); i++) {
|
|
||||||
newValues = newValues.matmul(factors[i]->getValues());
|
|
||||||
}
|
|
||||||
return new Factor(newVariables, newCardinalities, newValues);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
31
src/Factor.h
31
src/Factor.h
@ -1,31 +0,0 @@
|
|||||||
#ifndef FACTOR_H
|
|
||||||
#define FACTOR_H
|
|
||||||
#include <torch/torch.h>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
namespace bayesnet {
|
|
||||||
class Factor {
|
|
||||||
private:
|
|
||||||
vector<string> variables;
|
|
||||||
vector<int> cardinalities;
|
|
||||||
torch::Tensor values;
|
|
||||||
public:
|
|
||||||
Factor(vector<string>&, vector<int>&, torch::Tensor&);
|
|
||||||
~Factor();
|
|
||||||
Factor(const Factor&);
|
|
||||||
Factor& operator=(const Factor&);
|
|
||||||
void setVariables(vector<string>&);
|
|
||||||
void setCardinalities(vector<int>&);
|
|
||||||
void setValues(torch::Tensor&);
|
|
||||||
vector<string>& getVariables();
|
|
||||||
vector<int>& getCardinalities();
|
|
||||||
bool contains(string&);
|
|
||||||
torch::Tensor& getValues();
|
|
||||||
static Factor* product(vector<Factor*>&);
|
|
||||||
Factor* sumOut(string&);
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif
|
|
@ -1,9 +1,9 @@
|
|||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "ExactInference.h"
|
#include "ExactInference.h"
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
Network::Network() : laplaceSmoothing(1), root(nullptr), features(vector<string>()), className("") {}
|
Network::Network() : laplaceSmoothing(1), root(nullptr), features(vector<string>()), className(""), classNumStates(0) {}
|
||||||
Network::Network(int smoothing) : laplaceSmoothing(smoothing), root(nullptr), features(vector<string>()), className("") {}
|
Network::Network(int smoothing) : laplaceSmoothing(smoothing), root(nullptr), features(vector<string>()), className(""), classNumStates(0) {}
|
||||||
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), root(other.root), features(other.features), className(other.className)
|
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), root(other.root), features(other.features), className(other.className), classNumStates(other.getClassNumStates())
|
||||||
{
|
{
|
||||||
for (auto& pair : other.nodes) {
|
for (auto& pair : other.nodes) {
|
||||||
nodes[pair.first] = new Node(*pair.second);
|
nodes[pair.first] = new Node(*pair.second);
|
||||||
@ -31,6 +31,14 @@ namespace bayesnet {
|
|||||||
{
|
{
|
||||||
return features;
|
return features;
|
||||||
}
|
}
|
||||||
|
int Network::getClassNumStates()
|
||||||
|
{
|
||||||
|
return classNumStates;
|
||||||
|
}
|
||||||
|
string Network::getClassName()
|
||||||
|
{
|
||||||
|
return className;
|
||||||
|
}
|
||||||
void Network::setRoot(string name)
|
void Network::setRoot(string name)
|
||||||
{
|
{
|
||||||
if (nodes.find(name) == nodes.end()) {
|
if (nodes.find(name) == nodes.end()) {
|
||||||
@ -93,6 +101,7 @@ namespace bayesnet {
|
|||||||
this->dataset[featureNames[i]] = dataset[i];
|
this->dataset[featureNames[i]] = dataset[i];
|
||||||
}
|
}
|
||||||
this->dataset[className] = labels;
|
this->dataset[className] = labels;
|
||||||
|
this->classNumStates = *max_element(labels.begin(), labels.end()) + 1;
|
||||||
estimateParameters();
|
estimateParameters();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,29 +109,7 @@ namespace bayesnet {
|
|||||||
{
|
{
|
||||||
auto dimensions = vector<int64_t>();
|
auto dimensions = vector<int64_t>();
|
||||||
for (auto [name, node] : nodes) {
|
for (auto [name, node] : nodes) {
|
||||||
// Get dimensions of the CPT
|
node->computeCPT(dataset, laplaceSmoothing);
|
||||||
dimensions.clear();
|
|
||||||
dimensions.push_back(node->getNumStates());
|
|
||||||
for (auto father : node->getParents()) {
|
|
||||||
dimensions.push_back(father->getNumStates());
|
|
||||||
}
|
|
||||||
auto length = dimensions.size();
|
|
||||||
// Create a tensor of zeros with the dimensions of the CPT
|
|
||||||
torch::Tensor cpt = torch::zeros(dimensions, torch::kFloat) + laplaceSmoothing;
|
|
||||||
// Fill table with counts
|
|
||||||
for (int n_sample = 0; n_sample < dataset[name].size(); ++n_sample) {
|
|
||||||
torch::List<c10::optional<torch::Tensor>> coordinates;
|
|
||||||
coordinates.push_back(torch::tensor(dataset[name][n_sample]));
|
|
||||||
for (auto father : node->getParents()) {
|
|
||||||
coordinates.push_back(torch::tensor(dataset[father->getName()][n_sample]));
|
|
||||||
}
|
|
||||||
// Increment the count of the corresponding coordinate
|
|
||||||
cpt.index_put_({ coordinates }, cpt.index({ coordinates }) + 1);
|
|
||||||
}
|
|
||||||
// Normalize the counts
|
|
||||||
cpt = cpt / cpt.sum(0);
|
|
||||||
// store thre resulting cpt in the node
|
|
||||||
node->setCPT(cpt);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,14 +162,8 @@ namespace bayesnet {
|
|||||||
for (int i = 0; i < sample.size(); ++i) {
|
for (int i = 0; i < sample.size(); ++i) {
|
||||||
evidence[features[i]] = sample[i];
|
evidence[features[i]] = sample[i];
|
||||||
}
|
}
|
||||||
inference.setEvidence(evidence);
|
vector<double> classProbabilities = inference.variableElimination(evidence);
|
||||||
vector<double> classProbabilities = inference.variableElimination();
|
|
||||||
|
|
||||||
// Normalize the probabilities to sum to 1
|
|
||||||
double sum = accumulate(classProbabilities.begin(), classProbabilities.end(), 0.0);
|
|
||||||
for (double& prob : classProbabilities) {
|
|
||||||
prob /= sum;
|
|
||||||
}
|
|
||||||
// Find the class with the maximum posterior probability
|
// Find the class with the maximum posterior probability
|
||||||
auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
|
auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
|
||||||
int predictedClass = distance(classProbabilities.begin(), maxElem);
|
int predictedClass = distance(classProbabilities.begin(), maxElem);
|
||||||
|
@ -11,6 +11,7 @@ namespace bayesnet {
|
|||||||
map<string, Node*> nodes;
|
map<string, Node*> nodes;
|
||||||
map<string, vector<int>> dataset;
|
map<string, vector<int>> dataset;
|
||||||
Node* root;
|
Node* root;
|
||||||
|
int classNumStates;
|
||||||
vector<string> features;
|
vector<string> features;
|
||||||
string className;
|
string className;
|
||||||
int laplaceSmoothing;
|
int laplaceSmoothing;
|
||||||
@ -25,6 +26,8 @@ namespace bayesnet {
|
|||||||
void addEdge(const string, const string);
|
void addEdge(const string, const string);
|
||||||
map<string, Node*>& getNodes();
|
map<string, Node*>& getNodes();
|
||||||
vector<string> getFeatures();
|
vector<string> getFeatures();
|
||||||
|
int getClassNumStates();
|
||||||
|
string getClassName();
|
||||||
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&);
|
||||||
void estimateParameters();
|
void estimateParameters();
|
||||||
void setRoot(string);
|
void setRoot(string);
|
||||||
|
49
src/Node.cc
49
src/Node.cc
@ -1,10 +1,9 @@
|
|||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
int Node::next_id = 0;
|
|
||||||
|
|
||||||
Node::Node(const std::string& name, int numStates)
|
Node::Node(const std::string& name, int numStates)
|
||||||
: id(next_id++), name(name), numStates(numStates), cpt(torch::Tensor()), parents(vector<Node*>()), children(vector<Node*>())
|
: name(name), numStates(numStates), cpTable(torch::Tensor()), parents(vector<Node*>()), children(vector<Node*>())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,11 +46,7 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
torch::Tensor& Node::getCPT()
|
torch::Tensor& Node::getCPT()
|
||||||
{
|
{
|
||||||
return cpt;
|
return cpTable;
|
||||||
}
|
|
||||||
void Node::setCPT(const torch::Tensor& cpt)
|
|
||||||
{
|
|
||||||
this->cpt = cpt;
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
The MinFill criterion is a heuristic for variable elimination.
|
The MinFill criterion is a heuristic for variable elimination.
|
||||||
@ -83,17 +78,37 @@ namespace bayesnet {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
Factor* Node::toFactor()
|
void Node::computeCPT(map<string, vector<int>>& dataset, const int laplaceSmoothing)
|
||||||
{
|
{
|
||||||
vector<string> variables;
|
// Get dimensions of the CPT
|
||||||
vector<int> cardinalities;
|
dimensions.push_back(numStates);
|
||||||
variables.push_back(name);
|
for (auto father : getParents()) {
|
||||||
cardinalities.push_back(numStates);
|
dimensions.push_back(father->getNumStates());
|
||||||
for (auto parent : parents) {
|
|
||||||
variables.push_back(parent->getName());
|
|
||||||
cardinalities.push_back(parent->getNumStates());
|
|
||||||
}
|
}
|
||||||
return new Factor(variables, cardinalities, cpt);
|
auto length = dimensions.size();
|
||||||
|
// Create a tensor of zeros with the dimensions of the CPT
|
||||||
|
cpTable = torch::zeros(dimensions, torch::kFloat) + laplaceSmoothing;
|
||||||
|
// Fill table with counts
|
||||||
|
for (int n_sample = 0; n_sample < dataset[name].size(); ++n_sample) {
|
||||||
|
torch::List<c10::optional<torch::Tensor>> coordinates;
|
||||||
|
coordinates.push_back(torch::tensor(dataset[name][n_sample]));
|
||||||
|
for (auto father : getParents()) {
|
||||||
|
coordinates.push_back(torch::tensor(dataset[father->getName()][n_sample]));
|
||||||
|
}
|
||||||
|
// Increment the count of the corresponding coordinate
|
||||||
|
cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + 1);
|
||||||
|
}
|
||||||
|
// Normalize the counts
|
||||||
|
cpTable = cpTable / cpTable.sum(0);
|
||||||
|
}
|
||||||
|
float Node::getFactorValue(map<string, int>& evidence)
|
||||||
|
{
|
||||||
|
torch::List<c10::optional<torch::Tensor>> coordinates;
|
||||||
|
// following predetermined order of indices in the cpTable (see Node.h)
|
||||||
|
coordinates.push_back(torch::tensor(evidence[name]));
|
||||||
|
for (auto parent : getParents()) {
|
||||||
|
coordinates.push_back(torch::tensor(evidence[parent->getName()]));
|
||||||
|
}
|
||||||
|
return cpTable.index({ coordinates }).item<float>();
|
||||||
}
|
}
|
||||||
}
|
}
|
14
src/Node.h
14
src/Node.h
@ -1,21 +1,18 @@
|
|||||||
#ifndef NODE_H
|
#ifndef NODE_H
|
||||||
#define NODE_H
|
#define NODE_H
|
||||||
#include <torch/torch.h>
|
#include <torch/torch.h>
|
||||||
#include "Factor.h"
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
class Node {
|
class Node {
|
||||||
private:
|
private:
|
||||||
static int next_id;
|
|
||||||
const int id;
|
|
||||||
string name;
|
string name;
|
||||||
vector<Node*> parents;
|
vector<Node*> parents;
|
||||||
vector<Node*> children;
|
vector<Node*> children;
|
||||||
torch::Tensor cpTable;
|
int numStates; // number of states of the variable
|
||||||
int numStates;
|
torch::Tensor cpTable; // Order of indices is 0-> node variable, 1-> 1st parent, 2-> 2nd parent, ...
|
||||||
torch::Tensor cpt;
|
vector<int64_t> dimensions; // dimensions of the cpTable
|
||||||
vector<string> combinations(const set<string>&);
|
vector<string> combinations(const set<string>&);
|
||||||
public:
|
public:
|
||||||
Node(const std::string&, int);
|
Node(const std::string&, int);
|
||||||
@ -27,12 +24,11 @@ namespace bayesnet {
|
|||||||
vector<Node*>& getParents();
|
vector<Node*>& getParents();
|
||||||
vector<Node*>& getChildren();
|
vector<Node*>& getChildren();
|
||||||
torch::Tensor& getCPT();
|
torch::Tensor& getCPT();
|
||||||
void setCPT(const torch::Tensor&);
|
void computeCPT(map<string, vector<int>>&, const int);
|
||||||
int getNumStates() const;
|
int getNumStates() const;
|
||||||
void setNumStates(int);
|
void setNumStates(int);
|
||||||
unsigned minFill();
|
unsigned minFill();
|
||||||
int getId() const { return id; }
|
float getFactorValue(map<string, int>&);
|
||||||
Factor* toFactor();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
Loading…
Reference in New Issue
Block a user