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 <xgboost/base.h>
13 #include <xgboost/data.h>
15 #include <xgboost/model.h>
16 
17 #include <vector>
18 #include <utility>
19 #include <string>
20 #include <functional>
21 #include <unordered_map>
22 #include <memory>
23 
24 namespace xgboost {
25 
26 class Json;
27 class FeatureMap;
28 class ObjFunction;
29 
30 struct Context;
31 struct LearnerModelParam;
32 struct PredictionCacheEntry;
33 
37 class GradientBooster : public Model, public Configurable {
38  protected:
39  Context const* ctx_;
40  explicit GradientBooster(Context const* ctx) : ctx_{ctx} {}
41 
42  public:
44  ~GradientBooster() override = default;
51  virtual void Configure(const std::vector<std::pair<std::string, std::string> >& cfg) = 0;
56  virtual void Load(dmlc::Stream* fi) = 0;
61  virtual void Save(dmlc::Stream* fo) const = 0;
69  virtual void Slice(bst_layer_t /*begin*/, bst_layer_t /*end*/, bst_layer_t /*step*/,
70  GradientBooster* /*out*/, bool* /*out_of_bound*/) const {
71  LOG(FATAL) << "Slice is not supported by the current booster.";
72  }
76  [[nodiscard]] virtual std::int32_t BoostedRounds() const = 0;
81  [[nodiscard]] virtual bool ModelFitted() const = 0;
91  virtual void DoBoost(DMatrix* p_fmat, linalg::Matrix<GradientPair>* in_gpair,
92  PredictionCacheEntry*, ObjFunction const* obj) = 0;
93 
104  virtual void PredictBatch(DMatrix* dmat, PredictionCacheEntry* out_preds, bool training,
105  bst_layer_t begin, bst_layer_t end) = 0;
106 
116  virtual void InplacePredict(std::shared_ptr<DMatrix>, float, PredictionCacheEntry*, bst_layer_t,
117  bst_layer_t) const {
118  LOG(FATAL) << "Inplace predict is not supported by the current booster.";
119  }
132  virtual void PredictInstance(const SparsePage::Inst& inst,
133  std::vector<bst_float>* out_preds,
134  unsigned layer_begin, unsigned layer_end) = 0;
143  virtual void PredictLeaf(DMatrix *dmat,
144  HostDeviceVector<bst_float> *out_preds,
145  unsigned layer_begin, unsigned layer_end) = 0;
146 
156  virtual void PredictContribution(DMatrix* dmat, HostDeviceVector<float>* out_contribs,
157  bst_layer_t layer_begin, bst_layer_t layer_end,
158  bool approximate = false) = 0;
159 
161  bst_layer_t layer_begin, bst_layer_t layer_end,
162  bool approximate) = 0;
163 
171  [[nodiscard]] virtual std::vector<std::string> DumpModel(const FeatureMap& fmap, bool with_stats,
172  std::string format) const = 0;
173 
174  virtual void FeatureScore(std::string const& importance_type,
176  std::vector<bst_feature_t>* features,
177  std::vector<float>* scores) const = 0;
181  [[nodiscard]] virtual bool UseGPU() const = 0;
189  static GradientBooster* Create(const std::string& name, Context const* ctx,
190  LearnerModelParam const* learner_model_param);
191 };
192 
197  : public dmlc::FunctionRegEntryBase<
198  GradientBoosterReg,
199  std::function<GradientBooster*(LearnerModelParam const* learner_model_param,
200  Context const* ctx)> > {};
201 
214 #define XGBOOST_REGISTER_GBM(UniqueId, Name) \
215  static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \
216  __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \
217  ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name)
218 
219 } // namespace xgboost
220 #endif // XGBOOST_GBM_H_
Defines configuration macros and basic types for xgboost.
Internal data structured used by XGBoost during training.
Definition: data.h:505
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
interface of gradient boosting model.
Definition: gbm.h:37
virtual void Load(dmlc::Stream *fi)=0
load model from stream
~GradientBooster() override=default
virtual destructor
GradientBooster(Context const *ctx)
Definition: gbm.h:40
virtual std::int32_t BoostedRounds() const =0
Return number of boosted rounds.
virtual void PredictLeaf(DMatrix *dmat, HostDeviceVector< bst_float > *out_preds, unsigned layer_begin, unsigned layer_end)=0
predict the leaf index of each tree, the output will be nsample * ntree vector this is only valid in ...
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 PredictInteractionContributions(DMatrix *dmat, HostDeviceVector< float > *out_contribs, bst_layer_t layer_begin, bst_layer_t layer_end, bool approximate)=0
virtual void InplacePredict(std::shared_ptr< DMatrix >, float, PredictionCacheEntry *, bst_layer_t, bst_layer_t) const
Inplace prediction.
Definition: gbm.h:116
static GradientBooster * Create(const std::string &name, Context const *ctx, LearnerModelParam const *learner_model_param)
create a gradient booster from given name
virtual void FeatureScore(std::string const &importance_type, common::Span< int32_t const > trees, std::vector< bst_feature_t > *features, std::vector< float > *scores) const =0
virtual bool UseGPU() const =0
Whether the current booster uses GPU.
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...
virtual bool ModelFitted() const =0
Whether the model has already been trained. When tree booster is chosen, then returns true when there...
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 layer_begin, unsigned layer_end)=0
online prediction function, predict score for one instance at a time NOTE: use the batch prediction i...
virtual void PredictContribution(DMatrix *dmat, HostDeviceVector< float > *out_contribs, bst_layer_t layer_begin, bst_layer_t layer_end, bool approximate=false)=0
feature contributions to individual predictions; the output will be a vector of length (nfeats + 1) *...
virtual void DoBoost(DMatrix *p_fmat, linalg::Matrix< GradientPair > *in_gpair, PredictionCacheEntry *, ObjFunction const *obj)=0
perform update to the model(boosting)
Context const * ctx_
Definition: gbm.h:39
virtual void PredictBatch(DMatrix *dmat, PredictionCacheEntry *out_preds, bool training, bst_layer_t begin, bst_layer_t end)=0
Generate predictions for given feature matrix.
virtual void Slice(bst_layer_t, bst_layer_t, bst_layer_t, GradientBooster *, bool *) const
Slice a model using boosting index. The slice m:n indicates taking all trees that were fit during the...
Definition: gbm.h:69
interface of objective function
Definition: objective.h:29
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:422
A tensor storage. To use it for other functionality like slicing one needs to obtain a view first....
Definition: linalg.h:762
The input data structure of xgboost.
A device-and-host vector abstraction layer.
Defines the abstract interface for different components in XGBoost.
Core data structure for multi-target trees.
Definition: base.h:87
std::int32_t bst_layer_t
Type for indexing boosted layers.
Definition: base.h:117
Definition: model.h:31
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:133
Registry entry for tree updater.
Definition: gbm.h:200
Basic model parameters, used to describe the booster.
Definition: learner.h:292
Definition: model.h:17
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:30