xgboost
gbm.h
Go to the documentation of this file.
1 
8 #ifndef XGBOOST_GBM_H_
9 #define XGBOOST_GBM_H_
10 
11 #include <dmlc/registry.h>
12 #include <dmlc/any.h>
13 #include <xgboost/base.h>
14 #include <xgboost/data.h>
16 #include <xgboost/model.h>
17 
18 #include <vector>
19 #include <utility>
20 #include <string>
21 #include <functional>
22 #include <unordered_map>
23 #include <memory>
24 
25 namespace xgboost {
26 
27 class Json;
28 class FeatureMap;
29 class ObjFunction;
30 
31 struct GenericParameter;
32 struct LearnerModelParam;
33 struct PredictionCacheEntry;
34 class PredictionContainer;
35 
39 class GradientBooster : public Model, public Configurable {
40  protected:
42 
43  public:
45  ~GradientBooster() override = default;
52  virtual void Configure(const std::vector<std::pair<std::string, std::string> >& cfg) = 0;
57  virtual void Load(dmlc::Stream* fi) = 0;
62  virtual void Save(dmlc::Stream* fo) const = 0;
68  virtual bool AllowLazyCheckPoint() const {
69  return false;
70  }
78  virtual void DoBoost(DMatrix* p_fmat, HostDeviceVector<GradientPair>* in_gpair,
79  PredictionCacheEntry *prediction) = 0;
80 
92  virtual void PredictBatch(DMatrix* dmat,
93  PredictionCacheEntry* out_preds,
94  bool training,
95  unsigned ntree_limit = 0) = 0;
96 
106  virtual void InplacePredict(dmlc::any const &x, float missing,
107  PredictionCacheEntry *out_preds,
108  uint32_t layer_begin = 0,
109  uint32_t layer_end = 0) const {
110  LOG(FATAL) << "Inplace predict is not supported by current booster.";
111  }
123  virtual void PredictInstance(const SparsePage::Inst& inst,
124  std::vector<bst_float>* out_preds,
125  unsigned ntree_limit = 0) = 0;
134  virtual void PredictLeaf(DMatrix* dmat,
135  std::vector<bst_float>* out_preds,
136  unsigned ntree_limit = 0) = 0;
137 
149  virtual void PredictContribution(DMatrix* dmat,
150  std::vector<bst_float>* out_contribs,
151  unsigned ntree_limit = 0,
152  bool approximate = false, int condition = 0,
153  unsigned condition_feature = 0) = 0;
154 
155  virtual void PredictInteractionContributions(DMatrix* dmat,
156  std::vector<bst_float>* out_contribs,
157  unsigned ntree_limit, bool approximate) = 0;
158 
166  virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
167  bool with_stats,
168  std::string format) const = 0;
172  virtual bool UseGPU() const = 0;
180  static GradientBooster* Create(
181  const std::string& name,
182  GenericParameter const* generic_param,
183  LearnerModelParam const* learner_model_param);
184 };
185 
190  : public dmlc::FunctionRegEntryBase<
191  GradientBoosterReg,
192  std::function<GradientBooster* (LearnerModelParam const* learner_model_param)> > {
193 };
194 
207 #define XGBOOST_REGISTER_GBM(UniqueId, Name) \
208  static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \
209  __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \
210  ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name)
211 
212 } // namespace xgboost
213 #endif // XGBOOST_GBM_H_
static GradientBooster * Create(const std::string &name, GenericParameter const *generic_param, LearnerModelParam const *learner_model_param)
create a gradient booster from given name
virtual void InplacePredict(dmlc::any const &x, float missing, PredictionCacheEntry *out_preds, uint32_t layer_begin=0, uint32_t layer_end=0) const
Inplace prediction.
Definition: gbm.h:106
Definition: learner.h:241
virtual std::vector< std::string > DumpModel(const FeatureMap &fmap, bool with_stats, std::string format) const =0
dump the model in the requested format
virtual void PredictContribution(DMatrix *dmat, std::vector< bst_float > *out_contribs, 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) *...
Definition: host_device_vector.h:86
virtual void PredictInteractionContributions(DMatrix *dmat, std::vector< bst_float > *out_contribs, unsigned ntree_limit, bool approximate)=0
The input data structure of xgboost.
virtual void PredictBatch(DMatrix *dmat, PredictionCacheEntry *out_preds, bool training, unsigned ntree_limit=0)=0
generate predictions for given feature matrix
Definition: generic_parameters.h:14
Defines the abstract interface for different components in XGBoost.
Internal data structured used by XGBoost during training.
Definition: data.h:464
A device-and-host vector abstraction layer.
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:126
Registry entry for tree updater.
Definition: gbm.h:189
Definition: model.h:17
virtual void Configure(const std::vector< std::pair< std::string, std::string > > &cfg)=0
Set the configuration of gradient boosting. User must call configure once before InitModel and Traini...
~GradientBooster() override=default
virtual destructor
virtual bool AllowLazyCheckPoint() const
whether the model allow lazy checkpoint return true if model is only updated in DoBoost after all All...
Definition: gbm.h:68
virtual void PredictLeaf(DMatrix *dmat, std::vector< bst_float > *out_preds, unsigned ntree_limit=0)=0
predict the leaf index of each tree, the output will be nsample * ntree vector this is only valid in ...
virtual void Load(dmlc::Stream *fi)=0
load model from stream
Definition: model.h:31
namespace of xgboost
Definition: base.h:102
defines configuration macros of xgboost.
virtual bool UseGPU() const =0
Whether the current booster uses GPU.
virtual void Save(dmlc::Stream *fo) const =0
save model to stream.
virtual void PredictInstance(const SparsePage::Inst &inst, std::vector< bst_float > *out_preds, unsigned ntree_limit=0)=0
online prediction function, predict score for one instance at a time NOTE: use the batch prediction i...
virtual void DoBoost(DMatrix *p_fmat, HostDeviceVector< GradientPair > *in_gpair, PredictionCacheEntry *prediction)=0
perform update to the model(boosting)
interface of gradient boosting model.
Definition: gbm.h:39
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:35
GenericParameter const * generic_param_
Definition: gbm.h:41