-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGbmFun.h
132 lines (102 loc) · 3.42 KB
/
GbmFun.h
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Copyright 2015,2016 Tao Xu
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#pragma once
#include <boost/scoped_array.hpp>
#include <vector>
namespace boosting {
// Implementing a few simple function could extend Gbm to different
// loss functions, like l2 loss (least square), logloss (logistic
// regression), huber loss (robust regression), lambdaloss (lambda
// rank), etc.
class GbmFun {
public:
virtual double getLeafVal(const std::vector<int>& subset,
const boost::scoped_array<double>& y) const = 0;
virtual double getF0(const std::vector<double>& y) const = 0;
virtual void getGradient(const std::vector<double>& y,
const boost::scoped_array<double>& F,
boost::scoped_array<double>& grad) const = 0;
virtual double getInitLoss(const std::vector<double>& y) const = 0;
virtual double getExampleLoss(const double y, const double f) const = 0;
virtual void accumulateExampleLoss(const double y, const double f) = 0;
virtual double getReduction() const = 0;
virtual int getNumExamples() const = 0;
virtual double getLoss() const = 0;
};
class LeastSquareFun : public GbmFun {
public:
LeastSquareFun() : numExamples_(0), sumy_(0.0), sumy2_(0.0), l2_(0.0) {
}
double getLeafVal(const std::vector<int>& subset,
const boost::scoped_array<double>& y) const {
double sum = 0;
for (const auto& id : subset) {
sum += y[id];
}
return sum/subset.size();
}
double getF0(const std::vector<double>& yvec) const {
double sum = 0.0;
for (const auto& y : yvec) {
sum += y;
}
return sum/yvec.size();
}
void getGradient(const std::vector<double>& y,
const boost::scoped_array<double>& F,
boost::scoped_array<double>& grad) const {
int size = y.size();
for (int i = 0; i < size; i++) {
grad[i] = y[i] - F[i];
}
}
double getInitLoss(const std::vector<double>& yvec) const {
double sumy = 0.0;
double sumy2 = 0.0;
for (const auto& y : yvec) {
sumy += y;
sumy2 += y*y;
}
return sumy2 - sumy * sumy/yvec.size();
}
double getExampleLoss(const double y, const double f) const {
return (y - f) * (y - f);
}
void accumulateExampleLoss(const double y, const double f) {
sumy_ += y;
numExamples_ += 1;
sumy2_ += y * y;
l2_ += getExampleLoss(y, f);
}
double getReduction() const {
return 1.0 - l2_/(sumy2_ - sumy_ * sumy_/numExamples_);
}
int getNumExamples() const {
return numExamples_;
}
double getLoss() const {
return l2_;
}
private:
int numExamples_;
double sumy_;
double sumy2_;
double l2_;
};
}