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
33 enum class PredictionType : std::uint8_t { // NOLINT
34 kValue = 0,
35 kMargin = 1,
36 kContribution = 2,
37 kApproxContribution = 3,
38 kInteraction = 4,
39 kApproxInteraction = 5,
40 kLeaf = 6
41 };
42
44 struct XGBAPIThreadLocalEntry {
46 std::string ret_str;
48 std::vector<std::string> ret_vec_str;
50 std::vector<const char *> ret_vec_charp;
52 std::vector<bst_float> ret_vec_float;
54 std::vector<GradientPair> tmp_gpair;
56 PredictionCacheEntry prediction_entry;
58 std::vector<bst_ulong> prediction_shape;
59 };
60
77 class Learner : public Model, public Configurable, public dmlc::Serializable {
78 public:
80 ~Learner() override;
84 virtual void Configure() = 0;
91 virtual void UpdateOneIter(int iter, std::shared_ptr<DMatrix> train) = 0;
99 virtual void BoostOneIter(int iter,
100 std::shared_ptr<DMatrix> train,
101 HostDeviceVector<GradientPair>* in_gpair) = 0;
109 virtual std::string EvalOneIter(int iter,
110 const std::vector<std::shared_ptr<DMatrix>>& data_sets,
111 const std::vector<std::string>& data_names) = 0;
125 virtual void Predict(std::shared_ptr<DMatrix> data,
126 bool output_margin,
127 HostDeviceVector<bst_float> *out_preds,
128 unsigned layer_begin,
129 unsigned layer_end,
130 bool training = false,
131 bool pred_leaf = false,
132 bool pred_contribs = false,
133 bool approx_contribs = false,
134 bool pred_interactions = false) = 0;
135
148 virtual void InplacePredict(dmlc::any const &x,
149 std::shared_ptr<DMatrix> p_m,
150 PredictionType type,
151 float missing,
152 HostDeviceVector<bst_float> **out_preds,
153 uint32_t layer_begin, uint32_t layer_end) = 0;
154
155 /*
156 * \brief Get number of boosted rounds from gradient booster.
157 */
158 virtual int32_t BoostedRounds() const = 0;
159 virtual uint32_t Groups() const = 0;
160
161 void LoadModel(Json const& in) override = 0;
162 void SaveModel(Json* out) const override = 0;
163
164 virtual void LoadModel(dmlc::Stream* fi) = 0;
165 virtual void SaveModel(dmlc::Stream* fo) const = 0;
166
172 virtual void SetParams(Args const& args) = 0;
181 virtual void SetParam(const std::string& key, const std::string& value) = 0;
182
187 virtual uint32_t GetNumFeature() const = 0;
188
197 virtual void SetAttr(const std::string& key, const std::string& value) = 0;
205 virtual bool GetAttr(const std::string& key, std::string* out) const = 0;
211 virtual bool DelAttr(const std::string& key) = 0;
216 virtual std::vector<std::string> GetAttrNames() const = 0;
221 virtual void SetFeatureNames(std::vector<std::string> const& fn) = 0;
226 virtual void GetFeatureNames(std::vector<std::string>* fn) const = 0;
231 virtual void SetFeatureTypes(std::vector<std::string> const& ft) = 0;
236 virtual void GetFeatureTypes(std::vector<std::string>* ft) const = 0;
237
241 bool AllowLazyCheckPoint() const;
252 virtual Learner *Slice(int32_t begin_layer, int32_t end_layer, int32_t step,
253 bool *out_of_bound) = 0;
261 virtual std::vector<std::string> DumpModel(const FeatureMap& fmap,
262 bool with_stats,
263 std::string format) = 0;
264
265 virtual XGBAPIThreadLocalEntry& GetThreadLocal() const = 0;
271 static Learner* Create(const std::vector<std::shared_ptr<DMatrix> >& cache_data);
272
273 virtual GenericParameter const& GetGenericParameter() const = 0;
278 virtual const std::map<std::string, std::string>& GetConfigurationArguments() const = 0;
279
280 protected:
282 std::unique_ptr<ObjFunction> obj_;
284 std::unique_ptr<GradientBooster> gbm_;
286 std::vector<std::unique_ptr<Metric> > metrics_;
288 GenericParameter generic_parameters_;
289 };
290
291 struct LearnerModelParamLegacy;
292
293 /*
294 * \brief Basic Model Parameters, used to describe the booster.
295 */
296 struct LearnerModelParam {
297 /* \brief global bias */
298 bst_float base_score { 0.5f };
299 /* \brief number of features */
300 uint32_t num_feature { 0 };
301 /* \brief number of classes, if it is multi-class classification */
302 uint32_t num_output_group { 0 };
303
304 LearnerModelParam() = default;
305 // As the old `LearnerModelParamLegacy` is still used by binary IO, we keep
306 // this one as an immutable copy.
307 LearnerModelParam(LearnerModelParamLegacy const& user_param, float base_margin);
308 /* \brief Whether this parameter is initialized with LearnerModelParamLegacy. */
309 bool Initialized() const { return num_feature != 0; }
310 };
311
312 } // namespace xgboost
313 #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
Temp variable for returing prediction result.
Definition: learner.h:56
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:302
model.h
Defines the abstract interface for different components in XGBoost.
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::Learner::GetFeatureNames
virtual void GetFeatureNames(std::vector< std::string > *fn) const =0
Get the feature names for current booster.
xgboost::GenericParameter
Definition: generic_parameters.h:15
xgboost::PredictionType::kInteraction
@ kInteraction
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:50
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:309
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::LearnerModelParam::base_score
bst_float base_score
Definition: learner.h:298
xgboost::Learner::SetParam
virtual void SetParam(const std::string &key, const std::string &value)=0
Set parameter for booster.
xgboost::Learner::Predict
virtual void Predict(std::shared_ptr< DMatrix > data, bool output_margin, HostDeviceVector< bst_float > *out_preds, unsigned layer_begin, unsigned 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.
base.h
defines configuration macros of xgboost.
xgboost::XGBAPIThreadLocalEntry::prediction_shape
std::vector< bst_ulong > prediction_shape
Temp variable for returing prediction shape.
Definition: learner.h:58
xgboost::PredictionType::kContribution
@ kContribution
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:46
xgboost::Learner::obj_
std::unique_ptr< ObjFunction > obj_
objective function
Definition: learner.h:282
xgboost::XGBAPIThreadLocalEntry::ret_vec_float
std::vector< bst_float > ret_vec_float
returning float vector.
Definition: learner.h:52
xgboost::Learner::gbm_
std::unique_ptr< GradientBooster > gbm_
The gradient booster used by the model.
Definition: learner.h:284
xgboost::LearnerModelParam
Definition: learner.h:296
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::GetFeatureTypes
virtual void GetFeatureTypes(std::vector< std::string > *ft) const =0
Get the feature types for current 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:48
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::PredictionType::kApproxInteraction
@ kApproxInteraction
xgboost::Learner::InplacePredict
virtual void InplacePredict(dmlc::any const &x, std::shared_ptr< DMatrix > p_m, PredictionType type, float missing, HostDeviceVector< bst_float > **out_preds, uint32_t layer_begin, uint32_t layer_end)=0
Inplace prediction.
xgboost::PredictionType::kLeaf
@ kLeaf
xgboost::Learner::SetFeatureNames
virtual void SetFeatureNames(std::vector< std::string > const &fn)=0
Set the feature names for current booster.
xgboost::Learner::GetGenericParameter
virtual GenericParameter const & GetGenericParameter() const =0
xgboost::PredictionType::kValue
@ kValue
xgboost::Learner::Groups
virtual uint32_t Groups() 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:54
xgboost::Learner::GetNumFeature
virtual uint32_t GetNumFeature() const =0
Get the number of features of the booster.
xgboost::Learner::~Learner
~Learner() override
virtual destructor
xgboost::Learner::generic_parameters_
GenericParameter generic_parameters_
Training parameter.
Definition: learner.h:288
xgboost::XGBAPIThreadLocalEntry
entry to to easily hold returning information
Definition: learner.h:44
xgboost::Learner::GetAttrNames
virtual std::vector< std::string > GetAttrNames() const =0
Get a vector of attribute names from the booster.
generic_parameters.h
xgboost::PredictionType::kMargin
@ kMargin
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:77
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:286
xgboost::Learner::GetThreadLocal
virtual XGBAPIThreadLocalEntry & GetThreadLocal() const =0
xgboost::Json
Data structure representing JSON format.
Definition: json.h:355
xgboost::LearnerModelParam::LearnerModelParam
LearnerModelParam()=default
xgboost::Learner::BoostedRounds
virtual int32_t BoostedRounds() const =0
xgboost::LearnerModelParam::num_feature
uint32_t num_feature
Definition: learner.h:300
xgboost::PredictionType
PredictionType
Definition: learner.h:33
xgboost::PredictionType::kApproxContribution
@ kApproxContribution
xgboost::Learner::DelAttr
virtual bool DelAttr(const std::string &key)=0
Delete an attribute from the booster.
xgboost::Learner::SetFeatureTypes
virtual void SetFeatureTypes(std::vector< std::string > const &ft)=0
Set the feature types for current 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