Compare commits

7 Commits

16 changed files with 29 additions and 39 deletions

View File

@@ -13,6 +13,7 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")

View File

@@ -15,12 +15,12 @@ class PlatformConan(ConanFile):
def requirements(self): def requirements(self):
# Core dependencies from vcpkg.json # Core dependencies from vcpkg.json
self.requires("argparse/3.2") self.requires("argparse/3.2")
self.requires("libtorch/2.7.0") self.requires("libtorch/2.7.1")
self.requires("nlohmann_json/3.11.3") self.requires("nlohmann_json/3.11.3")
self.requires("folding/1.1.1") self.requires("folding/1.1.2")
self.requires("fimdlp/2.1.0") self.requires("fimdlp/2.1.1")
self.requires("arff-files/1.2.0") self.requires("arff-files/1.2.1")
self.requires("bayesnet/1.2.0") self.requires("bayesnet/1.2.1")
self.requires("pyclassifiers/1.0.3") self.requires("pyclassifiers/1.0.3")
self.requires("libxlsxwriter/1.2.2") self.requires("libxlsxwriter/1.2.2")

View File

@@ -1,18 +0,0 @@
#ifndef TENSOR_UTILS_H
#define TENSOR_UTILS_H
#include <torch/torch.h>
#include <vector>
namespace platform {
template <typename T>
std::vector<T> tensorToVector(const torch::Tensor& tensor)
{
torch::Tensor contig_tensor = tensor.contiguous();
auto num_elements = contig_tensor.numel();
const T* tensor_data = contig_tensor.data_ptr<T>();
std::vector<T> result(tensor_data, tensor_data + num_elements);
return result;
}
}
#endif

View File

