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>
16 #include <xgboost/generic_parameters.h>
17 #include <xgboost/host_device_vector.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
35 struct XGBAPIThreadLocalEntry {
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;
46 PredictionCacheEntry prediction_entry;
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
169 virtual void SetAttr(const std::string& key, const std::string& value) = 0;
177 virtual bool GetAttr(const std::string& key, std::string* out) const = 0;
183 virtual bool DelAttr(const std::string& key) = 0;
188 virtual std::vector<std::string> GetAttrNames() const = 0;
192 bool AllowLazyCheckPoint() const;
200 virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
201 bool with_stats,
202 std::string format) = 0;
203
204 virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0;
210 static Learner* Create(const std::vector<std::shared_ptr<DMatrix> >& cache_data);
211
212 virtual GenericParameter const& GetGenericParameter() const = 0;
217 virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0;
218
219 protected:
221 std::unique_ptr<ObjFunction> obj_;
223 std::unique_ptr<GradientBooster> gbm_;
225 std::vector<std::unique_ptr<Metric> > metrics_;
227 GenericParameter generic_parameters_;
228 };
229
230 struct LearnerModelParamLegacy;
231
232 /*
233 * \brief Basic Model Parameters, used to describe the booster.
234 */
235 struct LearnerModelParam {
236 /* \brief global bias */
237 bst_float base_score { 0.5f };
238 /* \brief number of features */
239 uint32_t num_feature { 0 };
240 /* \brief number of classes, if it is multi-class classification */
241 uint32_t num_output_group { 0 };
242
243 LearnerModelParam() = default;
244 // As the old `LearnerModelParamLegacy` is still used by binary IO, we keep
245 // this one as an immutable copy.
246 LearnerModelParam(LearnerModelParamLegacy const& user_param, float base_margin);
247 /* \brief Whether this parameter is initialized with LearnerModelParamLegacy. */
248 bool Initialized() const { return num_feature != 0; }
249 };
250
251 } // namespace xgboost
252 #endif // XGBOOST_LEARNER_H_
predictor.h
Interface of predictor, performs predictions for a gradient booster.
xgboost::bst_float
float bst_float
float type, used for storing statistics
Definition: base.h:111
xgboost::Learner::metrics_
std::vector< std::unique_ptr< Metric > > metrics_
The evaluation metrics used to evaluate the model.
Definition: learner.h:225
xgboost::LearnerModelParam
Definition: learner.h:235
xgboost::HostDeviceVector
Definition: host_device_vector.h:85
xgboost::Args
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:245
xgboost::GenericParameter
Definition: generic_parameters.h:14
model.h
Defines the abstract interface for different components in XGBoost.
xgboost::Learner::gbm_
std::unique_ptr< GradientBooster > gbm_
The gradient booster used by the model.
Definition: learner.h:223
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::LearnerModelParam::Initialized
bool Initialized() const
Definition: learner.h:248
xgboost::FeatureMap
Feature map data structure to help text model dump. TODO(tqchen) consider make it even more lightweig...
Definition: feature_map.h:22
xgboost::Model
Definition: model.h:17
xgboost::XGBAPIThreadLocalEntry::prediction_entry
PredictionCacheEntry prediction_entry
Definition: learner.h:46
xgboost::XGBAPIThreadLocalEntry::ret_vec_float
std::vector< bst_float > ret_vec_float
returning float vector.
Definition: learner.h:43
xgboost::XGBAPIThreadLocalEntry
entry to to easily hold returning information
Definition: learner.h:35
xgboost::Configurable
Definition: model.h:31
xgboost::XGBAPIThreadLocalEntry::tmp_gpair
std::vector< GradientPair > tmp_gpair
temp variable of gradient pairs.
Definition: learner.h:45
xgboost::Learner::obj_
std::unique_ptr< ObjFunction > obj_
objective function
Definition: learner.h:221
feature_map.h
Feature map data structure to help visualization and model dump.
xgboost
namespace of xgboost
Definition: base.h:102
xgboost::XGBAPIThreadLocalEntry::ret_vec_charp
std::vector< const char * > ret_vec_charp
result holder for returning string pointers
Definition: learner.h:41
base.h
defines configuration macros of xgboost.
xgboost::Learner
Learner class that does training and prediction. This is the user facing module of xgboost training...
Definition: learner.h:66
xgboost::Json
Data structure representing JSON format.
Definition: json.h:326
generic_parameters.h
xgboost::XGBAPIThreadLocalEntry::ret_str
std::string ret_str
result holder for returning string
Definition: learner.h:37
xgboost::XGBAPIThreadLocalEntry::ret_vec_str
std::vector< std::string > ret_vec_str
result holder for returning strings
Definition: learner.h:39
xgboost::PredictionCacheEntry
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:35
xgboost::Learner::generic_parameters_
GenericParameter generic_parameters_
Training parameter.
Definition: learner.h:227