xgboost
learner.h
Go to the documentation of this file.
1 
8 #ifndef XGBOOST_LEARNER_H_
9 #define XGBOOST_LEARNER_H_
10 
11 #include <dmlc/io.h> // for Serializable
12 #include <xgboost/base.h> // for bst_feature_t, bst_target_t, bst_float, Args, GradientPair, ..
13 #include <xgboost/context.h> // for Context
14 #include <xgboost/gradient.h> // for GradientContainer
15 #include <xgboost/linalg.h> // for Vector, VectorView
16 #include <xgboost/metric.h> // for Metric
17 #include <xgboost/model.h> // for Configurable, Model
18 #include <xgboost/span.h> // for Span
19 #include <xgboost/task.h> // for ObjInfo
20 
21 #include <algorithm> // for max
22 #include <cstdint> // for int32_t, uint32_t, uint8_t
23 #include <map> // for map
24 #include <memory> // for shared_ptr, unique_ptr
25 #include <string> // for string
26 #include <utility> // for move
27 #include <vector> // for vector
28 
29 namespace xgboost {
30 class FeatureMap;
31 class Metric;
32 class GradientBooster;
33 class ObjFunction;
34 class DMatrix;
35 class Json;
36 struct XGBAPIThreadLocalEntry;
37 template <typename T>
38 class HostDeviceVector;
39 class CatContainer;
40 
41 enum class PredictionType : std::uint8_t { // NOLINT
42  kValue = 0,
43  kMargin = 1,
44  kContribution = 2,
46  kInteraction = 4,
48  kLeaf = 6
49 };
50 
67 class Learner : public Model, public Configurable, public dmlc::Serializable {
68  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  GradientContainer* 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 Get number of boosted rounds from gradient booster.
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  // This ctor is only used by tests.
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 Whether this parameter is initialized with LearnerModelParamLegacy. */
353  [[nodiscard]] bool Initialized() const { return num_feature != 0 && num_output_group != 0; }
354 };
355 
356 } // namespace xgboost
357 #endif // XGBOOST_LEARNER_H_
Defines configuration macros and basic types for xgboost.
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
Data structure representing JSON format.
Definition: json.h:396
Learner class that does training and prediction. This is the user facing module of xgboost training....
Definition: learner.h:67
virtual std::vector< std::string > GetAttrNames() const =0
Get a vector of attribute names from the booster.
virtual void UpdateOneIter(std::int32_t iter, std::shared_ptr< DMatrix > train)=0
update the model for one iteration With the specified objective function.
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
Inplace prediction.
virtual void SetParam(const std::string &key, const std::string &value)=0
Set parameter for booster.
std::vector< std::unique_ptr< Metric > > metrics_
The evaluation metrics used to evaluate the model.
Definition: 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
Calculate feature score. See doc in C API for outputs.
virtual void Reset()=0
Reset the booster object to release data caches used for training.
virtual std::vector< std::string > DumpModel(const FeatureMap &fmap, bool with_stats, std::string format)=0
dump the model in the requested format
virtual std::string EvalOneIter(int iter, const std::vector< std::shared_ptr< DMatrix >> &data_sets, const std::vector< std::string > &data_names)=0
evaluate the model for specific iteration using the configured metrics.
~Learner() override
virtual XGBAPIThreadLocalEntry & GetThreadLocal() const =0
virtual const std::map< std::string, std::string > & GetConfigurationArguments() const =0
Get configuration arguments currently stored by the learner.
virtual void SetFeatureNames(std::vector< std::string > const &fn)=0
Set the feature names for current booster.
virtual void BoostOneIter(std::int32_t iter, std::shared_ptr< DMatrix > train, GradientContainer *in_gpair)=0
Do customized gradient boosting with in_gpair.
virtual Context const * Ctx() const =0
Return the context object of this Booster.
virtual void Configure()=0
Configure Learner based on set parameters.
virtual int32_t BoostedRounds() const =0
virtual bool DelAttr(const std::string &key)=0
Delete an attribute from the booster.
virtual bool GetAttr(const std::string &key, std::string *out) const =0
Get attribute from the booster. The property will be saved along the booster.
virtual void SetParams(Args const &args)=0
Set multiple parameters at once.
virtual void SetAttr(const std::string &key, const std::string &value)=0
Set additional attribute to the Booster.
std::unique_ptr< ObjFunction > obj_
objective function
Definition: learner.h:275
std::unique_ptr< GradientBooster > gbm_
The gradient booster used by the model.
Definition: learner.h:277
virtual void GetFeatureNames(std::vector< std::string > *fn) const =0
Get the feature names for current booster.
void LoadModel(Json const &in) override=0
load the model from a JSON object
virtual void SetFeatureTypes(std::vector< std::string > const &ft)=0
Set the feature types for current booster.
void SaveModel(Json *out) const override=0
saves the model config to a JSON object
static Learner * Create(const std::vector< std::shared_ptr< DMatrix > > &cache_data)
Create a new instance of learner.
virtual Learner * Slice(bst_layer_t begin, bst_layer_t end, bst_layer_t step, bool *out_of_bound)=0
Slice the model.
virtual bst_feature_t GetNumFeature() const =0
Get the number of features of the booster.
Context ctx_
Training parameter.
Definition: learner.h:281
virtual std::uint32_t Groups() const =0
Get the number of output groups from the model.
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
get prediction given the model.
virtual CatContainer const * Cats() const =0
Getter for categories.
virtual void GetFeatureTypes(std::vector< std::string > *ft) const =0
Get the feature types for current booster.
span class implementation, based on ISO++20 span<T>. The interface should be the same.
Definition: span.h:435
A tensor view with static type and dimension. It implements indexing and slicing.
Definition: linalg.h:278
A tensor storage. To use it for other functionality like slicing one needs to obtain a view first....
Definition: linalg.h:760
Linear algebra related utilities.
interface of evaluation metric function supported in xgboost.
Defines the abstract interface for different components in XGBoost.
Definition: intrusive_ptr.h:207
Learner interface that integrates objective, gbm and evaluation together. This is the user facing XGB...
Definition: base.h:89
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:306
PredictionType
Definition: learner.h:41
std::uint32_t bst_target_t
Type for indexing into output targets.
Definition: base.h:119
std::int32_t bst_layer_t
Type for indexing boosted layers.
Definition: base.h:123
std::uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:99
MultiStrategy
Strategy for building multi-target models.
Definition: learner.h:289
Definition: model.h:28
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:133
A type for device ordinal. The type is packed into 32-bit for efficient use in viewing types like lin...
Definition: context.h:34
Container for gradient produced by objective.
Definition: gradient.h:16
Basic model parameters, used to describe the booster.
Definition: learner.h:297
std::uint32_t num_output_group
The number of classes or targets.
Definition: learner.h:319
LearnerModelParam(Context const *ctx, LearnerModelParamLegacy const &user_param, linalg::Vector< float > base_score, ObjInfo t, MultiStrategy multi_strategy)
bool Initialized() const
Definition: learner.h:353
MultiStrategy multi_strategy
Strategy for building multi-target models.
Definition: 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)
Definition: learner.h:333
bst_target_t OutputLength() const noexcept
Definition: learner.h:347
linalg::VectorView< float const > BaseScore(DeviceOrd device) const
void Copy(LearnerModelParam const &that)
bst_target_t LeafLength() const noexcept
Definition: learner.h:348
ObjInfo task
Current task, determined by objective.
Definition: learner.h:323
bool IsVectorLeaf() const noexcept
Definition: learner.h:344
bst_feature_t num_feature
The number of features.
Definition: learner.h:315
Definition: model.h:14
A struct returned by objective, which determines task at hand. The struct is not used by any algorith...
Definition: task.h:24
@ kRegression
Definition: task.h:27