Begin Folding
This commit is contained in:
parent
d8218f9713
commit
a2622a4fb6
@ -2,5 +2,6 @@ include_directories(${BayesNet_SOURCE_DIR}/src/BayesNet)
|
|||||||
include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
||||||
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
|
||||||
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
|
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
|
||||||
add_executable(main Experiment.cc platformUtils.cc)
|
include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include)
|
||||||
|
add_executable(main Experiment.cc Folding.cc platformUtils.cc)
|
||||||
target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
|
target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
|
@ -2,7 +2,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <torch/torch.h>
|
#include <torch/torch.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <getopt.h>
|
#include <argparse/argparse.hpp>
|
||||||
#include "ArffFiles.h"
|
#include "ArffFiles.h"
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "BayesMetrics.h"
|
#include "BayesMetrics.h"
|
||||||
@ -11,71 +11,11 @@
|
|||||||
#include "SPODE.h"
|
#include "SPODE.h"
|
||||||
#include "AODE.h"
|
#include "AODE.h"
|
||||||
#include "TAN.h"
|
#include "TAN.h"
|
||||||
#include "platformUtils.h"
|
|
||||||
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
/* print a description of all supported options */
|
const string PATH = "../../data/";
|
||||||
void usage(const char* path)
|
|
||||||
{
|
|
||||||
/* take only the last portion of the path */
|
|
||||||
const char* basename = strrchr(path, '/');
|
|
||||||
basename = basename ? basename + 1 : path;
|
|
||||||
|
|
||||||
cout << "usage: " << basename << "[OPTION]" << endl;
|
|
||||||
cout << " -h, --help\t\t Print this help and exit." << endl;
|
|
||||||
cout
|
|
||||||
<< " -f, --file[=FILENAME]\t {diabetes, glass, iris, kdd_JapaneseVowels, letter, liver-disorders, mfeat-factors}."
|
|
||||||
<< endl;
|
|
||||||
cout << " -p, --path[=FILENAME]\t folder where the data files are located, default " << PATH << endl;
|
|
||||||
cout << " -m, --model={AODE, KDB, SPODE, TAN}\t " << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
tuple<string, string, string> parse_arguments(int argc, char** argv)
|
|
||||||
{
|
|
||||||
string file_name;
|
|
||||||
string model_name;
|
|
||||||
string path = PATH;
|
|
||||||
const vector<struct option> long_options = {
|
|
||||||
{"help", no_argument, nullptr, 'h'},
|
|
||||||
{"file", required_argument, nullptr, 'f'},
|
|
||||||
{"path", required_argument, nullptr, 'p'},
|
|
||||||
{"model", required_argument, nullptr, 'm'},
|
|
||||||
{nullptr, no_argument, nullptr, 0}
|
|
||||||
};
|
|
||||||
while (true) {
|
|
||||||
const auto c = getopt_long(argc, argv, "hf:p:m:", long_options.data(), nullptr);
|
|
||||||
if (c == -1)
|
|
||||||
break;
|
|
||||||
switch (c) {
|
|
||||||
case 'h':
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(0);
|
|
||||||
case 'f':
|
|
||||||
file_name = string(optarg);
|
|
||||||
break;
|
|
||||||
case 'm':
|
|
||||||
model_name = string(optarg);
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
path = optarg;
|
|
||||||
if (path.back() != '/')
|
|
||||||
path += '/';
|
|
||||||
break;
|
|
||||||
case '?':
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
default:
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (file_name.empty()) {
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return make_tuple(file_name, path, model_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline constexpr auto hash_conv(const std::string_view sv)
|
inline constexpr auto hash_conv(const std::string_view sv)
|
||||||
{
|
{
|
||||||
@ -91,9 +31,32 @@ inline constexpr auto operator"" _sh(const char* str, size_t len)
|
|||||||
return hash_conv(std::string_view{ str, len });
|
return hash_conv(std::string_view{ str, len });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y, vector<string> features)
|
||||||
|
{
|
||||||
|
vector<mdlp::labels_t>Xd;
|
||||||
|
map<string, int> maxes;
|
||||||
|
|
||||||
|
auto fimdlp = mdlp::CPPFImdlp();
|
||||||
|
for (int i = 0; i < X.size(); i++) {
|
||||||
|
fimdlp.fit(X[i], y);
|
||||||
|
mdlp::labels_t& xd = fimdlp.transform(X[i]);
|
||||||
|
maxes[features[i]] = *max_element(xd.begin(), xd.end()) + 1;
|
||||||
|
Xd.push_back(xd);
|
||||||
|
}
|
||||||
|
return { Xd, maxes };
|
||||||
|
}
|
||||||
|
|
||||||
tuple<string, string, string> get_options(int argc, char** argv)
|
bool file_exists(const std::string& name)
|
||||||
|
{
|
||||||
|
if (FILE* file = fopen(name.c_str(), "r")) {
|
||||||
|
fclose(file);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
map<string, bool> datasets = {
|
map<string, bool> datasets = {
|
||||||
{"diabetes", true},
|
{"diabetes", true},
|
||||||
@ -105,35 +68,60 @@ tuple<string, string, string> get_options(int argc, char** argv)
|
|||||||
{"liver-disorders", true},
|
{"liver-disorders", true},
|
||||||
{"mfeat-factors", true},
|
{"mfeat-factors", true},
|
||||||
};
|
};
|
||||||
vector <string> models = { "AODE", "KDB", "SPODE", "TAN" };
|
auto valid_datasets = vector<string>();
|
||||||
string file_name;
|
for (auto dataset : datasets) {
|
||||||
string path;
|
valid_datasets.push_back(dataset.first);
|
||||||
string model_name;
|
}
|
||||||
tie(file_name, path, model_name) = parse_arguments(argc, argv);
|
argparse::ArgumentParser program("BayesNetSample");
|
||||||
if (datasets.find(file_name) == datasets.end()) {
|
program.add_argument("-f", "--file")
|
||||||
cout << "Invalid file name: " << file_name << endl;
|
.help("Dataset file name")
|
||||||
usage(argv[0]);
|
.action([valid_datasets](const std::string& value) {
|
||||||
|
if (find(valid_datasets.begin(), valid_datasets.end(), value) != valid_datasets.end()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
throw runtime_error("file must be one of {diabetes, ecoli, glass, iris, kdd_JapaneseVowels, letter, liver-disorders, mfeat-factors}");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
program.add_argument("-p", "--path")
|
||||||
|
.help(" folder where the data files are located, default")
|
||||||
|
.default_value(string{ PATH }
|
||||||
|
);
|
||||||
|
program.add_argument("-m", "--model")
|
||||||
|
.help("Model to use {AODE, KDB, SPODE, TAN}")
|
||||||
|
.action([](const std::string& value) {
|
||||||
|
static const vector<string> choices = { "AODE", "KDB", "SPODE", "TAN" };
|
||||||
|
if (find(choices.begin(), choices.end(), value) != choices.end()) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
throw runtime_error("Model must be one of {AODE, KDB, SPODE, TAN}");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
program.add_argument("--discretize").default_value(false).implicit_value(true);
|
||||||
|
bool class_last, discretize_dataset;
|
||||||
|
string model_name, file_name, path, complete_file_name;
|
||||||
|
try {
|
||||||
|
program.parse_args(argc, argv);
|
||||||
|
file_name = program.get<string>("file");
|
||||||
|
path = program.get<string>("path");
|
||||||
|
model_name = program.get<string>("model");
|
||||||
|
discretize_dataset = program.get<bool>("discretize");
|
||||||
|
complete_file_name = path + file_name + ".arff";
|
||||||
|
class_last = datasets[file_name];
|
||||||
|
if (!file_exists(complete_file_name)) {
|
||||||
|
throw runtime_error("Data File " + path + file_name + ".arff" + " does not exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (const exception& err) {
|
||||||
|
cerr << err.what() << endl;
|
||||||
|
cerr << program;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (!file_exists(path + file_name + ".arff")) {
|
|
||||||
cout << "Data File " << path + file_name + ".arff" << " does not exist" << endl;
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (find(models.begin(), models.end(), model_name) == models.end()) {
|
|
||||||
cout << "Invalid model name: " << model_name << endl;
|
|
||||||
usage(argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return { file_name, path, model_name };
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
/*
|
||||||
{
|
* Begin Processing
|
||||||
string file_name, path, model_name;
|
*/
|
||||||
tie(file_name, path, model_name) = get_options(argc, argv);
|
|
||||||
auto handler = ArffFiles();
|
auto handler = ArffFiles();
|
||||||
handler.load(path + file_name + ".arff");
|
handler.load(complete_file_name, class_last);
|
||||||
// Get Dataset X, y
|
// Get Dataset X, y
|
||||||
vector<mdlp::samples_t>& X = handler.getX();
|
vector<mdlp::samples_t>& X = handler.getX();
|
||||||
mdlp::labels_t& y = handler.getY();
|
mdlp::labels_t& y = handler.getY();
|
||||||
|
31
src/Platform/Folding.cc
Normal file
31
src/Platform/Folding.cc
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include "Folding.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
KFold::KFold(int k, int n, int seed)
|
||||||
|
{
|
||||||
|
this->k = k;
|
||||||
|
this->n = n;
|
||||||
|
indices = vector<int>(n);
|
||||||
|
iota(begin(indices), end(indices), 0); // fill with 0, 1, ..., n - 1
|
||||||
|
shuffle(indices.begin(), indices.end(), default_random_engine(seed));
|
||||||
|
}
|
||||||
|
pair<vector<int>, vector<int>> KFold::getFold(int nFold)
|
||||||
|
{
|
||||||
|
if (nFold >= k || nFold < 0) {
|
||||||
|
throw invalid_argument("nFold (" + to_string(nFold) + ") must be less than k (" + to_string(k) + ")");
|
||||||
|
}
|
||||||
|
int nTest = n / k;
|
||||||
|
auto train = vector<int>();
|
||||||
|
auto test = vector<int>();
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (i >= nTest * nFold && i < nTest * (nFold + 1)) {
|
||||||
|
test.push_back(indices[i]);
|
||||||
|
} else {
|
||||||
|
train.push_back(indices[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { train, test };
|
||||||
|
}
|
18
src/Platform/Folding.h
Normal file
18
src/Platform/Folding.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef FOLDING_H
|
||||||
|
#define FOLDING_H
|
||||||
|
#include <vector>
|
||||||
|
using namespace std;
|
||||||
|
class KFold {
|
||||||
|
private:
|
||||||
|
int k;
|
||||||
|
int n;
|
||||||
|
vector<int> indices;
|
||||||
|
|
||||||
|
public:
|
||||||
|
KFold(int k, int n, int seed);
|
||||||
|
pair<vector<int>, vector<int>> getFold(int);
|
||||||
|
};
|
||||||
|
class KStratifiedFold {
|
||||||
|
|
||||||
|
};
|
||||||
|
#endif
|
BIN
src/Platform/m
Executable file
BIN
src/Platform/m
Executable file
Binary file not shown.
51
src/Platform/main.cpp
Normal file
51
src/Platform/main.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include "Folding.h"
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
class A {
|
||||||
|
private:
|
||||||
|
int a;
|
||||||
|
public:
|
||||||
|
A(int a) : a(a) {}
|
||||||
|
int getA() { return a; }
|
||||||
|
};
|
||||||
|
class B : public A {
|
||||||
|
private:
|
||||||
|
int b;
|
||||||
|
public:
|
||||||
|
B(int a, int b) : A(a), b(b) {}
|
||||||
|
int getB() { return b; }
|
||||||
|
};
|
||||||
|
class C : public A {
|
||||||
|
private:
|
||||||
|
int c;
|
||||||
|
public:
|
||||||
|
C(int a, int c) : A(a), c(c) {}
|
||||||
|
int getC() { return c; }
|
||||||
|
};
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
auto fold = KFold(5, 100, 1);
|
||||||
|
for (int i = 0; i < 5; ++i) {
|
||||||
|
cout << "Fold: " << i << endl;
|
||||||
|
auto [train, test] = fold.getFold(i);
|
||||||
|
cout << "Train: ";
|
||||||
|
cout << "(" << train.size() << "): ";
|
||||||
|
for (auto j = 0; j < static_cast<int>(train.size()); j++)
|
||||||
|
cout << train[j] << ", ";
|
||||||
|
cout << endl;
|
||||||
|
cout << "Test: ";
|
||||||
|
cout << "(" << train.size() << "): ";
|
||||||
|
for (auto j = 0; j < static_cast<int>(test.size()); j++)
|
||||||
|
cout << test[j] << ", ";
|
||||||
|
cout << endl;
|
||||||
|
cout << "Vector poly" << endl;
|
||||||
|
auto some = vector<A>();
|
||||||
|
auto cx = C(5, 4);
|
||||||
|
auto bx = B(7, 6);
|
||||||
|
some.push_back(cx);
|
||||||
|
some.push_back(bx);
|
||||||
|
for (auto& obj : some) {
|
||||||
|
cout << "Obj :" << obj.getA() << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user