multiclass_metric.hpp 6.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef LIGHTGBM_METRIC_MULTICLASS_METRIC_HPP_
#define LIGHTGBM_METRIC_MULTICLASS_METRIC_HPP_

#include <LightGBM/utils/log.h>

#include <LightGBM/metric.h>

#include <cmath>

namespace LightGBM {
/*!
* \brief Metric for multiclass task.
* Use static class "PointWiseLossCalculator" to calculate loss point-wise
*/
template<typename PointWiseLossCalculator>
class MulticlassMetric: public Metric {
public:
  explicit MulticlassMetric(const MetricConfig& config) {
      num_class_ = config.num_class;
  }

  virtual ~MulticlassMetric() {

  }

Guolin Ke's avatar
Guolin Ke committed
26
27
28
  void Init(const Metadata& metadata, data_size_t num_data) override {

    name_.emplace_back(PointWiseLossCalculator::Name());
29
30
31
32
33
34
    num_data_ = num_data;
    // get label
    label_ = metadata.label();
    // get weights
    weights_ = metadata.weights();
    if (weights_ == nullptr) {
35
      sum_weights_ = static_cast<double>(num_data_);
36
37
38
39
40
41
42
43
    } else {
      sum_weights_ = 0.0f;
      for (data_size_t i = 0; i < num_data_; ++i) {
        sum_weights_ += weights_[i];
      }
    }
  }
  
Guolin Ke's avatar
Guolin Ke committed
44
  const std::vector<std::string>& GetName() const override {
45
    return name_;
46
47
  }

48
  double factor_to_bigger_better() const override {
49
    return -1.0f;
50
51
  }
  
52
  std::vector<double> Eval(const double* score) const override {
53
    double sum_loss = 0.0;
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) {
57
        std::vector<double> rec(num_class_);
58
        for (int k = 0; k < num_class_; ++k) {
59
60
          size_t idx = static_cast<size_t>(num_data_) * k + i;
          rec[k] = static_cast<double>(score[idx]);
61
62
63
64
65
66
67
        }
        // add loss
        sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], rec);
      }
    } else {
      #pragma omp parallel for schedule(static) reduction(+:sum_loss)
      for (data_size_t i = 0; i < num_data_; ++i) {
68
        std::vector<double> rec(num_class_);
69
        for (int k = 0; k < num_class_; ++k) {
70
71
          size_t idx = static_cast<size_t>(num_data_) * k + i;
          rec[k] = static_cast<double>(score[idx]);
72
73
74
75
76
        }
        // add loss
        sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], rec) * weights_[i];
      }
    }
77
78
    double loss = sum_loss / sum_weights_;
    return std::vector<double>(1, loss);
79
80
81
82
83
84
85
86
87
88
89
90
  }

private:
  /*! \brief Number of data */
  data_size_t num_data_;
  /*! \brief Number of classes */
  int num_class_;
  /*! \brief Pointer of label */
  const float* label_;
  /*! \brief Pointer of weighs */
  const float* weights_;
  /*! \brief Sum weights */
91
  double sum_weights_;
92
  /*! \brief Name of this test set */
93
  std::vector<std::string> name_;
94
95
96
97
98
99
100
};

/*! \brief L2 loss for multiclass task */
class MultiErrorMetric: public MulticlassMetric<MultiErrorMetric> {
public:
  explicit MultiErrorMetric(const MetricConfig& config) :MulticlassMetric<MultiErrorMetric>(config) {}

zhangyafeikimi's avatar
zhangyafeikimi committed
101
  inline static double LossOnPoint(float label, std::vector<double>& score) {
102
103
    size_t k = static_cast<size_t>(label);
    for (size_t i = 0; i < score.size(); ++i){
Guolin Ke's avatar
Guolin Ke committed
104
105
        if (i != k && score[i] >= score[k]) {
            return 1.0f;
106
107
        }
    }
Guolin Ke's avatar
Guolin Ke committed
108
    return 0.0f;
109
110
111
  }

