Refactor TensorUtils to a unique header file
This commit is contained in:
@@ -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
|
|
@@ -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
|
@@ -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, ...) \
|
||||||
|
@@ -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:
|
||||||
|
@@ -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 {
|
||||||
|
|
||||||
|
@@ -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;
|
||||||
|
@@ -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)
|
||||||
|
@@ -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)
|
||||||
|
@@ -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)
|
||||||
|
@@ -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 {
|
||||||
|
@@ -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)
|
||||||
|
@@ -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) {
|
||||||
|
@@ -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;
|
||||||
|
Reference in New Issue
Block a user