multiclass_metric.hpp 5.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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:
Guolin Ke's avatar
Guolin Ke committed
18
19
  explicit MulticlassMetric(const MetricConfig& config) {
    num_class_ = config.num_class;
20
21
22
23
24
25
  }

  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
    } else {
      sum_weights_ = 0.0f;
      for (data_size_t i = 0; i < num_data_; ++i) {
        sum_weights_ += weights_[i];
      }
    }
  }
43

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

Guolin Ke's avatar
Guolin Ke committed
52
  std::vector<double> Eval(const double* score, const ObjectiveFunction* objective) const override {
53
    double sum_loss = 0.0;
Guolin Ke's avatar
Guolin Ke committed
54
55
56
    int num_tree_per_iteration = num_class_;
    int num_pred_per_row = num_class_;
    if (objective != nullptr) {
Guolin Ke's avatar
Guolin Ke committed
57
      num_tree_per_iteration = objective->NumModelPerIteration();
Guolin Ke's avatar
Guolin Ke committed
58
59
      num_pred_per_row = objective->NumPredictOneRow();
    }
60
61
62
63
    if (objective != nullptr) {
      if (weights_ == nullptr) {
        #pragma omp parallel for schedule(static) reduction(+:sum_loss)
        for (data_size_t i = 0; i < num_data_; ++i) {
Guolin Ke's avatar
Guolin Ke committed
64
          std::vector<double> raw_score(num_tree_per_iteration);
65
66
          for (int k = 0; k < num_tree_per_iteration; ++k) {
            size_t idx = static_cast<size_t>(num_data_) * k + i;
Guolin Ke's avatar
Guolin Ke committed
67
            raw_score[k] = static_cast<double>(score[idx]);
68
          }
Guolin Ke's avatar
Guolin Ke committed
69
70
          std::vector<double> rec(num_pred_per_row);
          objective->ConvertOutput(raw_score.data(), rec.data());
71
72
73
74
75
76
          // 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) {
Guolin Ke's avatar
Guolin Ke committed
77
          std::vector<double> raw_score(num_tree_per_iteration);
78
79
          for (int k = 0; k < num_tree_per_iteration; ++k) {
            size_t idx = static_cast<size_t>(num_data_) * k + i;
Guolin Ke's avatar
Guolin Ke committed
80
            raw_score[k] = static_cast<double>(score[idx]);
81
          }
Guolin Ke's avatar
Guolin Ke committed
82
83
          std::vector<double> rec(num_pred_per_row);
          objective->ConvertOutput(raw_score.data(), rec.data());
84
85
          // add loss
          sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], rec) * weights_[i];
86
87
88
        }
      }
    } else {
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      if (weights_ == nullptr) {
        #pragma omp parallel for schedule(static) reduction(+:sum_loss)
        for (data_size_t i = 0; i < num_data_; ++i) {
          std::vector<double> rec(num_tree_per_iteration);
          for (int k = 0; k < num_tree_per_iteration; ++k) {
            size_t idx = static_cast<size_t>(num_data_) * k + i;
            rec[k] = static_cast<double>(score[idx]);
          }
          // 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) {
          std::vector<double> rec(num_tree_per_iteration);
          for (int k = 0; k < num_tree_per_iteration; ++k) {
            size_t idx = static_cast<size_t>(num_data_) * k + i;
            rec[k] = static_cast<double>(score[idx]);
          }
          // add loss
          sum_loss += PointWiseLossCalculator::LossOnPoint(label_[i], rec) * weights_[i];
110
111
112
        }
      }
    }
113
114
    double loss = sum_loss / sum_weights_;
    return std::vector<double>(1, loss);
115
116
117
118
119
120
  }

private:
  /*! \brief Number of data */
  data_size_t num_data_;
  /*! \brief Pointer of label */
121
  const label_t* label_;
122
  /*! \brief Pointer of weighs */
123
  const label_t* weights_;
124
  /*! \brief Sum weights */
125
  double sum_weights_;
126
  /*! \brief Name of this test set */
127
  std::vector<std::string> name_;
Guolin Ke's avatar
Guolin Ke committed
128
  int num_class_;
129
130
131
132
133
134
135
};

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

136
  inline static double LossOnPoint(label_t label, std::vector<double>& score) {
137
    size_t k = static_cast<size_t>(label);
138
139
140
141
    for (size_t i = 0; i < score.size(); ++i) {
      if (i != k && score[i] >= score[k]) {
        return 1.0f;
      }
142
    }
Guolin Ke's avatar
Guolin Ke committed
143
    return 0.0f;
144
145
146
  }

  inline static const char* Name() {
147
    return "multi_error";
148
149
150
151
  }
};

/*! \brief Logloss for multiclass task */
Guolin Ke's avatar
Guolin Ke committed
152
class MultiSoftmaxLoglossMetric: public MulticlassMetric<MultiSoftmaxLoglossMetric> {
153
public:
Guolin Ke's avatar
Guolin Ke committed
154
  explicit MultiSoftmaxLoglossMetric(const MetricConfig& config) :MulticlassMetric<MultiSoftmaxLoglossMetric>(config) {}
155

156
  inline static double LossOnPoint(label_t label, std::vector<double>& score) {
157
158
    size_t k = static_cast<size_t>(label);
    if (score[k] > kEpsilon) {
159
      return static_cast<double>(-std::log(score[k]));
160
161
162
163
    } else {
      return -std::log(kEpsilon);
    }
  }
164

165
  inline static const char* Name() {
166
    return "multi_logloss";
167
168
169
170
171
  }
};

}  // namespace LightGBM
#endif   // LightGBM_METRIC_MULTICLASS_METRIC_HPP_