8 #ifndef XGBOOST_LEARNER_H_
9 #define XGBOOST_LEARNER_H_
31 class GradientBooster;
35 struct XGBAPIThreadLocalEntry;
37 class HostDeviceVector;
79 virtual void UpdateOneIter(
int iter, std::shared_ptr<DMatrix> train) = 0;
88 std::shared_ptr<DMatrix> train,
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,
bool output_margin,
115 bst_layer_t layer_end,
bool training =
false,
bool pred_leaf =
false,
116 bool pred_contribs =
false,
bool approx_contribs =
false,
117 bool pred_interactions =
false) = 0;
138 std::vector<bst_feature_t>* features,
139 std::vector<float>* scores) = 0;
148 virtual std::uint32_t
Groups()
const = 0;
170 virtual void SetParam(
const std::string& key,
const std::string& value) = 0;
186 virtual void SetAttr(
const std::string& key,
const std::string& value) = 0;
194 virtual bool GetAttr(
const std::string& key, std::string* out)
const = 0;
200 virtual bool DelAttr(
const std::string& key) = 0;
238 bool* out_of_bound) = 0;
248 std::string format) = 0;
256 static Learner*
Create(
const std::vector<std::shared_ptr<DMatrix> >& cache_data);
269 std::unique_ptr<ObjFunction>
obj_;
271 std::unique_ptr<GradientBooster>
gbm_;
278 struct LearnerModelParamLegacy;
326 : base_score_{
std::move(base_score)},
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
Definition: host_device_vector.h:87
Data structure representing JSON format.
Definition: json.h:357
Learner class that does training and prediction. This is the user facing module of xgboost training....
Definition: learner.h:65
virtual std::vector< std::string > GetAttrNames() const =0
Get a vector of attribute names from the booster.
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.
virtual void LoadModel(dmlc::Stream *fi)=0
std::vector< std::unique_ptr< Metric > > metrics_
The evaluation metrics used to evaluate the model.
Definition: learner.h:273
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 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 destructor
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 SaveModel(dmlc::Stream *fo) const =0
virtual Context const * Ctx() const =0
Return the context object of this Booster.
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.
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 UpdateOneIter(int iter, std::shared_ptr< DMatrix > train)=0
update the model for one iteration With the specified objective function.
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:269
std::unique_ptr< GradientBooster > gbm_
The gradient booster used by the model.
Definition: learner.h:271
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.
Context ctx_
Training parameter.
Definition: learner.h:275
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 uint32_t GetNumFeature() const =0
Get the number of features of the booster.
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:424
A tensor view with static type and dimension. It implements indexing and slicing.
Definition: linalg.h:293
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
namespace of xgboost
Definition: base.h:90
std::vector< std::pair< std::string, std::string > > Args
Definition: base.h:316
uint32_t bst_feature_t
Type for data column (feature) index.
Definition: base.h:101
PredictionType
Definition: learner.h:39
std::uint32_t bst_target_t
Type for indexing into output targets.
Definition: base.h:118
std::int32_t bst_layer_t
Type for indexing boosted layers.
Definition: base.h:122
MultiStrategy
Strategy for building multi-target models.
Definition: learner.h:283
Runtime context for XGBoost. Contains information like threads and device.
Definition: context.h:84
Basic model parameters, used to describe the booster.
Definition: learner.h:291
std::uint32_t num_output_group
The number of classes or targets.
Definition: learner.h:307
LearnerModelParam()=default
LearnerModelParam(Context const *ctx, LearnerModelParamLegacy const &user_param, linalg::Tensor< float, 1 > base_margin, ObjInfo t, MultiStrategy multi_strategy)
bool Initialized() const
Definition: learner.h:344
LearnerModelParam(bst_feature_t n_features, linalg::Tensor< float, 1 > base_score, std::uint32_t n_groups, bst_target_t n_targets, MultiStrategy multi_strategy)
Definition: learner.h:324
MultiStrategy multi_strategy
Strategy for building multi-target models.
Definition: learner.h:315
linalg::TensorView< float const, 1 > BaseScore(std::int32_t device) const
bst_target_t OutputLength() const noexcept
Definition: learner.h:338
LearnerModelParam(LearnerModelParamLegacy const &user_param, ObjInfo t, MultiStrategy multi_strategy)
void Copy(LearnerModelParam const &that)
linalg::TensorView< float const, 1 > BaseScore(Context const *ctx) const
bst_target_t LeafLength() const noexcept
Definition: learner.h:339
ObjInfo task
Current task, determined by objective.
Definition: learner.h:311
bool IsVectorLeaf() const noexcept
Definition: learner.h:335
bst_feature_t num_feature
The number of features.
Definition: learner.h:303
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