Refactor TensorUtils to a unique header file

This commit is contained in:
2025-07-21 11:10:26 +02:00
parent 5fd0ef692d
commit b990684581
13 changed files with 21 additions and 30 deletions

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 {
class TensorUtils {
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)
{
// Ensure tensor is contiguous in memory

View File

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

View File

@@ -38,7 +38,7 @@ namespace bayesnet {
torch::Tensor predict(torch::Tensor& X) override;
std::vector<int> predict(std::vector<std::vector<int>>& 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; }
protected:

View File

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

View File

@@ -40,7 +40,7 @@ namespace bayesnet {
torch::Tensor predict(torch::Tensor& X) override;
std::vector<int> predict(std::vector<std::vector<int>>& 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
int predictSample(const torch::Tensor& x) const;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
#include <sstream>
#include "Scores.h"
#include "common/TensorUtils.h" // tensorToVector
#include "common/TensorUtils.hpp" // tensorToVector
#include "common/Colors.h"
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)
@@ -50,7 +50,7 @@ namespace platform {
auto nClasses = num_classes;
if (num_classes == 2)
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<std::pair<double, int>> scoresAndLabels;
for (size_t classIdx = 0; classIdx < nClasses; ++classIdx) {

View File

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