Make fit build the network

This commit is contained in:
2023-06-30 02:46:06 +02:00
parent 31c22898de
commit 0a31aa2ff1
13 changed files with 580 additions and 82 deletions

48
main.cc
View File

@@ -3,40 +3,44 @@
#include <torch/torch.h>
#include "ArffFiles.h"
#include "Network.h"
#include "CPPFImdlp.h"
using namespace std;
vector<mdlp::labels_t> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y)
{
vector<mdlp::labels_t>Xd;
auto fimdlp = mdlp::CPPFImdlp();
for (int i = 0; i < X.size(); i++) {
fimdlp.fit(X[i], y);
Xd.push_back(fimdlp.transform(X[i]));
}
return Xd;
}
int main()
{
auto handler = ArffFiles();
handler.load("iris.arff");
auto X = handler.getX();
auto y = handler.getY();
// Get Dataset X, y
vector<mdlp::samples_t>& X = handler.getX();
mdlp::labels_t& y = handler.getY();
// Get className & Features
auto className = handler.getClassName();
vector<pair<string, string>> edges = { {className, "sepallength"}, {className, "sepalwidth"}, {className, "petallength"}, {className, "petalwidth"} };
auto network = bayesnet::Network();
// Add nodes to the network
vector<string> features;
for (auto feature : handler.getAttributes()) {
cout << "Adding feature: " << feature.first << endl;
network.addNode(feature.first, 7);
}
network.addNode(className, 3);
for (auto item : edges) {
network.addEdge(item.first, item.second);
features.push_back(feature.first);
}
// Discretize Dataset
vector<mdlp::labels_t> Xd = discretize(X, y);;
// Build Network
auto network = bayesnet::Network();
network.fit(Xd, y, features, className);
cout << "Hello, Bayesian Networks!" << endl;
torch::Tensor tensor = torch::eye(3);
cout << "Now I'll add a cycle" << endl;
try {
network.addEdge("petallength", className);
}
catch (invalid_argument& e) {
cout << e.what() << endl;
}
cout << tensor << std::endl;
cout << "Nodes:" << endl;
for (auto [name, item] : network.getNodes()) {
cout << "*" << item->getName() << endl;
cout << "*" << item->getName() << " -> " << item->getNumStates() << endl;
cout << "-Parents:" << endl;
for (auto parent : item->getParents()) {
cout << " " << parent->getName() << endl;
@@ -49,6 +53,6 @@ int main()
cout << "Root: " << network.getRoot()->getName() << endl;
network.setRoot(className);
cout << "Now Root should be class: " << network.getRoot()->getName() << endl;
cout << "PyTorch version: " << TORCH_VERSION << endl;
return 0;
}