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  explicit GradientBooster(GenericParameter const* ctx) : ctx_{ctx} {}
43 
44  public:
46  ~GradientBooster() override = default;
53  virtual void Configure(const std::vector<std::pair<std::string, std::string> >& cfg) = 0;
58  virtual void Load(dmlc::Stream* fi) = 0;
63  virtual void Save(dmlc::Stream* fo) const = 0;
71  virtual void Slice(int32_t layer_begin, int32_t layer_end, int32_t step,
72  GradientBooster *out, bool* out_of_bound) const {
73  LOG(FATAL) << "Slice is not supported by current booster.";
74  }
80  virtual bool AllowLazyCheckPoint() const {
81  return false;
82  }
85  virtual int32_t BoostedRounds() const = 0;
93  virtual void DoBoost(DMatrix* p_fmat,
96 
106  virtual void PredictBatch(DMatrix* dmat,
107  PredictionCacheEntry* out_preds,
108  bool training,
109  unsigned layer_begin,
110  unsigned layer_end) = 0;
111 
121  virtual void InplacePredict(dmlc::any const &, std::shared_ptr<DMatrix>, float,
123  uint32_t,
124  uint32_t) const {
125  LOG(FATAL) << "Inplace predict is not supported by current booster.";
126  }
139  virtual void PredictInstance(const SparsePage::Inst& inst,
140  std::vector<bst_float>* out_preds,
141  unsigned layer_begin, unsigned layer_end) = 0;
150  virtual void PredictLeaf(DMatrix *dmat,
151  HostDeviceVector<bst_float> *out_preds,
152  unsigned layer_begin, unsigned layer_end) = 0;
153 
165  virtual void PredictContribution(DMatrix* dmat,
166  HostDeviceVector<bst_float>* out_contribs,
167  unsigned layer_begin, unsigned layer_end,
168  bool approximate = false, int condition = 0,
169  unsigned condition_feature = 0) = 0;
170 
171  virtual void PredictInteractionContributions(
172  DMatrix *dmat, HostDeviceVector<bst_float> *out_contribs,
173  unsigned layer_begin, unsigned layer_end, bool approximate) = 0;
174 
182  virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
183  bool with_stats,
184  std::string format) const = 0;
185 
186  virtual void FeatureScore(std::string const& importance_type,
188  std::vector<bst_feature_t>* features,
189  std::vector<float>* scores) const = 0;
193  virtual bool UseGPU() const = 0;
201  static GradientBooster* Create(
202  const std::string& name,
203  GenericParameter const* generic_param,
204  LearnerModelParam const* learner_model_param);
205 };
206 
211  : public dmlc::FunctionRegEntryBase<
212  GradientBoosterReg,
213  std::function<GradientBooster*(LearnerModelParam const* learner_model_param,
214  GenericParameter const* ctx)> > {};
215 
228 #define XGBOOST_REGISTER_GBM(UniqueId, Name) \
229  static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \
230  __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \
231  ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name)
232 
233 } // namespace xgboost
234 #endif // XGBOOST_GBM_H_
xgboost::PredictionCacheEntry
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:35
model.h
Defines the abstract interface for different components in XGBoost.
xgboost::GenericParameter
Definition: generic_parameters.h:15
xgboost::GradientBooster::PredictContribution
virtual void PredictContribution(DMatrix *dmat, HostDeviceVector< bst_float > *out_contribs, unsigned layer_begin, unsigned layer_end, 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) *...
xgboost::HostDeviceVector
Definition: host_device_vector.h:86
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::GradientBooster::~GradientBooster
~GradientBooster() override=default
virtual destructor
xgboost::GradientBooster::GradientBooster
GradientBooster(GenericParameter const *ctx)
Definition: gbm.h:42
xgboost::GradientBooster::Load
virtual void Load(dmlc::Stream *fi)=0
load model from stream
base.h
defines configuration macros of xgboost.
xgboost::GradientBooster::Slice
virtual void Slice(int32_t layer_begin, int32_t layer_end, int32_t step, GradientBooster *out, bool *out_of_bound) const
Slice a model using boosting index. The slice m:n indicates taking all trees that were fit during the...
Definition: gbm.h:71
xgboost::GradientBooster
interface of gradient boosting model.
Definition: gbm.h:39
xgboost::DMatrix
Internal data structured used by XGBoost during training.
Definition: data.h:475
xgboost::LearnerModelParam
Definition: learner.h:309
xgboost::FeatureMap
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
xgboost::GradientBooster::DoBoost
virtual void DoBoost(DMatrix *p_fmat, HostDeviceVector< GradientPair > *in_gpair, PredictionCacheEntry *)=0
perform update to the model(boosting)
xgboost::Model
Definition: model.h:17
xgboost::GradientBooster::Configure
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...
xgboost::GradientBooster::PredictInstance
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...
xgboost::GradientBooster::PredictInteractionContributions
virtual void PredictInteractionContributions(DMatrix *dmat, HostDeviceVector< bst_float > *out_contribs, unsigned layer_begin, unsigned layer_end, bool approximate)=0
xgboost::Configurable
Definition: model.h:31
xgboost::GradientBooster::FeatureScore
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
xgboost::common::Span
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:148
xgboost::GradientBooster::UseGPU
virtual bool UseGPU() const =0
Whether the current booster uses GPU.
xgboost::GradientBooster::PredictBatch
virtual void PredictBatch(DMatrix *dmat, PredictionCacheEntry *out_preds, bool training, unsigned layer_begin, unsigned layer_end)=0
generate predictions for given feature matrix
xgboost::GradientBooster::InplacePredict
virtual void InplacePredict(dmlc::any const &, std::shared_ptr< DMatrix >, float, PredictionCacheEntry *, uint32_t, uint32_t) const
Inplace prediction.
Definition: gbm.h:121
data.h
The input data structure of xgboost.
xgboost::GradientBooster::DumpModel
virtual std::vector< std::string > DumpModel(const FeatureMap &fmap, bool with_stats, std::string format) const =0
dump the model in the requested format
xgboost::GradientBooster::PredictLeaf
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 ...
xgboost::GradientBooster::BoostedRounds
virtual int32_t BoostedRounds() const =0
Return number of boosted rounds.
xgboost::GradientBoosterReg
Registry entry for tree updater.
Definition: gbm.h:210
xgboost::GradientBooster::ctx_
GenericParameter const * ctx_
Definition: gbm.h:41
xgboost::GradientBooster::AllowLazyCheckPoint
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:80
xgboost::GradientBooster::Create
static GradientBooster * Create(const std::string &name, GenericParameter const *generic_param, LearnerModelParam const *learner_model_param)
create a gradient booster from given name
xgboost::GradientBooster::Save
virtual void Save(dmlc::Stream *fo) const =0
save model to stream.
xgboost
namespace of xgboost
Definition: base.h:110