11/* *
2- * Copyright 2014-2024 , XGBoost Contributors
3- * \file objective.h
4- * \ brief interface of objective function used by xgboost.
5- * \ author Tianqi Chen, Kailong Chen
2+ * Copyright 2014-2025 , XGBoost Contributors
3+ *
4+ * @ brief interface of objective function used by xgboost.
5+ * @ author Tianqi Chen, Kailong Chen
66 */
77#ifndef XGBOOST_OBJECTIVE_H_
88#define XGBOOST_OBJECTIVE_H_
1111#include < xgboost/base.h>
1212#include < xgboost/data.h>
1313#include < xgboost/host_device_vector.h>
14+ #include < xgboost/linalg.h> // for Vector
1415#include < xgboost/model.h>
1516#include < xgboost/task.h>
1617
17- #include < cstdint> // std:: int32_t
18+ #include < cstdint> // for int32_t
1819#include < functional>
19- #include < string>
20+ #include < string> // for string
2021
2122namespace xgboost {
2223
2324class RegTree ;
2425struct Context ;
2526
26- /* ! \ brief interface of objective function */
27+ /* * @ brief The interface of objective function */
2728class ObjFunction : public Configurable {
2829 protected:
2930 Context const * ctx_;
@@ -32,32 +33,30 @@ class ObjFunction : public Configurable {
3233 static constexpr float DefaultBaseScore () { return 0 .5f ; }
3334
3435 public:
35- /* ! \brief virtual destructor */
3636 ~ObjFunction () override = default ;
37- /* !
38- * \brief Configure the objective with the specified parameters.
39- * \param args arguments to the objective function.
37+ /* *
38+ * @brief Configure the objective with the specified parameters.
39+ *
40+ * @param args arguments to the objective function.
4041 */
4142 virtual void Configure (Args const & args) = 0;
4243 /* *
4344 * @brief Get gradient over each of predictions, given existing information.
4445 *
45- * @param preds prediction of current round
46- * @param info information about labels, weights, groups in rank
46+ * @param preds Raw prediction (before applying the inverse link) of the current round.
47+ * @param info information about labels, weights, groups in rank.
4748 * @param iteration current iteration number.
4849 * @param out_gpair output of get gradient, saves gradient and second order gradient in
4950 */
5051 virtual void GetGradient (HostDeviceVector<float > const & preds, MetaInfo const & info,
5152 std::int32_t iter, linalg::Matrix<GradientPair>* out_gpair) = 0;
5253
53- /* ! \ return the default evaluation metric for the objective */
54- virtual const char * DefaultEvalMetric () const = 0;
54+ /* * @ return the default evaluation metric for the objective */
55+ [[nodiscard]] virtual const char * DefaultEvalMetric () const = 0;
5556 /* *
56- * \ brief Return the configuration for the default metric.
57+ * @ brief Return the configuration for the default metric.
5758 */
58- virtual Json DefaultMetricConfig () const { return Json{Null{}}; }
59-
60- // the following functions are optional, most of time default implementation is good enough
59+ [[nodiscard]] virtual Json DefaultMetricConfig () const { return Json{Null{}}; }
6160 /* *
6261 * @brief Apply inverse link (activation) function to prediction values.
6362 *
@@ -75,25 +74,28 @@ class ObjFunction : public Configurable {
7574 */
7675 virtual void EvalTransform (HostDeviceVector<float >* io_preds) { this ->PredTransform (io_preds); }
7776 /* *
78- * @brief Apply link function to the intercept.
77+ * @brief Apply the link function to the intercept.
7978 *
80- * This is used to transform user-set base_score back to margin used by gradient
81- * boosting
79+ * This is an inverse of `PredTransform` for most of the objectives (if there's a
80+ * valid inverse). It's used to transform user-set base_score back to margin used by
81+ * gradient boosting. The method converts objective-based valid outputs like
82+ * probability back to raw model outputs.
8283 *
8384 * @return transformed value
8485 */
8586 [[nodiscard]] virtual float ProbToMargin (float base_score) const { return base_score; }
8687 /* *
87- * @brief Obtain the initial estimation of prediction.
88+ * @brief Obtain the initial estimation of prediction (intercept) .
8889 *
89- * The output in `base_score` represents prediction after apply the inverse link function.
90+ * The output in `base_score` represents prediction after apply the inverse link function
91+ * (valid prediction instead of raw).
9092 *
9193 * @param info MetaInfo that contains label.
9294 * @param base_score Output estimation.
9395 */
94- virtual void InitEstimation (MetaInfo const & info, linalg::Tensor <float , 1 >* base_score) const ;
95- /* !
96- * \ brief Return task of this objective.
96+ virtual void InitEstimation (MetaInfo const & info, linalg::Vector <float >* base_score) const ;
97+ /* *
98+ * @ brief Return task of this objective.
9799 */
98100 [[nodiscard]] virtual struct ObjInfo Task () const = 0;
99101 /* *
@@ -106,31 +108,33 @@ class ObjFunction : public Configurable {
106108 }
107109 return 1 ;
108110 }
111+ /* * @brief Getter of the context. */
112+ [[nodiscard]] Context const * Ctx () const { return this ->ctx_ ; }
109113
110114 /* *
111- * \ brief Update the leaf values after a tree is built. Needed for objectives with 0
115+ * @ brief Update the leaf values after a tree is built. Needed for objectives with 0
112116 * hessian.
113117 *
114118 * Note that the leaf update is not well defined for distributed training as XGBoost
115119 * computes only an average of quantile between workers. This breaks when some leaf
116120 * have no sample assigned in a local worker.
117121 *
118- * \ param position The leaf index for each rows.
119- * \ param info MetaInfo providing labels and weights.
120- * \ param learning_rate The learning rate for current iteration.
121- * \ param prediction Model prediction after transformation.
122- * \ param group_idx The group index for this tree, 0 when it's not multi-target or multi-class.
123- * \ param p_tree Tree that needs to be updated.
122+ * @ param position The leaf index for each rows.
123+ * @ param info MetaInfo providing labels and weights.
124+ * @ param learning_rate The learning rate for current iteration.
125+ * @ param prediction Model prediction after transformation.
126+ * @ param group_idx The group index for this tree, 0 when it's not multi-target or multi-class.
127+ * @ param p_tree Tree that needs to be updated.
124128 */
125129 virtual void UpdateTreeLeaf (HostDeviceVector<bst_node_t > const & /* position*/ ,
126130 MetaInfo const & /* info*/ , float /* learning_rate*/ ,
127131 HostDeviceVector<float > const & /* prediction*/ ,
128132 std::int32_t /* group_idx*/ , RegTree* /* p_tree*/ ) const {}
129-
130- /* !
131- * \brief Create an objective function according to name.
132- * \ param ctx Pointer to runtime parameters .
133- * \ param name Name of the objective .
133+ /* *
134+ * @brief Create an objective function according to the name.
135+ *
136+ * @ param name Name of the objective .
137+ * @ param ctx Pointer to the context .
134138 */
135139 static ObjFunction* Create (const std::string& name, Context const * ctx);
136140};
0 commit comments