Add conversion methods
This commit is contained in:
@@ -61,6 +61,7 @@ namespace platform {
|
||||
std::cout << "* Time to build the model: " << timert.getDuration() << " seconds" << std::endl;
|
||||
// exit(1);
|
||||
}
|
||||
fitted = true;
|
||||
return *this;
|
||||
}
|
||||
std::vector<std::vector<double>> XA1DE::predict_proba(std::vector<std::vector<int>>& test_data)
|
||||
@@ -115,6 +116,9 @@ namespace platform {
|
||||
}
|
||||
std::vector<int> XA1DE::predict(std::vector<std::vector<int>>& test_data)
|
||||
{
|
||||
if (!fitted) {
|
||||
throw std::logic_error(CLASSIFIER_NOT_FITTED);
|
||||
}
|
||||
auto probabilities = predict_proba(test_data);
|
||||
std::vector<int> predictions(probabilities.size(), 0);
|
||||
|
||||
@@ -147,4 +151,47 @@ namespace platform {
|
||||
}
|
||||
return static_cast<float>(correct) / predictions.size();
|
||||
}
|
||||
std::vector<std::vector<int>> to_matrix(const torch::Tensor& X)
|
||||
{
|
||||
// Ensure tensor is contiguous in memory
|
||||
auto X_contig = X.contiguous();
|
||||
|
||||
// Access tensor data pointer directly
|
||||
auto data_ptr = X_contig.data_ptr<int>();
|
||||
|
||||
// IF you are using int64_t as the data type, use the following line
|
||||
//auto data_ptr = X_contig.data_ptr<int64_t>();
|
||||
//std::vector<std::vector<int64_t>> data(X.size(0), std::vector<int64_t>(X.size(1)));
|
||||
|
||||
// Prepare output container
|
||||
std::vector<std::vector<int>> data(X.size(0), std::vector<int>(X.size(1)));
|
||||
|
||||
// Fill the 2D vector in a single loop using pointer arithmetic
|
||||
int rows = X.size(0);
|
||||
int cols = X.size(1);
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
std::copy(data_ptr + i * cols, data_ptr + (i + 1) * cols, data[i].begin());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
std::vector<int> to_vector(const torch::Tensor& y)
|
||||
{
|
||||
// Ensure the tensor is contiguous in memory
|
||||
auto y_contig = y.contiguous();
|
||||
|
||||
// Access data pointer
|
||||
auto data_ptr = y_contig.data_ptr<int>();
|
||||
|
||||
// Prepare output container
|
||||
std::vector<int> data(y.size(0));
|
||||
|
||||
// Copy data efficiently
|
||||
std::copy(data_ptr, data_ptr + y.size(0), data.begin());
|
||||
|
||||
return data;
|
||||
}
|
||||
XA1DE& XA1DE::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing)
|
||||
{
|
||||
return fit(to_matrix(X), to_vector(y), features, className, states, smoothing);
|
||||
}
|
||||
}
|
@@ -21,16 +21,18 @@ namespace platform {
|
||||
public:
|
||||
XA1DE();
|
||||
virtual ~XA1DE() = default;
|
||||
const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
|
||||
|
||||
std::vector<std::vector<double>> predict_proba_threads(const std::vector<std::vector<int>>& test_data);
|
||||
std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X) override;
|
||||
float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
|
||||
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||
XA1DE& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) override;
|
||||
|
||||
XA1DE& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) override { return *this; };
|
||||
XA1DE& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const bayesnet::Smoothing_t smoothing) override { return *this; };
|
||||
XA1DE& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing) override { return *this; };
|
||||
torch::Tensor predict(torch::Tensor& X) override { return torch::zeros(0); };
|
||||
std::vector<int> predict(std::vector<std::vector<int>>& X) override;
|
||||
torch::Tensor predict_proba(torch::Tensor& X) override { return torch::zeros(0); };
|
||||
|
||||
int getNumberOfNodes() const override { return 0; };
|
||||
@@ -61,6 +63,8 @@ namespace platform {
|
||||
w = w * num_instances / sum;
|
||||
}
|
||||
}
|
||||
std::vector<int> to_vector(const torch::Tensor& y);
|
||||
std::vector<std::vector<int>> to_matrix(const torch::Tensor& X);
|
||||
Xaode aode_;
|
||||
std::vector<double> weights_;
|
||||
CountingSemaphore& semaphore_;
|
||||
@@ -69,6 +73,7 @@ namespace platform {
|
||||
std::vector<std::string> notes;
|
||||
bool use_threads = false;
|
||||
std::string version = "0.9.7";
|
||||
bool fitted = false;
|
||||
};
|
||||
}
|
||||
#endif // XA1DE_H
|
Reference in New Issue
Block a user