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 <memory>
22 
23 namespace xgboost {
24 
25 class Json;
26 class FeatureMap;
27 class ObjFunction;
28 
29 struct GenericParameter;
30 struct LearnerModelParam;
31 
35 class GradientBooster : public Model, public Configurable {
36  protected:
38 
39  public:
41  virtual ~GradientBooster() = default;
48  virtual void Configure(const std::vector<std::pair<std::string, std::string> >& cfg) = 0;
53  virtual void Load(dmlc::Stream* fi) = 0;
58  virtual void Save(dmlc::Stream* fo) const = 0;
64  virtual bool AllowLazyCheckPoint() const {
65  return false;
66  }
74  virtual void DoBoost(DMatrix* p_fmat,
76  ObjFunction* obj = nullptr) = 0;
77 
85  virtual void PredictBatch(DMatrix* dmat,
86  HostDeviceVector<bst_float>* out_preds,
87  bool training,
88  unsigned ntree_limit = 0) = 0;
100  virtual void PredictInstance(const SparsePage::Inst& inst,
101  std::vector<bst_float>* out_preds,
102  unsigned ntree_limit = 0) = 0;
111  virtual void PredictLeaf(DMatrix* dmat,
112  std::vector<bst_float>* out_preds,
113  unsigned ntree_limit = 0) = 0;
114 
126  virtual void PredictContribution(DMatrix* dmat,
127  std::vector<bst_float>* out_contribs,
128  unsigned ntree_limit = 0,
129  bool approximate = false, int condition = 0,
130  unsigned condition_feature = 0) = 0;
131 
132  virtual void PredictInteractionContributions(DMatrix* dmat,
133  std::vector<bst_float>* out_contribs,
134  unsigned ntree_limit, bool approximate) = 0;
135 
143  virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
144  bool with_stats,
145  std::string format) const = 0;
149  virtual bool UseGPU() const = 0;
158  static GradientBooster* Create(
159  const std::string& name,
160  GenericParameter const* generic_param,
161  LearnerModelParam const* learner_model_param,
162  const std::vector<std::shared_ptr<DMatrix> >& cache_mats);
163 
164  static void AssertGPUSupport() {
165 #ifndef XGBOOST_USE_CUDA
166  LOG(FATAL) << "XGBoost version not compiled with GPU support.";
167 #endif // XGBOOST_USE_CUDA
168  }
169 };
170 
175  : public dmlc::FunctionRegEntryBase<
176  GradientBoosterReg,
177  std::function<GradientBooster* (const std::vector<std::shared_ptr<DMatrix> > &cached_mats,
178  LearnerModelParam const* learner_model_param)> > {
179 };
180 
193 #define XGBOOST_REGISTER_GBM(UniqueId, Name) \
194  static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \
195  __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \
196  ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name)
197 
198 } // namespace xgboost
199 #endif // XGBOOST_GBM_H_
Definition: learner.h:199
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 ~GradientBooster()=default
virtual destructor
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:85
virtual void PredictInteractionContributions(DMatrix *dmat, std::vector< bst_float > *out_contribs, unsigned ntree_limit, bool approximate)=0
The input data structure of xgboost.
static void AssertGPUSupport()
Definition: gbm.h:164
Definition: generic_parameters.h:14
Defines the abstract interface for different components in XGBoost.
Internal data structured used by XGBoost during training. There are two ways to create a customized D...
Definition: data.h:428
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:115
Registry entry for tree updater.
Definition: gbm.h:174
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...
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:64
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
interface of objective function
Definition: objective.h:25
namespace of xgboost
Definition: base.h:102
static GradientBooster * Create(const std::string &name, GenericParameter const *generic_param, LearnerModelParam const *learner_model_param, const std::vector< std::shared_ptr< DMatrix > > &cache_mats)
create a gradient booster from given name
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, ObjFunction *obj=nullptr)=0
perform update to the model(boosting)
interface of gradient boosting model.
Definition: gbm.h:35
GenericParameter const * generic_param_
Definition: gbm.h:37
virtual void PredictBatch(DMatrix *dmat, HostDeviceVector< bst_float > *out_preds, bool training, unsigned ntree_limit=0)=0
generate predictions for given feature matrix