try openssl sha256
This commit is contained in:
parent
df9b4c48d2
commit
ca833a34f5
@ -45,7 +45,7 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
|||||||
# CMakes modules
|
# CMakes modules
|
||||||
# --------------
|
# --------------
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
|
||||||
|
find_package(OpenSSL REQUIRED)
|
||||||
include(AddGitSubmodule)
|
include(AddGitSubmodule)
|
||||||
if (CODE_COVERAGE)
|
if (CODE_COVERAGE)
|
||||||
enable_testing()
|
enable_testing()
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
#include "BoostAODE.h"
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
#include <functional>
|
||||||
|
#include <limits.h>
|
||||||
|
#include "BoostAODE.h"
|
||||||
#include "BayesMetrics.h"
|
#include "BayesMetrics.h"
|
||||||
#include "Colors.h"
|
#include "Colors.h"
|
||||||
#include "Folding.h"
|
#include "Folding.h"
|
||||||
#include <limits.h>
|
|
||||||
#include "Paths.h"
|
#include "Paths.h"
|
||||||
|
#include <openssl/evp.h>
|
||||||
|
|
||||||
namespace bayesnet {
|
namespace bayesnet {
|
||||||
BoostAODE::BoostAODE() : Ensemble() {}
|
BoostAODE::BoostAODE() : Ensemble() {}
|
||||||
@ -13,6 +15,8 @@ namespace bayesnet {
|
|||||||
// Models shall be built in trainModel
|
// Models shall be built in trainModel
|
||||||
// Prepare the validation dataset
|
// Prepare the validation dataset
|
||||||
auto y_ = dataset.index({ -1, "..." });
|
auto y_ = dataset.index({ -1, "..." });
|
||||||
|
int nSamples = dataset.size(1);
|
||||||
|
int nFeatures = dataset.size(0) - 1;
|
||||||
if (convergence) {
|
if (convergence) {
|
||||||
// Prepare train & validation sets from train data
|
// Prepare train & validation sets from train data
|
||||||
auto fold = platform::StratifiedKFold(5, y_, 271);
|
auto fold = platform::StratifiedKFold(5, y_, 271);
|
||||||
@ -38,7 +42,7 @@ namespace bayesnet {
|
|||||||
y_train = y_;
|
y_train = y_;
|
||||||
}
|
}
|
||||||
if (cfs != "") {
|
if (cfs != "") {
|
||||||
initializeModels();
|
initializeModels(nSamples, nFeatures);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void BoostAODE::setHyperparameters(nlohmann::json& hyperparameters)
|
void BoostAODE::setHyperparameters(nlohmann::json& hyperparameters)
|
||||||
@ -62,18 +66,52 @@ namespace bayesnet {
|
|||||||
cfs = hyperparameters["cfs"];
|
cfs = hyperparameters["cfs"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void BoostAODE::initializeModels()
|
string sha256(const string& input)
|
||||||
{
|
{
|
||||||
ifstream file(cfs + ".json");
|
EVP_MD_CTX* mdctx;
|
||||||
|
const EVP_MD* md;
|
||||||
|
unsigned char hash[EVP_MAX_MD_SIZE];
|
||||||
|
unsigned int hash_len;
|
||||||
|
|
||||||
|
OpenSSL_add_all_digests();
|
||||||
|
md = EVP_get_digestbyname("sha256");
|
||||||
|
mdctx = EVP_MD_CTX_new();
|
||||||
|
EVP_DigestInit_ex(mdctx, md, nullptr);
|
||||||
|
EVP_DigestUpdate(mdctx, input.c_str(), input.size());
|
||||||
|
EVP_DigestFinal_ex(mdctx, hash, &hash_len);
|
||||||
|
EVP_MD_CTX_free(mdctx);
|
||||||
|
stringstream oss;
|
||||||
|
for (unsigned int i = 0; i < hash_len; i++) {
|
||||||
|
oss << hex << (int)hash[i];
|
||||||
|
}
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoostAODE::initializeModels(int nSamples, int nFeatures)
|
||||||
|
{
|
||||||
|
// Read the CFS features
|
||||||
|
string output = "[", prefix = "";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& feature : features) {
|
||||||
|
output += prefix + feature;
|
||||||
|
if (first) {
|
||||||
|
prefix = ", ";
|
||||||
|
first = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output += "]";
|
||||||
|
// std::size_t str_hash = std::hash<std::string>{}(output);
|
||||||
|
string str_hash = sha256(output);
|
||||||
|
stringstream oss;
|
||||||
|
oss << "cfs/" << str_hash << ".json";
|
||||||
|
string name = oss.str();
|
||||||
|
ifstream file(name);
|
||||||
if (file.is_open()) {
|
if (file.is_open()) {
|
||||||
nlohmann::json data;
|
nlohmann::json features = nlohmann::json::parse(file);
|
||||||
file >> data;
|
|
||||||
file.close();
|
file.close();
|
||||||
auto model = "iris"; // has to come in when building object
|
|
||||||
auto features = data[model];
|
|
||||||
cout << "features: " << features.dump() << endl;
|
cout << "features: " << features.dump() << endl;
|
||||||
} else {
|
} else {
|
||||||
throw runtime_error("File " + cfs + ".json not found");
|
throw runtime_error("File " + name + " not found");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void BoostAODE::trainModel(const torch::Tensor& weights)
|
void BoostAODE::trainModel(const torch::Tensor& weights)
|
||||||
|
@ -15,7 +15,7 @@ namespace bayesnet {
|
|||||||
private:
|
private:
|
||||||
torch::Tensor dataset_;
|
torch::Tensor dataset_;
|
||||||
torch::Tensor X_train, y_train, X_test, y_test;
|
torch::Tensor X_train, y_train, X_test, y_test;
|
||||||
void initializeModels();
|
void initializeModels(int nSamples, int nFeatures);
|
||||||
// Hyperparameters
|
// Hyperparameters
|
||||||
bool repeatSparent = false; // if true, a feature can be selected more than once
|
bool repeatSparent = false; // if true, a feature can be selected more than once
|
||||||
int maxModels = 0;
|
int maxModels = 0;
|
||||||
|
@ -6,4 +6,4 @@ include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
|
|||||||
add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc
|
add_library(BayesNet bayesnetUtils.cc Network.cc Node.cc BayesMetrics.cc Classifier.cc
|
||||||
KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc BoostAODE.cc
|
KDB.cc TAN.cc SPODE.cc Ensemble.cc AODE.cc TANLd.cc KDBLd.cc SPODELd.cc AODELd.cc BoostAODE.cc
|
||||||
Mst.cc Proposal.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
|
Mst.cc Proposal.cc ${BayesNet_SOURCE_DIR}/src/Platform/Models.cc)
|
||||||
target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}")
|
target_link_libraries(BayesNet mdlp "${TORCH_LIBRARIES}" OpenSSL::Crypto)
|
Loading…
Reference in New Issue
Block a user