| 23 | } |
| 24 | |
| 25 | PredictionEarlyStopInstance CreateMulticlass(const PredictionEarlyStopConfig& config) { |
| 26 | // margin_threshold will be captured by value |
| 27 | const double margin_threshold = config.margin_threshold; |
| 28 | |
| 29 | return PredictionEarlyStopInstance{ |
| 30 | [margin_threshold](const double* pred, int sz) { |
| 31 | if (sz < 2) { |
| 32 | Log::Fatal("Multiclass early stopping needs predictions to be of length two or larger"); |
| 33 | } |
| 34 | |
| 35 | // copy and sort |
| 36 | std::vector<double> votes(static_cast<size_t>(sz)); |
| 37 | for (int i = 0; i < sz; ++i) { |
| 38 | votes[i] = pred[i]; |
| 39 | } |
| 40 | std::partial_sort(votes.begin(), votes.begin() + 2, votes.end(), std::greater<double>()); |
| 41 | |
| 42 | const auto margin = votes[0] - votes[1]; |
| 43 | |
| 44 | if (margin > margin_threshold) { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | return false; |
| 49 | }, |
| 50 | config.round_period |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | PredictionEarlyStopInstance CreateBinary(const PredictionEarlyStopConfig& config) { |
| 55 | // margin_threshold will be captured by value |
no test coverage detected