Fix inplace prediction interval. (#6259)

* Add back the interval in call.
* Make the interval non-optional.
This commit is contained in:
Jiaming Yuan 2020-10-28 13:13:59 +08:00 committed by GitHub
parent cc76724762
commit 3310e208fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 12 deletions

View File

@ -133,7 +133,7 @@ class Learner : public Model, public Configurable, public dmlc::Serializable {
virtual void InplacePredict(dmlc::any const& x, std::string const& type, virtual void InplacePredict(dmlc::any const& x, std::string const& type,
float missing, float missing,
HostDeviceVector<bst_float> **out_preds, HostDeviceVector<bst_float> **out_preds,
uint32_t layer_begin = 0, uint32_t layer_end = 0) = 0; uint32_t layer_begin, uint32_t layer_end) = 0;
void LoadModel(Json const& in) override = 0; void LoadModel(Json const& in) override = 0;
void SaveModel(Json* out) const override = 0; void SaveModel(Json* out) const override = 0;

View File

@ -528,8 +528,8 @@ XGB_DLL int XGBoosterPredictFromDense(BoosterHandle handle, float *values,
xgboost::bst_ulong n_rows, xgboost::bst_ulong n_rows,
xgboost::bst_ulong n_cols, xgboost::bst_ulong n_cols,
float missing, float missing,
unsigned, unsigned iteration_begin,
unsigned, unsigned iteration_end,
char const* c_type, char const* c_type,
xgboost::bst_ulong cache_id, xgboost::bst_ulong cache_id,
xgboost::bst_ulong *out_len, xgboost::bst_ulong *out_len,
@ -543,7 +543,7 @@ XGB_DLL int XGBoosterPredictFromDense(BoosterHandle handle, float *values,
new xgboost::data::DenseAdapter(values, n_rows, n_cols)}; new xgboost::data::DenseAdapter(values, n_rows, n_cols)};
HostDeviceVector<float>* p_predt { nullptr }; HostDeviceVector<float>* p_predt { nullptr };
std::string type { c_type }; std::string type { c_type };
learner->InplacePredict(x, type, missing, &p_predt); learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end);
CHECK(p_predt); CHECK(p_predt);
*out_result = dmlc::BeginPtr(p_predt->HostVector()); *out_result = dmlc::BeginPtr(p_predt->HostVector());
@ -560,8 +560,8 @@ XGB_DLL int XGBoosterPredictFromCSR(BoosterHandle handle,
size_t nelem, size_t nelem,
size_t num_col, size_t num_col,
float missing, float missing,
unsigned, unsigned iteration_begin,
unsigned, unsigned iteration_end,
char const *c_type, char const *c_type,
xgboost::bst_ulong cache_id, xgboost::bst_ulong cache_id,
xgboost::bst_ulong *out_len, xgboost::bst_ulong *out_len,
@ -575,7 +575,7 @@ XGB_DLL int XGBoosterPredictFromCSR(BoosterHandle handle,
new xgboost::data::CSRAdapter(indptr, indices, data, nindptr - 1, nelem, num_col)}; new xgboost::data::CSRAdapter(indptr, indices, data, nindptr - 1, nelem, num_col)};
HostDeviceVector<float>* p_predt { nullptr }; HostDeviceVector<float>* p_predt { nullptr };
std::string type { c_type }; std::string type { c_type };
learner->InplacePredict(x, type, missing, &p_predt); learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end);
CHECK(p_predt); CHECK(p_predt);
*out_result = dmlc::BeginPtr(p_predt->HostVector()); *out_result = dmlc::BeginPtr(p_predt->HostVector());

View File

@ -49,7 +49,7 @@ XGB_DLL int XGBoosterPredictFromArrayInterfaceColumns(BoosterHandle handle,
auto x = std::make_shared<data::CudfAdapter>(json_str); auto x = std::make_shared<data::CudfAdapter>(json_str);
HostDeviceVector<float>* p_predt { nullptr }; HostDeviceVector<float>* p_predt { nullptr };
std::string type { c_type }; std::string type { c_type };
learner->InplacePredict(x, type, missing, &p_predt); learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end);
CHECK(p_predt); CHECK(p_predt);
CHECK(p_predt->DeviceCanRead()); CHECK(p_predt->DeviceCanRead());
@ -77,7 +77,7 @@ XGB_DLL int XGBoosterPredictFromArrayInterface(BoosterHandle handle,
auto x = std::make_shared<data::CupyAdapter>(json_str); auto x = std::make_shared<data::CupyAdapter>(json_str);
HostDeviceVector<float>* p_predt { nullptr }; HostDeviceVector<float>* p_predt { nullptr };
std::string type { c_type }; std::string type { c_type };
learner->InplacePredict(x, type, missing, &p_predt); learner->InplacePredict(x, type, missing, &p_predt, iteration_begin, iteration_end);
CHECK(p_predt); CHECK(p_predt);
CHECK(p_predt->DeviceCanRead()); CHECK(p_predt->DeviceCanRead());

View File

@ -207,8 +207,8 @@ class GBTree : public GradientBooster {
void InplacePredict(dmlc::any const &x, float missing, void InplacePredict(dmlc::any const &x, float missing,
PredictionCacheEntry *out_preds, PredictionCacheEntry *out_preds,
uint32_t layer_begin = 0, uint32_t layer_begin,
unsigned layer_end = 0) const override { unsigned layer_end) const override {
CHECK(configured_); CHECK(configured_);
// From here on, layer becomes concrete trees. // From here on, layer becomes concrete trees.
bst_group_t groups = model_.learner_model_param->num_output_group; bst_group_t groups = model_.learner_model_param->num_output_group;

View File

@ -1098,7 +1098,7 @@ class LearnerImpl : public LearnerIO {
void InplacePredict(dmlc::any const &x, std::string const &type, void InplacePredict(dmlc::any const &x, std::string const &type,
float missing, HostDeviceVector<bst_float> **out_preds, float missing, HostDeviceVector<bst_float> **out_preds,
uint32_t layer_begin = 0, uint32_t layer_end = 0) override { uint32_t layer_begin, uint32_t layer_end) override {
this->Configure(); this->Configure();
auto& out_predictions = this->GetThreadLocal().prediction_entry; auto& out_predictions = this->GetThreadLocal().prediction_entry;
this->gbm_->InplacePredict(x, missing, &out_predictions, layer_begin, this->gbm_->InplacePredict(x, missing, &out_predictions, layer_begin,

View File

@ -44,6 +44,11 @@ class TestInplacePredict(unittest.TestCase):
np.testing.assert_allclose(predt_from_dmatrix, predt_from_array) np.testing.assert_allclose(predt_from_dmatrix, predt_from_array)
predt_from_array = booster.inplace_predict(X[:10, ...], iteration_range=(0, 4))
predt_from_dmatrix = booster.predict(test, ntree_limit=4)
np.testing.assert_allclose(predt_from_dmatrix, predt_from_array)
def predict_dense(x): def predict_dense(x):
inplace_predt = booster.inplace_predict(x) inplace_predt = booster.inplace_predict(x)
d = xgb.DMatrix(x) d = xgb.DMatrix(x)