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 <xgboost/base.h>
13 #include <xgboost/feature_map.h>
14 #include <xgboost/predictor.h>
15 #include <xgboost/generic_parameters.h>
16 #include <xgboost/host_device_vector.h>
17 #include <xgboost/model.h>
18
19 #include <utility>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 namespace xgboost {
26
27 class Metric;
28 class GradientBooster;
29 class ObjFunction;
30 class DMatrix;
31 class Json;
32
34 struct XGBAPIThreadLocalEntry {
36 std::string ret_str;
38 std::vector<std::string> ret_vec_str;
40 std::vector<const char *> ret_vec_charp;
42 std::vector<bst_float> ret_vec_float;
44 std::vector<GradientPair> tmp_gpair;
45 PredictionCacheEntry prediction_entry;
46 };
47
48
65 class Learner : public Model, public Configurable, public dmlc::Serializable {
66 public:
68 ~Learner() override;
72 virtual void Configure() = 0;
79 virtual void UpdateOneIter(int iter, std::shared_ptr<DMatrix> train) = 0;
87 virtual void BoostOneIter(int iter,
88 std::shared_ptr<DMatrix> train,
89 HostDeviceVector<GradientPair>* in_gpair) = 0;
97 virtual std::string EvalOneIter(int iter,
98 const std::vector<std::shared_ptr<DMatrix>>& data_sets,
99 const std::vector<std::string>& data_names) = 0;
113 virtual void Predict(std::shared_ptr<DMatrix> data,
114 bool output_margin,
115 HostDeviceVector<bst_float> *out_preds,
116 unsigned ntree_limit = 0,
117 bool training = false,
118 bool pred_leaf = false,
119 bool pred_contribs = false,
120 bool approx_contribs = false,
121 bool pred_interactions = false) = 0;
122
133 virtual void InplacePredict(dmlc::any const& x, std::string const& type,
134 float missing,
135 HostDeviceVector<bst_float> **out_preds,
136 uint32_t layer_begin, uint32_t layer_end) = 0;
137
138 void LoadModel(Json const& in) override = 0;
139 void SaveModel(Json* out) const override = 0;
140
141 virtual void LoadModel(dmlc::Stream* fi) = 0;
142 virtual void SaveModel(dmlc::Stream* fo) const = 0;
143
149 virtual void SetParams(Args const& args) = 0;
158 virtual void SetParam(const std::string& key, const std::string& value) = 0;
159
164 virtual uint32_t GetNumFeature() = 0;
165
174 virtual void SetAttr(const std::string& key, const std::string& value) = 0;
182 virtual bool GetAttr(const std::string& key, std::string* out) const = 0;
188 virtual bool DelAttr(const std::string& key) = 0;
193 virtual std::vector<std::string> GetAttrNames() const = 0;
197 bool AllowLazyCheckPoint() const;
208 virtual Learner *Slice(int32_t begin_layer, int32_t end_layer, int32_t step,
209 bool *out_of_bound) = 0;
217 virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
218 bool with_stats,
219 std::string format) = 0;
220
221 virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0;
227 static Learner* Create(const std::vector<std::shared_ptr<DMatrix> >& cache_data);
228
229 virtual GenericParameter const& GetGenericParameter() const = 0;
234 virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0;
235
236 protected:
238 std::unique_ptr<ObjFunction> obj_;
240 std::unique_ptr<GradientBooster> gbm_;
242 std::vector<std::unique_ptr<Metric> > metrics_;
244 GenericParameter generic_parameters_;
245 };
246
247 struct LearnerModelParamLegacy;
248
249 /*
250 * \brief Basic Model Parameters, used to describe the booster.
251 */
252 struct LearnerModelParam {
253 /* \brief global bias */
254 bst_float base_score { 0.5f };
255 /* \brief number of features */
256 uint32_t num_feature { 0 };
257 /* \brief number of classes, if it is multi-class classification */
258 uint32_t num_output_group { 0 };
259
260 LearnerModelParam() = default;
261 // As the old `LearnerModelParamLegacy` is still used by binary IO, we keep
262 // this one as an immutable copy.
263 LearnerModelParam(LearnerModelParamLegacy const& user_param, float base_margin);
264 /* \brief Whether this parameter is initialized with LearnerModelParamLegacy. */
265 bool Initialized() const { return num_feature != 0; }
266 };
267
268 } // namespace xgboost
269 #endif // XGBOOST_LEARNER_H_
xgboost::Args
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:262
xgboost::Learner::DumpModel
virtual std::vector< std::string > DumpModel(const FeatureMap &fmap, bool with_stats, std::string format)=0
dump the model in the requested format
predictor.h
Interface of predictor, performs predictions for a gradient booster.
xgboost::XGBAPIThreadLocalEntry::prediction_entry
PredictionCacheEntry prediction_entry
Definition: learner.h:45
xgboost::PredictionCacheEntry
Contains pointer to input matrix and associated cached predictions.
Definition: predictor.h:35
xgboost::LearnerModelParam::num_output_group
uint32_t num_output_group
Definition: learner.h:258
model.h
Defines the abstract interface for different components in XGBoost.
xgboost::Learner::Predict
virtual void Predict(std::shared_ptr< DMatrix > data, bool output_margin, HostDeviceVector< bst_float > *out_preds, unsigned ntree_limit=0, 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.
xgboost::Learner::Slice
virtual Learner * Slice(int32_t begin_layer, int32_t end_layer, int32_t step, bool *out_of_bound)=0
Slice the model.
xgboost::Learner::SetParams
virtual void SetParams(Args const &args)=0
Set multiple parameters at once.
xgboost::Learner::Configure
virtual void Configure()=0
Configure Learner based on set parameters.
xgboost::GenericParameter
Definition: generic_parameters.h:15
xgboost::HostDeviceVector
Definition: host_device_vector.h:86
xgboost::XGBAPIThreadLocalEntry::ret_vec_charp
std::vector< const char * > ret_vec_charp
result holder for returning string pointers
Definition: learner.h:40
xgboost::Learner::BoostOneIter
virtual void BoostOneIter(int iter, std::shared_ptr< DMatrix > train, HostDeviceVector< GradientPair > *in_gpair)=0
Do customized gradient boosting with in_gpair. in_gair can be mutated after this call.
xgboost::LearnerModelParam::Initialized
bool Initialized() const
Definition: learner.h:265
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::LearnerModelParam::base_score
bst_float base_score
Definition: learner.h:254
xgboost::Learner::SetParam
virtual void SetParam(const std::string &key, const std::string &value)=0
Set parameter for booster.
base.h
defines configuration macros of xgboost.
xgboost::Learner::InplacePredict
virtual void InplacePredict(dmlc::any const &x, std::string const &type, float missing, HostDeviceVector< bst_float > **out_preds, uint32_t layer_begin, uint32_t layer_end)=0
Inplace prediction.
feature_map.h
Feature map data structure to help visualization and model dump.
xgboost::XGBAPIThreadLocalEntry::ret_str
std::string ret_str
result holder for returning string
Definition: learner.h:36
xgboost::Learner::obj_
std::unique_ptr< ObjFunction > obj_
objective function
Definition: learner.h:238
xgboost::XGBAPIThreadLocalEntry::ret_vec_float
std::vector< bst_float > ret_vec_float
returning float vector.
Definition: learner.h:42
xgboost::Learner::gbm_
std::unique_ptr< GradientBooster > gbm_
The gradient booster used by the model.
Definition: learner.h:240
xgboost::LearnerModelParam
Definition: learner.h:252
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::Learner::SetAttr
virtual void SetAttr(const std::string &key, const std::string &value)=0
Set additional attribute to the Booster.
xgboost::Learner::GetAttr
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.
xgboost::Learner::UpdateOneIter
virtual void UpdateOneIter(int iter, std::shared_ptr< DMatrix > train)=0
update the model for one iteration With the specified objective function.
xgboost::XGBAPIThreadLocalEntry::ret_vec_str
std::vector< std::string > ret_vec_str
result holder for returning strings
Definition: learner.h:38
xgboost::Model
Definition: model.h:17
xgboost::Learner::Create
static Learner * Create(const std::vector< std::shared_ptr< DMatrix > > &cache_data)
Create a new instance of learner.
xgboost::Learner::AllowLazyCheckPoint
bool AllowLazyCheckPoint() const
xgboost::Learner::GetNumFeature
virtual uint32_t GetNumFeature()=0
Get the number of features of the booster.
xgboost::Learner::GetGenericParameter
virtual GenericParameter const & GetGenericParameter() const =0
xgboost::Configurable
Definition: model.h:31
xgboost::XGBAPIThreadLocalEntry::tmp_gpair
std::vector< GradientPair > tmp_gpair
temp variable of gradient pairs.
Definition: learner.h:44
xgboost::Learner::~Learner
~Learner() override
virtual destructor
xgboost::Learner::generic_parameters_
GenericParameter generic_parameters_
Training parameter.
Definition: learner.h:244
xgboost::XGBAPIThreadLocalEntry
entry to to easily hold returning information
Definition: learner.h:34
xgboost::Learner::GetAttrNames
virtual std::vector< std::string > GetAttrNames() const =0
Get a vector of attribute names from the booster.
generic_parameters.h
xgboost::Learner::GetConfigurationArguments
virtual const std::map< std::string, std::string > & GetConfigurationArguments() const =0
Get configuration arguments currently stored by the learner.
xgboost::Learner::SaveModel
void SaveModel(Json *out) const override=0
saves the model config to a json object
xgboost::Learner
Learner class that does training and prediction. This is the user facing module of xgboost training....
Definition: learner.h:65
xgboost::Learner::EvalOneIter
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.
xgboost::Learner::metrics_
std::vector< std::unique_ptr< Metric > > metrics_
The evaluation metrics used to evaluate the model.
Definition: learner.h:242
xgboost::Learner::GetThreadLocal
virtual XGBAPIThreadLocalEntry & GetThreadLocal() const =0
xgboost::Json
Data structure representing JSON format.
Definition: json.h:345
xgboost::LearnerModelParam::LearnerModelParam
LearnerModelParam()=default
xgboost::LearnerModelParam::num_feature
uint32_t num_feature
Definition: learner.h:256
xgboost::Learner::DelAttr
virtual bool DelAttr(const std::string &key)=0
Delete an attribute from the booster.
xgboost::Learner::LoadModel
void LoadModel(Json const &in) override=0
load the model from a json object
xgboost
namespace of xgboost
Definition: base.h:110
xgboost::bst_float
float bst_float
float type, used for storing statistics
Definition: base.h:119