  inline static const char* Name() {
112
    return "multi_error";
113
114
115
116
  }
};

/*! \brief Logloss for multiclass task */
Guolin Ke's avatar
Guolin Ke committed
117
class MultiSoftmaxLoglossMetric: public MulticlassMetric<MultiSoftmaxLoglossMetric> {
118
public:
Guolin Ke's avatar
Guolin Ke committed
119
  explicit MultiSoftmaxLoglossMetric(const MetricConfig& config) :MulticlassMetric<MultiSoftmaxLoglossMetric>(config) {}
120

zhangyafeikimi's avatar
zhangyafeikimi committed
121
  inline static double LossOnPoint(float label, std::vector<double>& score) {
122
123
124
    size_t k = static_cast<size_t>(label);
    Common::Softmax(&score);
    if (score[k] > kEpsilon) {
125
      return static_cast<double>(-std::log(score[k]));
126
127
128
129
130
131
    } else {
      return -std::log(kEpsilon);
    }
  }
  
  inline static const char* Name() {
132
    return "multi_logloss";
133
134
135
  }
};

Guolin Ke's avatar
Guolin Ke committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
class MultiOVALoglossMetric: public Metric {
public:
  explicit MultiOVALoglossMetric(const MetricConfig& config) {
    num_class_ = config.num_class;
    sigmoid_ = config.sigmoid;
  }

  virtual ~MultiOVALoglossMetric() {

  }

  void Init(const Metadata& metadata, data_size_t num_data) override {

    name_.emplace_back("multi_loglossova");
    num_data_ = num_data;
    // get label
    label_ = metadata.label();
    // get weights
    weights_ = metadata.weights();
    if (weights_ == nullptr) {
      sum_weights_ = static_cast<double>(num_data_);
    } else {
      sum_weights_ = 0.0f;
      for (data_size_t i = 0; i < num_data_; ++i) {
        sum_weights_ += weights_[i];
      }
    }
  }

  const std::vector<std::string>& GetName() const override {
    return name_;
  }

  double factor_to_bigger_better() const override {
    return -1.0f;
  }

  std::vector<double> Eval(const double* score) const override {
    double sum_loss = 0.0;
    if (weights_ == nullptr) {
      #pragma omp parallel for schedule(static) reduction(+:sum_loss)
      for (data_size_t i = 0; i < num_data_; ++i) {
        size_t idx = static_cast<size_t>(num_data_) * static_cast<int>(label_[i]) + i;
        double prob = 1.0f / (1.0f + std::exp(-sigmoid_ * score[idx]));
        if (prob < kEpsilon) { prob = kEpsilon; }
        // add loss
        sum_loss += -std::log(prob);
      }
    } else {
      #pragma omp parallel for schedule(static) reduction(+:sum_loss)
      for (data_size_t i = 0; i < num_data_; ++i) {
        size_t idx = static_cast<size_t>(num_data_) * static_cast<int>(label_[i]) + i;
        double prob = 1.0f / (1.0f + std::exp(-sigmoid_ * score[idx]));
        if (prob < kEpsilon) { prob = kEpsilon; }
        // add loss
        sum_loss += -std::log(prob) * weights_[i];
      }
    }
    double loss = sum_loss / sum_weights_;
    return std::vector<double>(1, loss);
  }

private:
  /*! \brief Number of data */
  data_size_t num_data_;
  /*! \brief Number of classes */
  int num_class_;
  /*! \brief Pointer of label */
  const float* label_;
  /*! \brief Pointer of weighs */
  const float* weights_;
  /*! \brief Sum weights */
  double sum_weights_;
  /*! \brief Name of this test set */
  std::vector<std::string> name_;
  double sigmoid_;
};

214
215
}  // namespace LightGBM
#endif   // LightGBM_METRIC_MULTICLASS_METRIC_HPP_