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 class CatContainer;
28 
29 struct Context;
30 struct LearnerModelParam;
31 struct PredictionCacheEntry;
32 
36 class GradientBooster : public Model, public Configurable {
37  protected
38  Context const* ctx_;
39  explicit GradientBooster(Context const* ctx) : ctx_{ctx} {}
40 
41  public
43  ~GradientBooster() override = default;
50  virtual void Configure(Args const& cfg) = 0;
51 
59  virtual void Slice(bst_layer_t /*begin*/, bst_layer_t /*end*/, bst_layer_t /*step*/,
60  GradientBooster* /*out*/, bool* /*out_of_bound*/) const {
61  LOG(FATAL) << "当前增强器不支持切片操作。";
62  }
66  [[nodiscard]] virtual std::int32_t BoostedRounds() const = 0;
71  [[nodiscard]] virtual bool ModelFitted() const = 0;
81  virtual void DoBoost(DMatrix* p_fmat, linalg::Matrix<GradientPair>* in_gpair,
82  PredictionCacheEntry*, ObjFunction const* obj) = 0;
83 
94  virtual void PredictBatch(DMatrix* dmat, PredictionCacheEntry* out_preds, bool training,
95  bst_layer_t begin, bst_layer_t end) = 0;
96 
106  virtual void InplacePredict(std::shared_ptr<DMatrix>, float, PredictionCacheEntry*, bst_layer_t,
107  bst_layer_t) const {
108  LOG(FATAL) << "当前增强器不支持原地预测。";
109  }
118  virtual void PredictLeaf(DMatrix *dmat,
119  HostDeviceVector<bst_float> *out_preds,
120  unsigned layer_begin, unsigned layer_end) = 0;
121 
131  virtual void PredictContribution(DMatrix* dmat, HostDeviceVector<float>* out_contribs,
132  bst_layer_t layer_begin, bst_layer_t layer_end,
133  bool approximate = false) = 0;
134 
136  bst_layer_t layer_begin, bst_layer_t layer_end,
137  bool approximate) = 0;
138 
146  [[nodiscard]] virtual std::vector<std::string> DumpModel(const FeatureMap& fmap, bool with_stats,
147  std::string format) const = 0;
148 
149  virtual void FeatureScore(std::string const& importance_type,
151  std::vector<bst_feature_t>* features,
152  std::vector<float>* scores) const = 0;
156  [[nodiscard]] virtual CatContainer const* Cats() const {
157  LOG(FATAL) << "当前增强器不支持获取类别。";
158  return nullptr;
159  }
167  static GradientBooster* Create(const std::string& name, Context const* ctx,
168  LearnerModelParam const* learner_model_param);
169 };
170 
175  : public dmlc::FunctionRegEntryBase<
176  GradientBoosterReg,
177  std::function<GradientBooster*(LearnerModelParam const* learner_model_param,
178  Context const* ctx)> > {};
179 
192 #define XGBOOST_REGISTER_GBM(UniqueId, Name) \
193  static DMLC_ATTRIBUTE_UNUSED ::xgboost::GradientBoosterReg & \
194  __make_ ## GradientBoosterReg ## _ ## UniqueId ## __ = \
195  ::dmlc::Registry< ::xgboost::GradientBoosterReg>::Get()->__REGISTER__(Name)
196 
197 } // namespace xgboost
198 #endif // XGBOOST_GBM_H_
为 xgboost 定义配置宏和基本类型。
内部数据结构,由XGBoost用于保存所有外部数据。
Definition: data.h:573
特征映射数据结构,用于辅助文本模型转储。TODO(tqchen) 考虑使其更轻量级...
定义: feature_map.h:22
梯度提升模型的接口。
定义: gbm.h:36
~GradientBooster() override=default
虚析构函数
GradientBooster(Context const *ctx)
定义: gbm.h:39
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 和 Traini... 之前调用一次 configure。
virtual void InplacePredict(std::shared_ptr< DMatrix >, float, PredictionCacheEntry *, bst_layer_t, bst_layer_t) const
原地预测。
定义: gbm.h:106
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 ModelFitted() const =0
模型是否已经训练。当选择树增强器时,当...时返回 true。
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:38
virtual CatContainer const * Cats() const
类别的 Getter。
定义: gbm.h:156
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
使用 boosting 索引对模型进行切片。切片 m:n 表示获取在...期间拟合的所有树。
定义: gbm.h:59
目标函数的接口。
定义: objective.h:28
span类实现,基于ISO++20 span<T>。接口应相同。
Definition: span.h:431
一个张量存储。要将其用于切片等其他功能,首先需要获取一个视图...
定义: linalg.h:745
xgboost 的输入数据结构。
设备与主机向量抽象层。
定义 XGBoost 中不同组件的抽象接口。
集成目标、gbm和评估的学习器接口。这是用户面临的XGB...
Definition: base.h:97
std::vector< std::pair< std::string, std::string > > Args
定义: base.h:324
std::int32_t bst_layer_t
用于索引提升层的类型。
定义: base.h:131
定义: model.h:28
XGBoost的运行时上下文。包含线程和设备等信息。
Definition: context.h:133
树更新器的注册表项。
定义: gbm.h:178
基本的模型参数,用于描述助推器。
定义: learner.h:297
定义: model.h:14
包含指向输入矩阵和相关缓存预测的指针。
定义: predictor.h:30