@@ -5,6 +5,15 @@
namespace platform { namespace platform {
class TensorUtils { class TensorUtils {
public: public:
template <typename T>
static std::vector<T> tensorToVector(const torch::Tensor& tensor)
{
torch::Tensor contig_tensor = tensor.contiguous();
auto num_elements = contig_tensor.numel();
const T* tensor_data = contig_tensor.data_ptr<T>();
std::vector<T> result(tensor_data, tensor_data + num_elements);
return result;
}
static std::vector<std::vector<int>> to_matrix(const torch::Tensor& X) static std::vector<std::vector<int>> to_matrix(const torch::Tensor& X)
{ {
// Ensure tensor is contiguous in memory // Ensure tensor is contiguous in memory
@@ -53,7 +62,7 @@ namespace platform {
torch::Tensor tensor = torch::empty({ static_cast<long>(rows), static_cast<long>(cols) }, torch::kInt64); torch::Tensor tensor = torch::empty({ static_cast<long>(rows), static_cast<long>(cols) }, torch::kInt64);
for (size_t i = 0; i < rows; ++i) { for (size_t i = 0; i < rows; ++i) {
for (size_t j = 0; j < cols; ++j) { for (size_t j = 0; j < cols; ++j) {
tensor.index_put_({ static_cast<long>(i), static_cast<long>(j) }, data[i][j]); tensor.index_put_({static_cast<int64_t>(i), static_cast<int64_t>(j)}, torch::scalar_tensor(data[i][j]));
} }
} }
return tensor; return tensor;

View File

@@ -11,7 +11,7 @@
#include <numeric> #include <numeric>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include "TensorUtils.hpp" #include "common/TensorUtils.hpp"
// Conditional debug macro for performance-critical sections // Conditional debug macro for performance-critical sections
#define DEBUG_LOG(condition, ...) \ #define DEBUG_LOG(condition, ...) \

View File

@@ -38,7 +38,7 @@ namespace bayesnet {
torch::Tensor predict(torch::Tensor& X) override; torch::Tensor predict(torch::Tensor& X) override;
std::vector<int> predict(std::vector<std::vector<int>>& X) override; std::vector<int> predict(std::vector<std::vector<int>>& X) override;
torch::Tensor predict_proba(torch::Tensor& X) override; torch::Tensor predict_proba(torch::Tensor& X) override;
std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X); std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X) override;
void setDebug(bool debug) { this->debug = debug; } void setDebug(bool debug) { this->debug = debug; }
protected: protected:

View File

@@ -10,7 +10,7 @@
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
#include <limits> #include <limits>
#include "TensorUtils.hpp" #include "common/TensorUtils.hpp"
namespace bayesnet { namespace bayesnet {

View File

@@ -40,7 +40,7 @@ namespace bayesnet {
torch::Tensor predict(torch::Tensor& X) override; torch::Tensor predict(torch::Tensor& X) override;
std::vector<int> predict(std::vector<std::vector<int>>& X) override; std::vector<int> predict(std::vector<std::vector<int>>& X) override;
torch::Tensor predict_proba(torch::Tensor& X) override; torch::Tensor predict_proba(torch::Tensor& X) override;
std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X); std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X) override;
// Make predictions for a single sample // Make predictions for a single sample
int predictSample(const torch::Tensor& x) const; int predictSample(const torch::Tensor& x) const;

View File

@@ -5,7 +5,7 @@
// *************************************************************** // ***************************************************************
#include "ExpClf.h" #include "ExpClf.h"
#include "TensorUtils.hpp" #include "common/TensorUtils.hpp"
namespace platform { namespace platform {
ExpClf::ExpClf() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false) ExpClf::ExpClf() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false)

View File

@@ -5,7 +5,7 @@
// *************************************************************** // ***************************************************************
#include "ExpEnsemble.h" #include "ExpEnsemble.h"
#include "TensorUtils.hpp" #include "common/TensorUtils.hpp"
namespace platform { namespace platform {
ExpEnsemble::ExpEnsemble() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false) ExpEnsemble::ExpEnsemble() : semaphore_{ CountingSemaphore::getInstance() }, Boost(false)

View File

@@ -5,7 +5,7 @@
// *************************************************************** // ***************************************************************
#include "XA1DE.h" #include "XA1DE.h"
#include "TensorUtils.hpp" #include "common/TensorUtils.hpp"
namespace platform { namespace platform {
void XA1DE::trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) void XA1DE::trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing)

View File

@@ -10,7 +10,7 @@
#include <tuple> #include <tuple>
#include "XBAODE.h" #include "XBAODE.h"
#include "XSpode.hpp" #include "XSpode.hpp"
#include "TensorUtils.hpp" #include "common/TensorUtils.hpp"
#include <loguru.hpp> #include <loguru.hpp>
namespace platform { namespace platform {

View File

@@ -3,7 +3,7 @@
#include <numeric> #include <numeric>
#include <utility> #include <utility>
#include "RocAuc.h" #include "RocAuc.h"
#include "common/TensorUtils.h" // tensorToVector #include "common/TensorUtils.hpp" // tensorToVector
namespace platform { namespace platform {
double RocAuc::compute(const torch::Tensor& y_proba, const torch::Tensor& labels) double RocAuc::compute(const torch::Tensor& y_proba, const torch::Tensor& labels)

View File

@@ -1,6 +1,6 @@
#include <sstream> #include <sstream>
#include "Scores.h" #include "Scores.h"
#include "common/TensorUtils.h" // tensorToVector #include "common/TensorUtils.hpp" // tensorToVector
#include "common/Colors.h" #include "common/Colors.h"
namespace platform { namespace platform {
Scores::Scores(torch::Tensor& y_test, torch::Tensor& y_proba, int num_classes, std::vector<std::string> labels) : num_classes(num_classes), labels(labels), y_test(y_test), y_proba(y_proba) Scores::Scores(torch::Tensor& y_test, torch::Tensor& y_proba, int num_classes, std::vector<std::string> labels) : num_classes(num_classes), labels(labels), y_test(y_test), y_proba(y_proba)
@@ -50,7 +50,7 @@ namespace platform {
auto nClasses = num_classes; auto nClasses = num_classes;
if (num_classes == 2) if (num_classes == 2)
nClasses = 1; nClasses = 1;
auto y_testv = tensorToVector<int>(y_test); auto y_testv = TensorUtils::tensorToVector<int>(y_test);
std::vector<double> aucScores(nClasses, 0.0); std::vector<double> aucScores(nClasses, 0.0);
std::vector<std::pair<double, int>> scoresAndLabels; std::vector<std::pair<double, int>> scoresAndLabels;
for (size_t classIdx = 0; classIdx < nClasses; ++classIdx) { for (size_t classIdx = 0; classIdx < nClasses; ++classIdx) {

View File

@@ -54,10 +54,8 @@ namespace platform {
} }
void ExcelFile::setProperties(std::string title) void ExcelFile::setProperties(std::string title)
{ {
char line[title.size() + 1];
strcpy(line, title.c_str());
lxw_doc_properties properties = { lxw_doc_properties properties = {
.title = line, .title = title.c_str(),
.subject = (char*)"Machine learning results", .subject = (char*)"Machine learning results",
.author = (char*)"Ricardo Montañana Gómez", .author = (char*)"Ricardo Montañana Gómez",
.manager = (char*)"Dr. J. A. Gámez, Dr. J. M. Puerta", .manager = (char*)"Dr. J. A. Gámez, Dr. J. M. Puerta",

View File

@@ -13,7 +13,7 @@
#include <stdexcept> #include <stdexcept>
#include "experimental_clfs/AdaBoost.h" #include "experimental_clfs/AdaBoost.h"
#include "experimental_clfs/DecisionTree.h" #include "experimental_clfs/DecisionTree.h"
#include "experimental_clfs/TensorUtils.hpp" #include "common/TensorUtils.hpp"
#include "TestUtils.h" #include "TestUtils.h"
using namespace bayesnet; using namespace bayesnet;