try openssl sha256

This commit is contained in:
Ricardo Montañana Gómez 2023-10-10 18:16:43 +02:00
parent df9b4c48d2
commit ca833a34f5
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
4 changed files with 51 additions and 13 deletions

View File

@ -45,7 +45,7 @@ SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
# CMakes modules
# --------------
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
find_package(OpenSSL REQUIRED)
include(AddGitSubmodule)
if (CODE_COVERAGE)
enable_testing()

View File

@ -1,10 +1,12 @@
#include "BoostAODE.h"
#include <set>
#include <functional>
#include <limits.h>
#include "BoostAODE.h"
#include "BayesMetrics.h"
#include "Colors.h"
#include "Folding.h"
#include <limits.h>
#include "Paths.h"
#include <openssl/evp.h>
namespace bayesnet {
BoostAODE::BoostAODE() : Ensemble() {}
@ -13,6 +15,8 @@ namespace bayesnet {
// Models shall be built in trainModel
// Prepare the validation dataset
auto y_ = dataset.index({ -1, "..." });
int nSamples = dataset.size(1);
int nFeatures = dataset.size(0) - 1;
if (convergence) {
// Prepare train & validation sets from train data
auto fold = platform::StratifiedKFold(5, y_, 271);
@ -38,7 +42,7 @@ namespace bayesnet {
y_train = y_;
}
if (cfs != "") {
initializeModels();
initializeModels(nSamples, nFeatures);
}
}
void BoostAODE::setHyperparameters(nlohmann::json& hyperparameters)
@ -62,18 +66,52 @@ namespace bayesnet {
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()) {
nlohmann::json data;
file >> data;
nlohmann::json features = nlohmann::json::parse(file);
file.close();
auto model = "iris"; // has to come in when building object
auto features = data[model];
cout << "features: " << features.dump() << endl;
} else {
throw runtime_error("File " + cfs + ".json not found");
throw runtime_error("File " + name + " not found");
}
}
void BoostAODE::trainModel(const torch::Tensor& weights)

View File

@ -15,7 +15,7 @@ namespace bayesnet {
private:
torch::Tensor dataset_;
torch::Tensor X_train, y_train, X_test, y_test;
void initializeModels();
void initializeModels(int nSamples, int nFeatures);
// Hyperparameters
bool repeatSparent = false; // if true, a feature can be selected more than once
int maxModels = 0;

View File

@ -6,4 +6,4 @@ include_directories(${BayesNet_SOURCE_DIR}/src/Platform)
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
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)