xgboost
learner.h
前往此文件文档。
1 
8 #ifndef XGBOOST_LEARNER_H_
9 #define XGBOOST_LEARNER_H_
10 
11 #include <dmlc/io.h> // 用于 Serializable
12 #include <xgboost/base.h> // 用于 bst_feature_t, bst_target_t, bst_float, Args, GradientPair, ..
13 #include <xgboost/context.h> // 用于 Context
14 #include <xgboost/linalg.h> // 用于 Vector, VectorView
15 #include <xgboost/metric.h> // 用于 Metric
16 #include <xgboost/model.h> // 用于 Configurable, Model
17 #include <xgboost/span.h> // 用于 Span
18 #include <xgboost/task.h> // 用于 ObjInfo
19 
20 #include <algorithm> // 用于 max
21 #include <cstdint> // 用于 int32_t, uint32_t, uint8_t
22 #include <map> // 用于 map
23 #include <memory> // 用于 shared_ptr, unique_ptr
24 #include <string> // 用于 string
25 #include <utility> // 用于 move
26 #include <vector> // 用于 vector
27 
28 namespace xgboost {
29 class FeatureMap;
30 class Metric;
31 class GradientBooster;
32 class ObjFunction;
33 class DMatrix;
34 class Json;
35 struct XGBAPIThreadLocalEntry;
36 template <typename T>
37 class HostDeviceVector;
38 class CatContainer;
39 
40 enum class PredictionType : std::uint8_t { // NOLINT
41  kValue = 0,
42  kMargin = 1,
43  kContribution = 2,
45  kInteraction = 4,
47  kLeaf = 6
48 };
49 
66 class Learner : public Model, public Configurable, public dmlc::Serializable {
67  public
69  ~Learner() override;
73  virtual void Configure() = 0;
80  virtual void UpdateOneIter(std::int32_t iter, std::shared_ptr<DMatrix> train) = 0;
90  virtual void BoostOneIter(std::int32_t iter, std::shared_ptr<DMatrix> train,
91  linalg::Matrix<GradientPair>* in_gpair) = 0;
99  virtual std::string EvalOneIter(int iter,
100  const std::vector<std::shared_ptr<DMatrix>>& data_sets,
101  const std::vector<std::string>& data_names) = 0;
115  virtual void Predict(std::shared_ptr<DMatrix> data, bool output_margin,
116  HostDeviceVector<bst_float>* out_preds, bst_layer_t layer_begin,
117  bst_layer_t layer_end, bool training = false, bool pred_leaf = false,
118  bool pred_contribs = false, bool approx_contribs = false,
119  bool pred_interactions = false) = 0;
120 
131  virtual void InplacePredict(std::shared_ptr<DMatrix> p_m, PredictionType type, float missing,
132  HostDeviceVector<float>** out_preds, bst_layer_t layer_begin,
133  bst_layer_t layer_end) = 0;
134 
138  virtual void CalcFeatureScore(std::string const& importance_type,
140  std::vector<bst_feature_t>* features,
141  std::vector<float>* scores) = 0;
142 
143  /*
144  * \brief 从梯度提升器获取提升轮数。
145  */
146  virtual int32_t BoostedRounds() const = 0;
150  virtual std::uint32_t Groups() const = 0;
151 
152  void LoadModel(Json const& in) override = 0;
153  void SaveModel(Json* out) const override = 0;
154 
160  virtual void SetParams(Args const& args) = 0;
169  virtual void SetParam(const std::string& key, const std::string& value) = 0;
170 
175  virtual bst_feature_t GetNumFeature() const = 0;
176 
185  virtual void SetAttr(const std::string& key, const std::string& value) = 0;
193  virtual bool GetAttr(const std::string& key, std::string* out) const = 0;
199  virtual bool DelAttr(const std::string& key) = 0;
204  virtual std::vector<std::string> GetAttrNames() const = 0;
209  virtual void SetFeatureNames(std::vector<std::string> const& fn) = 0;
214  virtual void GetFeatureNames(std::vector<std::string>* fn) const = 0;
219  virtual void SetFeatureTypes(std::vector<std::string> const& ft) = 0;
224  virtual void GetFeatureTypes(std::vector<std::string>* ft) const = 0;
228  [[nodiscard]] virtual CatContainer const* Cats() const = 0;
239  virtual Learner* Slice(bst_layer_t begin, bst_layer_t end, bst_layer_t step,
240  bool* out_of_bound) = 0;
248  virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
249  bool with_stats,
250  std::string format) = 0;
251 
252  virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0;
256  virtual void Reset() = 0;
262  static Learner* Create(const std::vector<std::shared_ptr<DMatrix> >& cache_data);
266  virtual Context const* Ctx() const = 0;
271  virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0;
272 
273  protected
275  std::unique_ptr<ObjFunction> obj_;
277  std::unique_ptr<GradientBooster> gbm_;
279  std::vector<std::unique_ptr<Metric> > metrics_;
282 };
283 
284 struct LearnerModelParamLegacy;
285 
289 enum class MultiStrategy : std::int32_t {
290  kOneOutputPerTree = 0,
291  kMultiOutputTree = 1,
292 };
293 
298  private
306  linalg::Vector<float> base_score_;
307 
308  LearnerModelParam(LearnerModelParamLegacy const& user_param, ObjInfo t,
310 
311  public
319  std::uint32_t num_output_group{0};
328 
329  LearnerModelParam() = default;
330  LearnerModelParam(Context const* ctx, LearnerModelParamLegacy const& user_param,
332  // 此构造函数仅用于测试。
334  std::uint32_t n_groups, bst_target_t n_targets, MultiStrategy multi_strategy)
335  : base_score_{std::move(base_score)},
336  num_feature{n_features},
337  num_output_group{std::max(n_groups, n_targets)},
339 
342 
343  void Copy(LearnerModelParam const& that);
344  [[nodiscard]] bool IsVectorLeaf() const noexcept {
346  }
347  [[nodiscard]] bst_target_t OutputLength() const noexcept { return this->num_output_group; }
348  [[nodiscard]] bst_target_t LeafLength() const noexcept {
349  return this->IsVectorLeaf() ? this->OutputLength() : 1;
350  }
351 
352  /* \brief 此参数是否已使用 LearnerModelParamLegacy 初始化。 */
353  [[nodiscard]] bool Initialized() const { return num_feature != 0 && num_output_group != 0; }
354 };
355 
356 } // namespace xgboost
357 #endif // XGBOOST_LEARNER_H_
为 xgboost 定义配置宏和基本类型。
特征映射数据结构,用于辅助文本模型转储。TODO(tqchen) 考虑使其更轻量级...
定义: feature_map.h:22
表示JSON格式的数据结构。
Definition: json.h:392
用于训练和预测的 Learner 类。这是 XGBoost 训练的用户界面模块。
定义: learner.h:66
virtual std::vector< std::string > GetAttrNames() const =0
从助推器获取属性名称的向量。
virtual void UpdateOneIter(std::int32_t iter, std::shared_ptr< DMatrix > train)=0
使用指定的目标函数更新模型一次迭代。
virtual void InplacePredict(std::shared_ptr< DMatrix > p_m, PredictionType type, float missing, HostDeviceVector< float > **out_preds, bst_layer_t layer_begin, bst_layer_t layer_end)=0
原地预测。
virtual void SetParam(const std::string &key, const std::string &value)=0
为助推器设置参数。
std::vector< std::unique_ptr< Metric > > metrics_
用于评估模型的评估指标。
定义: learner.h:279
virtual void CalcFeatureScore(std::string const &importance_type, common::Span< int32_t const > trees, std::vector< bst_feature_t > *features, std::vector< float > *scores)=0
计算特征分数。输出请参阅 C API 中的文档。
virtual void Reset()=0
重置booster对象以释放用于训练的数据缓存。
virtual std::vector< std::string > DumpModel(const FeatureMap &fmap, bool with_stats, std::string format)=0
以请求的格式转储模型
virtual std::string EvalOneIter(int iter, const std::vector< std::shared_ptr< DMatrix >> &data_sets, const std::vector< std::string > &data_names)=0
使用配置的指标评估特定迭代的模型。
~Learner() override
虚析构函数
virtual XGBAPIThreadLocalEntry & GetThreadLocal() const =0
virtual const std::map< std::string, std::string > & GetConfigurationArguments() const =0
获取学习器当前存储的配置参数。
virtual void SetFeatureNames(std::vector< std::string > const &fn)=0
为当前助推器设置特征名称。
virtual Context const * Ctx() const =0
返回此助推器的上下文对象。
virtual void Configure()=0
根据设置的参数配置学习器。
virtual int32_t BoostedRounds() const =0
virtual bool DelAttr(const std::string &key)=0
从助推器中删除一个属性。
virtual bool GetAttr(const std::string &key, std::string *out) const =0
从助推器获取属性。该属性将与助推器一起保存。
virtual void SetParams(Args const &args)=0
一次性设置多个参数。
virtual void SetAttr(const std::string &key, const std::string &value)=0
为助推器设置附加属性。
std::unique_ptr< ObjFunction > obj_
目标函数
定义: learner.h:275
std::unique_ptr< GradientBooster > gbm_
模型使用的梯度助推器。
定义: learner.h:277
virtual void GetFeatureNames(std::vector< std::string > *fn) const =0
获取当前助推器的特征名称。
void LoadModel(Json const &in) override=0
从JSON对象加载模型
virtual void SetFeatureTypes(std::vector< std::string > const &ft)=0
为当前助推器设置特征类型。
void SaveModel(Json *out) const override=0
将模型配置保存到JSON对象
static Learner * Create(const std::vector< std::shared_ptr< DMatrix > > &cache_data)
创建一个新的学习器实例。
virtual Learner * Slice(bst_layer_t begin, bst_layer_t end, bst_layer_t step, bool *out_of_bound)=0
切片模型。
virtual bst_feature_t GetNumFeature() const =0
获取助推器的特征数量。
Context ctx_
训练参数。
定义: learner.h:281
virtual std::uint32_t Groups() const =0
从模型中获取输出组的数量。
virtual void BoostOneIter(std::int32_t iter, std::shared_ptr< DMatrix > train, linalg::Matrix< GradientPair > *in_gpair)=0
使用 in_gpair 进行自定义梯度提升。
virtual void Predict(std::shared_ptr< DMatrix > data, bool output_margin, HostDeviceVector< bst_float > *out_preds, bst_layer_t layer_begin, bst_layer_t layer_end, bool training=false, bool pred_leaf=false, bool pred_contribs=false, bool approx_contribs=false, bool pred_interactions=false)=0
给定模型进行预测。
virtual CatContainer const * Cats() const =0
类别的 Getter。
virtual void GetFeatureTypes(std::vector< std::string > *ft) const =0
获取当前助推器的特征类型。
span类实现,基于ISO++20 span<T>。接口应相同。
Definition: span.h:431
具有静态类型和维度的张量视图。它实现了索引和切片。
定义: linalg.h:277
一个张量存储。要将其用于切片等其他功能,首先需要获取一个视图...
定义: linalg.h:745
线性代数相关工具。
xgboost 中支持的评估指标函数的接口。
定义 XGBoost 中不同组件的抽象接口。
Definition: intrusive_ptr.h:207
集成目标、gbm和评估的学习器接口。这是用户面临的XGB...
Definition: base.h:97
std::vector< std::pair< std::string, std::string > > Args
定义: base.h:324
PredictionType
定义: learner.h:40
std::uint32_t bst_target_t
用于索引输出目标的类型。
定义: base.h:127
std::int32_t bst_layer_t
用于索引提升层的类型。
定义: base.h:131
std::uint32_t bst_feature_t
数据列(特征)索引的类型。
Definition: base.h:107
MultiStrategy
构建多目标模型的策略。
定义: learner.h:289
定义: model.h:28
XGBoost的运行时上下文。包含线程和设备等信息。
Definition: context.h:133
设备序号的类型。该类型被打包成32位,以便在查看类型(如lin...)时高效使用
Definition: context.h:34
基本的模型参数,用于描述助推器。
定义: learner.h:297
std::uint32_t num_output_group
类别或目标数量。
定义: learner.h:319
LearnerModelParam(Context const *ctx, LearnerModelParamLegacy const &user_param, linalg::Vector< float > base_score, ObjInfo t, MultiStrategy multi_strategy)
bool Initialized() const
定义: learner.h:353
MultiStrategy multi_strategy
构建多目标模型的策略。
定义: learner.h:327
linalg::VectorView< float const > BaseScore(Context const *ctx) const
LearnerModelParam(bst_feature_t n_features, linalg::Vector< float > base_score, std::uint32_t n_groups, bst_target_t n_targets, MultiStrategy multi_strategy)
定义: learner.h:333
bst_target_t OutputLength() const noexcept
定义: learner.h:347
linalg::VectorView< float const > BaseScore(DeviceOrd device) const
void Copy(LearnerModelParam const &that)
bst_target_t LeafLength() const noexcept
定义: learner.h:348
ObjInfo task
当前任务,由目标决定。
定义: learner.h:323
bool IsVectorLeaf() const noexcept
定义: learner.h:344
bst_feature_t num_feature
特征数量。
定义: learner.h:315
定义: model.h:14
由目标返回的结构体,用于确定当前任务。此结构体不被任何算法使用……
定义: task.h:24
@ kRegression
定义: task.h:27