regression_metric.hpp 3.95 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef LIGHTGBM_METRIC_REGRESSION_METRIC_HPP_
#define LIGHTGBM_METRIC_REGRESSION_METRIC_HPP_

#include <LightGBM/utils/log.h>

#include <LightGBM/metric.h>

#include <cmath>

namespace LightGBM {
/*!
* \brief Metric for regression task.
* Use static class "PointWiseLossCalculator" to calculate loss point-wise
*/
template<typename PointWiseLossCalculator>
class RegressionMetric: public Metric {
public:
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
18
  explicit RegressionMetric(const MetricConfig&) :huber_delta_(1.0f) {
Guolin Ke's avatar
Guolin Ke committed
19
20
21
22
23
24
  }

  virtual ~RegressionMetric() {

  }

Guolin Ke's avatar
Guolin Ke committed
25
  const std::vector<std::string>& GetName() const override {
26
    return name_;
27
28
  }

29
30
  score_t factor_to_bigger_better() const override {
    return -1.0f;
31
32
  }

Guolin Ke's avatar
Guolin Ke committed
33
34
  void Init(const Metadata& metadata, data_size_t num_data) override {
    name_.emplace_back(PointWiseLossCalculator::Name());
35

Guolin Ke's avatar
Guolin Ke committed
36
37
38
39
40
41
    num_data_ = num_data;
    // get label
    label_ = metadata.label();
    // get weights
    weights_ = metadata.weights();
    if (weights_ == nullptr) {
42
      sum_weights_ = static_cast<double>(num_data_);
Guolin Ke's avatar
Guolin Ke committed
43
44
45
46
47
48
49
    } else {
      sum_weights_ = 0.0f;
      for (data_size_t i = 0; i < num_data_; ++i) {
        sum_weights_ += weights_[i];
      }
    }
  }
50

51
52
  std::vector<double> Eval(const score_t* score) const override {
    double sum_loss = 0.0f;
53
54
55
56
    if (weights_ == nullptr) {
#pragma omp parallel for schedule(static) reduction(+:sum_loss)
      for (data_size_t i = 0; i < num_data_; ++i) {
        // add loss
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
57
        sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], score[i], huber_delta_);
Guolin Ke's avatar
Guolin Ke committed
58
      }
59
60
61
62
    } else {
#pragma omp parallel for schedule(static) reduction(+:sum_loss)
      for (data_size_t i = 0; i < num_data_; ++i) {
        // add loss
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
63
        sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], score[i], huber_delta_) * weights_[i];
wxchan's avatar
wxchan committed
64
      }
Guolin Ke's avatar
Guolin Ke committed
65
    }
66
67
    double loss = PointWiseLossCalculator::AverageLoss(sum_loss, sum_weights_);
    return std::vector<double>(1, loss);
68

Guolin Ke's avatar
Guolin Ke committed
69
70
  }

71
  inline static double AverageLoss(double sum_loss, double sum_weights) {
Guolin Ke's avatar
Guolin Ke committed
72
73
74
    return sum_loss / sum_weights;
  }

Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
75
76
77
78
protected:
  /*! \brief delta for Huber loss */
  double huber_delta_;

Guolin Ke's avatar
Guolin Ke committed
79
80
81
82
83
84
85
86
private:
  /*! \brief Number of data */
  data_size_t num_data_;
  /*! \brief Pointer of label */
  const float* label_;
  /*! \brief Pointer of weighs */
  const float* weights_;
  /*! \brief Sum weights */
87
  double sum_weights_;
Guolin Ke's avatar
Guolin Ke committed
88
  /*! \brief Name of this test set */
89
  std::vector<std::string> name_;
Guolin Ke's avatar
Guolin Ke committed
90
91
92
93
94
95
96
};

/*! \brief L2 loss for regression task */
class L2Metric: public RegressionMetric<L2Metric> {
public:
  explicit L2Metric(const MetricConfig& config) :RegressionMetric<L2Metric>(config) {}

Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
97
  inline static score_t LossOnPoint(float label, score_t score, float) {
Guolin Ke's avatar
Guolin Ke committed
98
99
100
    return (score - label)*(score - label);
  }

101
  inline static double AverageLoss(double sum_loss, double sum_weights) {
Guolin Ke's avatar
Guolin Ke committed
102
103
104
105
106
    // need sqrt the result for L2 loss
    return std::sqrt(sum_loss / sum_weights);
  }

  inline static const char* Name() {
107
    return "l2";
Guolin Ke's avatar
Guolin Ke committed
108
109
110
111
112
113
114
115
  }
};

/*! \brief L1 loss for regression task */
class L1Metric: public RegressionMetric<L1Metric> {
public:
  explicit L1Metric(const MetricConfig& config) :RegressionMetric<L1Metric>(config) {}

Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
116
  inline static score_t LossOnPoint(float label, score_t score, float) {
Guolin Ke's avatar
Guolin Ke committed
117
118
119
    return std::fabs(score - label);
  }
  inline static const char* Name() {
120
    return "l1";
Guolin Ke's avatar
Guolin Ke committed
121
122
123
  }
};

Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*! \brief Huber loss for regression task */
class HuberLossMetric: public RegressionMetric<HuberLossMetric> {
public:
    explicit HuberLossMetric(const MetricConfig& config) :RegressionMetric<HuberLossMetric>(config) {
        huber_delta_ = config.huber_delta;
    }

    inline static score_t LossOnPoint(float label, score_t score, float delta) {
        const double diff = score - label;
        if (std::abs(diff) <= delta) {
            return 0.5 * diff * diff;
        } else {
            return delta * (std::abs(diff) - 0.5 * delta);
        }
    }

    inline static const char* Name() {
        return "huber";
    }
};

Guolin Ke's avatar
Guolin Ke committed
145
}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
146
#endif   // LightGBM_METRIC_REGRESSION_METRIC_HPP_