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/any.h>
12 #include <rabit/rabit.h>
13 #include <xgboost/base.h>
14 #include <xgboost/feature_map.h>
15 #include <xgboost/predictor.h>
18 #include <xgboost/model.h>
19 
20 #include <utility>
21 #include <map>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 namespace xgboost {
27 
28 class Metric;
29 class GradientBooster;
30 class ObjFunction;
31 class DMatrix;
32 class Json;
33 
37  std::string ret_str;
39  std::vector<std::string> ret_vec_str;
41  std::vector<const char *> ret_vec_charp;
43  std::vector<bst_float> ret_vec_float;
45  std::vector<GradientPair> tmp_gpair;
47 };
48 
49 
66 class Learner : public Model, public Configurable, public rabit::Serializable {
67  public:
69  ~Learner() override;
73  virtual void Configure() = 0;
80  virtual void UpdateOneIter(int iter, std::shared_ptr<DMatrix> train) = 0;
88  virtual void BoostOneIter(int iter,
89  std::shared_ptr<DMatrix> train,
90  HostDeviceVector<GradientPair>* in_gpair) = 0;
98  virtual std::string EvalOneIter(int iter,
99  const std::vector<std::shared_ptr<DMatrix>>& data_sets,
100  const std::vector<std::string>& data_names) = 0;
114  virtual void Predict(std::shared_ptr<DMatrix> data,
115  bool output_margin,
116  HostDeviceVector<bst_float> *out_preds,
117  unsigned ntree_limit = 0,
118  bool training = false,
119  bool pred_leaf = false,
120  bool pred_contribs = false,
121  bool approx_contribs = false,
122  bool pred_interactions = false) = 0;
123 
134  virtual void InplacePredict(dmlc::any const& x, std::string const& type,
135  float missing,
136  HostDeviceVector<bst_float> **out_preds,
137  uint32_t layer_begin = 0, uint32_t layer_end = 0) = 0;
138 
139  void LoadModel(Json const& in) override = 0;
140  void SaveModel(Json* out) const override = 0;
141 
142  virtual void LoadModel(dmlc::Stream* fi) = 0;
143  virtual void SaveModel(dmlc::Stream* fo) const = 0;
144 
150  virtual void SetParams(Args const& args) = 0;
159  virtual void SetParam(const std::string& key, const std::string& value) = 0;
160 
165  virtual uint32_t GetNumFeature() = 0;
166 
175  virtual void SetAttr(const std::string& key, const std::string& value) = 0;
183  virtual bool GetAttr(const std::string& key, std::string* out) const = 0;
189  virtual bool DelAttr(const std::string& key) = 0;
194  virtual std::vector<std::string> GetAttrNames() const = 0;
198  bool AllowLazyCheckPoint() const;
206  virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
207  bool with_stats,
208  std::string format) = 0;
209 
210  virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0;
216  static Learner* Create(const std::vector<std::shared_ptr<DMatrix> >& cache_data);
217 
218  virtual GenericParameter const& GetGenericParameter() const = 0;
223  virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0;
224 
225  protected:
227  std::unique_ptr<ObjFunction> obj_;
229  std::unique_ptr<GradientBooster> gbm_;
231  std::vector<std::unique_ptr<Metric> > metrics_;
234 };
235 
236 struct LearnerModelParamLegacy;
237 
238 /*
239  * \brief Basic Model Parameters, used to describe the booster.
240  */
242  /* \brief global bias */
243  bst_float base_score { 0.5f };
244  /* \brief number of features */
245  uint32_t num_feature { 0 };
246  /* \brief number of classes, if it is multi-class classification */
247  uint32_t num_output_group { 0 };
248 
249  LearnerModelParam() = default;
250  // As the old `LearnerModelParamLegacy` is still used by binary IO, we keep
251  // this one as an immutable copy.
252  LearnerModelParam(LearnerModelParamLegacy const& user_param, float base_margin);
253  /* \brief Whether this parameter is initialized with LearnerModelParamLegacy. */
254  bool Initialized() const { return num_feature != 0; }
255 };
256 
257 } // namespace xgboost
258 #endif // XGBOOST_LEARNER_H_
Interface of predictor, performs predictions for a gradient booster.
float bst_float
float type, used for storing statistics
Definition: base.h:111
std::vector< std::unique_ptr< Metric > > metrics_
The evaluation metrics used to evaluate the model.
Definition: learner.h:231
Definition: learner.h:241
Definition: host_device_vector.h:86
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:253
Definition: generic_parameters.h:14
Defines the abstract interface for different components in XGBoost.
std::unique_ptr< GradientBooster > gbm_
The gradient booster used by the model.
Definition: learner.h:229
A device-and-host vector abstraction layer.
bool Initialized() const
Definition: learner.h:254
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
Definition: model.h:17
PredictionCacheEntry prediction_entry
Definition: learner.h:46
std::vector< bst_float > ret_vec_float
returning float vector.
Definition: learner.h:43
entry to to easily hold returning information
Definition: learner.h:35
Definition: model.h:31
std::vector< GradientPair > tmp_gpair
temp variable of gradient pairs.
Definition: learner.h:45
std::unique_ptr< ObjFunction > obj_
objective function
Definition: learner.h:227
Feature map data structure to help visualization and model dump.
namespace of xgboost
Definition: base.h:102
std::vector< const char * > ret_vec_charp
result holder for returning string pointers
Definition: learner.h:41
defines configuration macros of xgboost.
Learner class that does training and prediction. This is the user facing module of xgboost training...
Definition: learner.h:66
Data structure representing JSON format.
Definition: json.h:326
std::string ret_str
result holder for returning string
Definition: learner.h:37
std::vector< std::string > ret_vec_str
result holder for returning strings
Definition: learner.h:39
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:35
GenericParameter generic_parameters_
Training parameter.
Definition: learner.h:233