Medial Code Documentation
Loading...
Searching...
No Matches
regression_loss.h
1
4#ifndef XGBOOST_OBJECTIVE_REGRESSION_LOSS_H_
5#define XGBOOST_OBJECTIVE_REGRESSION_LOSS_H_
6
7#include <dmlc/omp.h>
8
9#include <cmath>
10
11#include "../common/math.h"
12#include "xgboost/data.h" // MetaInfo
13#include "xgboost/logging.h"
14#include "xgboost/task.h" // ObjInfo
15
16namespace xgboost {
17namespace obj {
18// common regressions
19// linear regression
21 XGBOOST_DEVICE static bst_float PredTransform(bst_float x) { return x; }
22 XGBOOST_DEVICE static bool CheckLabel(bst_float) { return true; }
23 XGBOOST_DEVICE static bst_float FirstOrderGradient(bst_float predt, bst_float label) {
24 return predt - label;
25 }
26 XGBOOST_DEVICE static bst_float SecondOrderGradient(bst_float, bst_float) { return 1.0f; }
27 static bst_float ProbToMargin(bst_float base_score) { return base_score; }
28 static const char* LabelErrorMsg() { return ""; }
29 static const char* DefaultEvalMetric() { return "rmse"; }
30
31 static const char* Name() { return "reg:squarederror"; }
32 static ObjInfo Info() { return {ObjInfo::kRegression, true, false}; }
33};
34
36 XGBOOST_DEVICE static bst_float PredTransform(bst_float x) { return x; }
37 XGBOOST_DEVICE static bool CheckLabel(bst_float label) { return label > -1; }
38 XGBOOST_DEVICE static bst_float FirstOrderGradient(bst_float predt, bst_float label) {
39 predt = fmaxf(predt, -1 + 1e-6); // ensure correct value for log1p
40 return (std::log1p(predt) - std::log1p(label)) / (predt + 1);
41 }
42 XGBOOST_DEVICE static bst_float SecondOrderGradient(bst_float predt, bst_float label) {
43 predt = fmaxf(predt, -1 + 1e-6);
44 float res = (-std::log1p(predt) + std::log1p(label) + 1) / std::pow(predt + 1, 2);
45 res = fmaxf(res, 1e-6f);
46 return res;
47 }
48 static bst_float ProbToMargin(bst_float base_score) { return base_score; }
49 static const char* LabelErrorMsg() {
50 return "label must be greater than -1 for rmsle so that log(label + 1) can be valid.";
51 }
52 static const char* DefaultEvalMetric() { return "rmsle"; }
53
54 static const char* Name() { return "reg:squaredlogerror"; }
55
56 static ObjInfo Info() { return ObjInfo::kRegression; }
57};
58
59// logistic loss for probability regression task
61 XGBOOST_DEVICE static bst_float PredTransform(bst_float x) { return common::Sigmoid(x); }
62 XGBOOST_DEVICE static bool CheckLabel(bst_float x) { return x >= 0.0f && x <= 1.0f; }
63 XGBOOST_DEVICE static bst_float FirstOrderGradient(bst_float predt, bst_float label) {
64 return predt - label;
65 }
66 XGBOOST_DEVICE static bst_float SecondOrderGradient(bst_float predt, bst_float) {
67 const float eps = 1e-16f;
68 return fmaxf(predt * (1.0f - predt), eps);
69 }
70 static bst_float ProbToMargin(bst_float base_score) {
71 CHECK(base_score > 0.0f && base_score < 1.0f)
72 << "base_score must be in (0,1) for logistic loss, got: " << base_score;
73 return -logf(1.0f / base_score - 1.0f);
74 }
75 static const char* LabelErrorMsg() { return "label must be in [0,1] for logistic regression"; }
76 static const char* DefaultEvalMetric() { return "rmse"; }
77
78 static const char* Name() { return "reg:logistic"; }
79
80 static ObjInfo Info() { return ObjInfo::kRegression; }
81};
82
83// logistic loss for binary classification task
85 static const char* DefaultEvalMetric() { return "logloss"; }
86 static const char* Name() { return "binary:logistic"; }
87 static ObjInfo Info() { return ObjInfo::kBinary; }
88};
89
90// logistic loss, but predict un-transformed margin
92 XGBOOST_DEVICE static bst_float PredTransform(bst_float x) { return x; }
93 XGBOOST_DEVICE static bst_float FirstOrderGradient(bst_float predt, bst_float label) {
94 predt = common::Sigmoid(predt);
95 return predt - label;
96 }
97 XGBOOST_DEVICE static bst_float SecondOrderGradient(bst_float predt, bst_float) {
98 const float eps = 1e-16f;
99 predt = common::Sigmoid(predt);
100 return fmaxf(predt * (1.0f - predt), eps);
101 }
102 static bst_float ProbToMargin(bst_float base_score) { return base_score; }
103 static const char* DefaultEvalMetric() { return "logloss"; }
104
105 static const char* Name() { return "binary:logitraw"; }
106
107 static ObjInfo Info() { return ObjInfo::kRegression; }
108};
109} // namespace obj
110} // namespace xgboost
111
112#endif // XGBOOST_OBJECTIVE_REGRESSION_LOSS_H_
#define XGBOOST_DEVICE
Tag function as usable by device.
Definition base.h:64
Copyright 2015-2023 by XGBoost Contributors.
defines console logging options for xgboost. Use to enforce unified print behavior.
XGBOOST_DEVICE float Sigmoid(float x)
calculate the sigmoid of the input.
Definition math.h:28
namespace of xgboost
Definition base.h:90
float bst_float
float type, used for storing statistics
Definition base.h:97
header to handle OpenMP compatibility issues
A struct returned by objective, which determines task at hand. The struct is not used by any algorith...
Definition task.h:24
Definition regression_loss.h:20
Definition regression_loss.h:84
Definition regression_loss.h:91
Definition regression_loss.h:60
Definition regression_loss.h:35