xgboost
predictor.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include <xgboost/base.h>
9 #include <xgboost/data.h>
10 
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <unordered_map>
15 #include <utility>
16 #include <vector>
17 
18 #include "../../src/gbm/gbtree_model.h"
19 #include "../../src/common/host_device_vector.h"
20 
21 // Forward declarations
22 namespace xgboost {
23 class TreeUpdater;
24 }
25 
26 namespace xgboost {
27 
40 class Predictor {
41  public:
42  virtual ~Predictor() = default;
43 
54  virtual void Init(const std::vector<std::pair<std::string, std::string>>& cfg,
55  const std::vector<std::shared_ptr<DMatrix>>& cache);
56 
69  virtual void PredictBatch(DMatrix* dmat, HostDeviceVector<bst_float>* out_preds,
70  const gbm::GBTreeModel& model, int tree_begin,
71  unsigned ntree_limit = 0) = 0;
72 
88  virtual void UpdatePredictionCache(
89  const gbm::GBTreeModel& model,
90  std::vector<std::unique_ptr<TreeUpdater>>* updaters,
91  int num_new_trees) = 0;
92 
110  virtual void PredictInstance(const SparsePage::Inst& inst,
111  std::vector<bst_float>* out_preds,
112  const gbm::GBTreeModel& model,
113  unsigned ntree_limit = 0,
114  unsigned root_index = 0) = 0;
115 
130  virtual void PredictLeaf(DMatrix* dmat, std::vector<bst_float>* out_preds,
131  const gbm::GBTreeModel& model,
132  unsigned ntree_limit = 0) = 0;
133 
152  virtual void PredictContribution(DMatrix* dmat,
153  std::vector<bst_float>* out_contribs,
154  const gbm::GBTreeModel& model,
155  unsigned ntree_limit = 0,
156  bool approximate = false,
157  int condition = 0,
158  unsigned condition_feature = 0) = 0;
159 
160  virtual void PredictInteractionContributions(DMatrix* dmat,
161  std::vector<bst_float>* out_contribs,
162  const gbm::GBTreeModel& model,
163  unsigned ntree_limit = 0,
164  bool approximate = false) = 0;
165 
173  static Predictor* Create(std::string name);
174 
175  protected:
182  std::shared_ptr<DMatrix> data;
184  };
185 
191  std::unordered_map<DMatrix*, PredictionCacheEntry> cache_;
192 };
193 
198  : public dmlc::FunctionRegEntryBase<PredictorReg,
199  std::function<Predictor*()>> {};
200 
201 #define XGBOOST_REGISTER_PREDICTOR(UniqueId, Name) \
202  static DMLC_ATTRIBUTE_UNUSED ::xgboost::PredictorReg& \
203  __make_##PredictorReg##_##UniqueId##__ = \
204  ::dmlc::Registry<::xgboost::PredictorReg>::Get()->__REGISTER__(Name)
205 } // namespace xgboost
virtual void PredictContribution(DMatrix *dmat, std::vector< bst_float > *out_contribs, const gbm::GBTreeModel &model, unsigned ntree_limit=0, bool approximate=false, int condition=0, unsigned condition_feature=0)=0
feature contributions to individual predictions; the output will be a vector of length (nfeats + 1) *...
virtual void PredictInstance(const SparsePage::Inst &inst, std::vector< bst_float > *out_preds, const gbm::GBTreeModel &model, unsigned ntree_limit=0, unsigned root_index=0)=0
online prediction function, predict score for one instance at a time NOTE: use the batch prediction i...
virtual void PredictLeaf(DMatrix *dmat, std::vector< bst_float > *out_preds, const gbm::GBTreeModel &model, unsigned ntree_limit=0)=0
predict the leaf index of each tree, the output will be nsample * ntree vector this is only valid in ...
Performs prediction on individual training instances or batches of instances for GBTree. The predictor also manages a prediction cache associated with input matrices. If possible, it will use previously calculated predictions instead of calculating new predictions. Prediction functions all take a GBTreeModel and a DMatrix as input and output a vector of predictions. The predictor does not modify any state of the model itself.
Definition: predictor.h:40
The input data structure of xgboost.
static Predictor * Create(std::string name)
Creates a new Predictor*.
Internal data structured used by XGBoost during training. There are two ways to create a customized D...
Definition: data.h:406
Registry entry for predictor.
Definition: predictor.h:197
virtual void Init(const std::vector< std::pair< std::string, std::string >> &cfg, const std::vector< std::shared_ptr< DMatrix >> &cache)
Configure and register input matrices in prediction cache.
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:109
std::unordered_map< DMatrix *, PredictionCacheEntry > cache_
Map of matrices and associated cached predictions to facilitate storing and looking up predictions...
Definition: predictor.h:191
HostDeviceVector< bst_float > predictions
Definition: predictor.h:183
virtual void PredictBatch(DMatrix *dmat, HostDeviceVector< bst_float > *out_preds, const gbm::GBTreeModel &model, int tree_begin, unsigned ntree_limit=0)=0
Generate batch predictions for a given feature matrix. May use cached predictions if available instea...
namespace of xgboost
Definition: base.h:79
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:181
defines configuration macros of xgboost.
virtual ~Predictor()=default
virtual void PredictInteractionContributions(DMatrix *dmat, std::vector< bst_float > *out_contribs, const gbm::GBTreeModel &model, unsigned ntree_limit=0, bool approximate=false)=0
virtual void UpdatePredictionCache(const gbm::GBTreeModel &model, std::vector< std::unique_ptr< TreeUpdater >> *updaters, int num_new_trees)=0
Update the internal prediction cache using newly added trees. Will use the tree updater to do this if...
std::shared_ptr< DMatrix > data
Definition: predictor.h:182