xgboost
gbm.h
转到此文件的文档。
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 <string>
19 #include <functional>
20 #include <memory>
21 
22 namespace xgboost {
23 
24 class Json;
25 class FeatureMap;
26 class ObjFunction;
27 
28 struct Context;
29 struct LearnerModelParam;
30 struct PredictionCacheEntry;
31 
35 class GradientBooster : public Model, public Configurable {
36  protected
37  Context const* ctx_;
38  explicit GradientBooster(Context const* ctx) : ctx_{ctx} {}
39 
40  public
42  ~GradientBooster() override = default;
49  virtual void Configure(Args const& cfg) = 0;
54  virtual void Load(dmlc::Stream* fi) = 0;
59  virtual void Save(dmlc::Stream* fo) const = 0;
67  virtual void Slice(bst_layer_t /*begin*/, bst_layer_t /*end*/, bst_layer_t /*step*/,
68  GradientBooster* /*out*/, bool* /*out_of_bound*/) const {
69  LOG(FATAL) << "Slice is not supported by the current booster.";
70  }
74  [[nodiscard]] virtual std::int32_t BoostedRounds() const = 0;
79  [[nodiscard]] virtual bool ModelFitted() const = 0;
89  virtual void DoBoost(DMatrix* p_fmat, linalg::Matrix<GradientPair>* in_gpair,
90  PredictionCacheEntry*, ObjFunction const* obj) = 0;
91 
102  virtual void PredictBatch(DMatrix* dmat, PredictionCacheEntry* out_preds, bool training,
103  bst_layer_t begin, bst_layer_t end) = 0;
104 
114  virtual void InplacePredict(std::shared_ptr<DMatrix>, float, PredictionCacheEntry*, bst_layer_t,
115  bst_layer_t) const {
116  LOG(FATAL) << "Inplace predict is not supported by the current booster.";
117  }
126  virtual void PredictLeaf(DMatrix *dmat,
127  HostDeviceVector<bst_float> *out_preds,
128  unsigned layer_begin, unsigned layer_end) = 0;
129 
139  virtual void PredictContribution(DMatrix* dmat, HostDeviceVector<float>* out_contribs,
140  bst_layer_t layer_begin, bst_layer_t layer_end,
141  bool approximate = false) = 0;
142 
144  bst_layer_t layer_begin, bst_layer_t layer_end,
145  bool approximate) = 0;
146 
154  [[nodiscard]] virtual std::vector<std::string> DumpModel(const FeatureMap& fmap, bool with_stats,
155  std::string format) const = 0;
156 
157  virtual void FeatureScore(std::string const& importance_type,
159  std::vector<bst_feature_t>* features,
160  std::vector<float>* scores) const = 0;
164  [[nodiscard]] virtual bool UseGPU() const = 0;
172  static GradientBooster* Create(const std::string& name, Context const* ctx,
173  LearnerModelParam const* learner_model_param);
174 };
175 
180  : public dmlc::FunctionRegEntryBase<
181  GradientBoosterReg,
182  std::function<GradientBooster*(LearnerModelParam const* learner_model_param,
183  Context const* ctx)> > {};
184 
197 #define XGBOOST_REGISTER_GBM(UniqueId, Name) \
198  static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \
199  __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \
200  ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name)
201 
202 } // namespace xgboost
203 #endif // XGBOOST_GBM_H_
定义了xgboost的配置宏和基本类型。
XGBoost内部使用的数据结构,用于保存所有外部数据。
定义: data.h:549
特征映射数据结构,用于辅助文本模型导出。TODO(tqchen) 考虑使其更加轻量...
定义: feature_map.h:22
梯度提升模型的接口。
定义: gbm.h:35
virtual void Load(dmlc::Stream *fi)=0
从流中加载模型
~GradientBooster() override=default
虚析构函数
GradientBooster(Context const *ctx)
定义: gbm.h:38
virtual std::int32_t BoostedRounds() const =0
返回提升轮数。
virtual void PredictLeaf(DMatrix *dmat, HostDeviceVector< bst_float > *out_preds, unsigned layer_begin, unsigned layer_end)=0
预测每棵树的叶子索引,输出将是一个 nsample * ntree 向量,这仅在 ...
virtual std::vector< std::string > DumpModel(const FeatureMap &fmap, bool with_stats, std::string format) const =0
以请求的格式导出模型
virtual void PredictInteractionContributions(DMatrix *dmat, HostDeviceVector< float > *out_contribs, bst_layer_t layer_begin, bst_layer_t layer_end, bool approximate)=0
virtual void Configure(Args const &cfg)=0
设置梯度提升的配置。用户必须在 InitModel 和 Training 之前调用一次 configure。
virtual void InplacePredict(std::shared_ptr< DMatrix >, float, PredictionCacheEntry *, bst_layer_t, bst_layer_t) const
原地预测。
定义: gbm.h:114
static GradientBooster * Create(const std::string &name, Context const *ctx, LearnerModelParam const *learner_model_param)
从给定名称创建一个梯度提升器
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
当前提升器是否使用 GPU。
virtual bool ModelFitted() const =0
模型是否已训练。当选择树提升器时,如果在...
virtual void Save(dmlc::Stream *fo) const =0
将模型保存到流。
virtual void PredictContribution(DMatrix *dmat, HostDeviceVector< float > *out_contribs, bst_layer_t layer_begin, bst_layer_t layer_end, bool approximate=false)=0
特征对个体预测的贡献;输出将是一个长度为 (nfeats + 1) *...
virtual void DoBoost(DMatrix *p_fmat, linalg::Matrix< GradientPair > *in_gpair, PredictionCacheEntry *, ObjFunction const *obj)=0
对模型执行更新(提升)
Context const * ctx_
定义: gbm.h:37
virtual void PredictBatch(DMatrix *dmat, PredictionCacheEntry *out_preds, bool training, bst_layer_t begin, bst_layer_t end)=0
为给定的特征矩阵生成预测。
virtual void Slice(bst_layer_t, bst_layer_t, bst_layer_t, GradientBooster *, bool *) const
使用提升索引切片模型。切片 m:n 表示获取在...
定义: gbm.h:67
目标函数接口
定义: objective.h:27
span 类实现,基于 ISO++20 span<T>。接口应相同。
定义: span.h:431
张量存储。要将其用于切片等其他功能,首先需要获取一个视图。
定义: linalg.h:762
xgboost的输入数据结构。
设备和主机向量抽象层。
定义了 XGBoost 中不同组件的抽象接口。
多目标树的核心数据结构。
定义: base.h:89
std::vector< std::pair< std::string, std::string > > Args
定义: base.h:316
std::int32_t bst_layer_t
用于索引提升层的类型。
定义: base.h:123
定义: model.h:31
XGBoost 的运行时上下文。包含线程和设备等信息。
定义: context.h:133
树更新器的注册表项。
定义: gbm.h:183
基本模型参数,用于描述提升器。
定义: learner.h:296
定义: model.h:17
包含指向输入矩阵和相关缓存预测的指针。
定义: predictor.h:29