Fix xgboost error in predict/predict_proba
This commit is contained in:
@@ -93,12 +93,20 @@ namespace pywrap {
|
|||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
throw std::runtime_error("Error creating object for predict in " + module + " and class " + className);
|
throw std::runtime_error("Error creating object for predict in " + module + " and class " + className);
|
||||||
}
|
}
|
||||||
|
if (xgboost) {
|
||||||
|
long* data = reinterpret_cast<long*>(prediction.get_data());
|
||||||
|
std::vector<int> vPrediction(data, data + prediction.shape(0));
|
||||||
|
auto resultTensor = torch::tensor(vPrediction, torch::kInt32);
|
||||||
|
Py_XDECREF(incoming);
|
||||||
|
return resultTensor;
|
||||||
|
} else {
|
||||||
int* data = reinterpret_cast<int*>(prediction.get_data());
|
int* data = reinterpret_cast<int*>(prediction.get_data());
|
||||||
std::vector<int> vPrediction(data, data + prediction.shape(0));
|
std::vector<int> vPrediction(data, data + prediction.shape(0));
|
||||||
auto resultTensor = torch::tensor(vPrediction, torch::kInt32);
|
auto resultTensor = torch::tensor(vPrediction, torch::kInt32);
|
||||||
Py_XDECREF(incoming);
|
Py_XDECREF(incoming);
|
||||||
return resultTensor;
|
return resultTensor;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
torch::Tensor PyClassifier::predict_proba(torch::Tensor& X)
|
torch::Tensor PyClassifier::predict_proba(torch::Tensor& X)
|
||||||
{
|
{
|
||||||
int dimension = X.size(1);
|
int dimension = X.size(1);
|
||||||
@@ -118,12 +126,20 @@ namespace pywrap {
|
|||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
throw std::runtime_error("Error creating object for predict_proba in " + module + " and class " + className);
|
throw std::runtime_error("Error creating object for predict_proba in " + module + " and class " + className);
|
||||||
}
|
}
|
||||||
|
if (xgboost) {
|
||||||
|
float* data = reinterpret_cast<float*>(prediction.get_data());
|
||||||
|
std::vector<float> vPrediction(data, data + prediction.shape(0) * prediction.shape(1));
|
||||||
|
auto resultTensor = torch::tensor(vPrediction, torch::kFloat64).reshape({ prediction.shape(0), prediction.shape(1) });
|
||||||
|
Py_XDECREF(incoming);
|
||||||
|
return resultTensor;
|
||||||
|
} else {
|
||||||
double* data = reinterpret_cast<double*>(prediction.get_data());
|
double* data = reinterpret_cast<double*>(prediction.get_data());
|
||||||
std::vector<double> vPrediction(data, data + prediction.shape(0) * prediction.shape(1));
|
std::vector<double> vPrediction(data, data + prediction.shape(0) * prediction.shape(1));
|
||||||
auto resultTensor = torch::tensor(vPrediction, torch::kFloat64).reshape({ prediction.shape(0), prediction.shape(1) });
|
auto resultTensor = torch::tensor(vPrediction, torch::kFloat64).reshape({ prediction.shape(0), prediction.shape(1) });
|
||||||
Py_XDECREF(incoming);
|
Py_XDECREF(incoming);
|
||||||
return resultTensor;
|
return resultTensor;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
float PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
float PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
|
||||||
{
|
{
|
||||||
auto [Xn, yn] = tensors2numpy(X, y);
|
auto [Xn, yn] = tensors2numpy(X, y);
|
||||||
|
@@ -49,6 +49,7 @@ namespace pywrap {
|
|||||||
nlohmann::json hyperparameters;
|
nlohmann::json hyperparameters;
|
||||||
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing = bayesnet::Smoothing_t::NONE) override {};
|
void trainModel(const torch::Tensor& weights, const bayesnet::Smoothing_t smoothing = bayesnet::Smoothing_t::NONE) override {};
|
||||||
std::vector<std::string> notes;
|
std::vector<std::string> notes;
|
||||||
|
bool xgboost = false;
|
||||||
private:
|
private:
|
||||||
PyWrap* pyWrap;
|
PyWrap* pyWrap;
|
||||||
std::string module;
|
std::string module;
|
||||||
|
@@ -5,5 +5,6 @@ namespace pywrap {
|
|||||||
XGBoost::XGBoost() : PyClassifier("xgboost", "XGBClassifier", true)
|
XGBoost::XGBoost() : PyClassifier("xgboost", "XGBClassifier", true)
|
||||||
{
|
{
|
||||||
validHyperparameters = { "tree_method", "early_stopping_rounds", "n_jobs" };
|
validHyperparameters = { "tree_method", "early_stopping_rounds", "n_jobs" };
|
||||||
|
xgboost = true;
|
||||||
}
|
}
|
||||||
} /* namespace pywrap */
|
} /* namespace pywrap */
|
@@ -116,23 +116,30 @@ TEST_CASE("XGBoost", "[PyClassifiers]")
|
|||||||
clf.setHyperparameters(hyperparameters);
|
clf.setHyperparameters(hyperparameters);
|
||||||
auto score = clf.score(raw.Xt, raw.yt);
|
auto score = clf.score(raw.Xt, raw.yt);
|
||||||
REQUIRE(score == Catch::Approx(0.98).epsilon(raw.epsilon));
|
REQUIRE(score == Catch::Approx(0.98).epsilon(raw.epsilon));
|
||||||
|
std::cout << "XGBoost score: " << score << std::endl;
|
||||||
}
|
}
|
||||||
// TEST_CASE("XGBoost predict proba", "[PyClassifiers]")
|
TEST_CASE("XGBoost predict proba", "[PyClassifiers]")
|
||||||
// {
|
{
|
||||||
// auto raw = RawDatasets("iris", true);
|
auto raw = RawDatasets("iris", true);
|
||||||
// auto clf = pywrap::XGBoost();
|
auto clf = pywrap::XGBoost();
|
||||||
// clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
clf.fit(raw.Xt, raw.yt, raw.featurest, raw.classNamet, raw.statest);
|
||||||
// // nlohmann::json hyperparameters = { "n_jobs=1" };
|
// nlohmann::json hyperparameters = { "n_jobs=1" };
|
||||||
// // clf.setHyperparameters(hyperparameters);
|
// clf.setHyperparameters(hyperparameters);
|
||||||
// auto predict = clf.predict(raw.Xt);
|
auto predict_proba = clf.predict_proba(raw.Xt);
|
||||||
// for (int row = 0; row < predict.size(0); row++) {
|
auto predict = clf.predict(raw.Xt);
|
||||||
|
// std::cout << "Predict proba: " << predict_proba << std::endl;
|
||||||
|
// std::cout << "Predict proba size: " << predict_proba.sizes() << std::endl;
|
||||||
|
// assert(predict.size(0) == predict_proba.size(0));
|
||||||
|
for (int row = 0; row < predict_proba.size(0); row++) {
|
||||||
// auto sum = 0.0;
|
// auto sum = 0.0;
|
||||||
// for (int col = 0; col < predict.size(1); col++) {
|
// std::cout << "Row " << std::setw(3) << row << ": ";
|
||||||
// std::cout << std::setw(12) << std::setprecision(10) << predict[row][col].item<double>() << " ";
|
// for (int col = 0; col < predict_proba.size(1); col++) {
|
||||||
// sum += predict[row][col].item<int>();
|
// std::cout << std::setw(9) << std::fixed << std::setprecision(7) << predict_proba[row][col].item<double>() << " ";
|
||||||
|
// sum += predict_proba[row][col].item<double>();
|
||||||
// }
|
// }
|
||||||
// std::cout << std::endl;
|
// std::cout << " -> " << std::setw(9) << std::fixed << std::setprecision(7) << sum << " -> " << torch::argmax(predict_proba[row]).item<int>() << " = " << predict[row].item<int>() << std::endl;
|
||||||
// // REQUIRE(sum == Catch::Approx(1.0).epsilon(raw.epsilon));
|
// // REQUIRE(sum == Catch::Approx(1.0).epsilon(raw.epsilon));
|
||||||
// }
|
REQUIRE(torch::argmax(predict_proba[row]).item<int>() == predict[row].item<int>());
|
||||||
// std::cout << predict << std::endl;
|
REQUIRE(torch::sum(predict_proba[row]).item<double>() == Catch::Approx(1.0).epsilon(raw.epsilon));
|
||||||
// }
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user