xgboost

objective.h
Go to the documentation of this file.
1
7 #ifndef XGBOOST_OBJECTIVE_H_
8 #define XGBOOST_OBJECTIVE_H_
9
10 #include <dmlc/registry.h>
11 #include <xgboost/base.h>
12 #include <xgboost/data.h>
13 #include <xgboost/model.h>
14 #include <xgboost/generic_parameters.h>
15 #include <xgboost/host_device_vector.h>
16 #include <xgboost/task.h>
17
18 #include <vector>
19 #include <utility>
20 #include <string>
21 #include <functional>
22
23 namespace xgboost {
24
26 class ObjFunction : public Configurable {
27 protected:
28 GenericParameter const* ctx_;
29
30 public:
32 ~ObjFunction() override = default;
37 virtual void Configure(const std::vector<std::pair<std::string, std::string> >& args) = 0;
45 virtual void GetGradient(const HostDeviceVector<bst_float>& preds,
46 const MetaInfo& info,
47 int iteration,
48 HostDeviceVector<GradientPair>* out_gpair) = 0;
49
51 virtual const char* DefaultEvalMetric() const = 0;
52 // the following functions are optional, most of time default implementation is good enough
57 virtual void PredTransform(HostDeviceVector<bst_float>*) const {}
58
64 virtual void EvalTransform(HostDeviceVector<bst_float> *io_preds) {
65 this->PredTransform(io_preds);
66 }
73 virtual bst_float ProbToMargin(bst_float base_score) const {
74 return base_score;
75 }
79 virtual struct ObjInfo Task() const = 0;
84 virtual uint32_t Targets(MetaInfo const& info) const {
85 if (info.labels.Shape(1) > 1) {
86 LOG(FATAL) << "multioutput is not supported by current objective function";
87 }
88 return 1;
89 }
90
96 static ObjFunction* Create(const std::string& name, GenericParameter const* tparam);
97 };
98
102 struct ObjFunctionReg
103 : public dmlc::FunctionRegEntryBase<ObjFunctionReg,
104 std::function<ObjFunction* ()> > {
105 };
106
119 #define XGBOOST_REGISTER_OBJECTIVE(UniqueId, Name) \
120 static DMLC_ATTRIBUTE_UNUSED ::xgboost::ObjFunctionReg & \
121 __make_ ## ObjFunctionReg ## _ ## UniqueId ## __ = \
122 ::dmlc::Registry< ::xgboost::ObjFunctionReg>::Get()->__REGISTER__(Name)
123 } // namespace xgboost
124 #endif // XGBOOST_OBJECTIVE_H_
xgboost::ObjFunction
interface of objective function
Definition: objective.h:26
xgboost::ObjFunction::EvalTransform
virtual void EvalTransform(HostDeviceVector< bst_float > *io_preds)
transform prediction values, this is only called when Eval is called, usually it redirect to PredTran...
Definition: objective.h:64
xgboost::ObjFunction::DefaultEvalMetric
virtual const char * DefaultEvalMetric() const =0
xgboost::ObjFunction::ProbToMargin
virtual bst_float ProbToMargin(bst_float base_score) const
transform probability value back to margin this is used to transform user-set base_score back to marg...
Definition: objective.h:73
task.h
model.h
Defines the abstract interface for different components in XGBoost.
xgboost::GenericParameter
Definition: generic_parameters.h:15
xgboost::ObjFunction::Targets
virtual uint32_t Targets(MetaInfo const &info) const
Return number of targets for input matrix. Right now XGBoost supports only multi-target regression.
Definition: objective.h:84
xgboost::HostDeviceVector< bst_float >
host_device_vector.h
A device-and-host vector abstraction layer.
xgboost::ObjFunction::ctx_
GenericParameter const * ctx_
Definition: objective.h:28
base.h
defines configuration macros of xgboost.
xgboost::ObjFunction::Task
virtual struct ObjInfo Task() const =0
Return task of this objective.
xgboost::ObjFunction::Create
static ObjFunction * Create(const std::string &name, GenericParameter const *tparam)
Create an objective function according to name.
xgboost::ObjFunctionReg
Registry entry for objective factory functions.
Definition: objective.h:102
xgboost::ObjFunction::Configure
virtual void Configure(const std::vector< std::pair< std::string, std::string > > &args)=0
Configure the objective with the specified parameters.
xgboost::ObjFunction::GetGradient
virtual void GetGradient(const HostDeviceVector< bst_float > &preds, const MetaInfo &info, int iteration, HostDeviceVector< GradientPair > *out_gpair)=0
Get gradient over each of predictions, given existing information.
xgboost::linalg::Tensor::Shape
auto Shape() const
Definition: linalg.h:735
xgboost::Configurable
Definition: model.h:31
xgboost::ObjFunction::PredTransform
virtual void PredTransform(HostDeviceVector< bst_float > *) const
transform prediction values, this is only called when Prediction is called
Definition: objective.h:57
xgboost::ObjFunction::~ObjFunction
~ObjFunction() override=default
virtual destructor
generic_parameters.h
data.h
The input data structure of xgboost.
xgboost::MetaInfo
Meta information about dataset, always sit in memory.
Definition: data.h:46
xgboost::MetaInfo::labels
linalg::Tensor< float, 2 > labels
label of each instance
Definition: data.h:58
xgboost::ObjInfo
A struct returned by objective, which determines task at hand. The struct is not used by any algorith...
Definition: task.h:24
xgboost
namespace of xgboost
Definition: base.h:110
xgboost::bst_float
float bst_float
float type, used for storing statistics
Definition: base.h:119