This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
boost_test.cc
261 lines (239 loc) · 9.93 KB
/
boost_test.cc
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
133
134
135
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Copyright 2015 Google Inc. All rights reserved.
Licensed 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.
*/
#include <math.h>
#include "boost.h"
#include "tree.h" // TODO(usyed): Figure out how not to have to include this.
#include "srm_test.h"
#include "gflags/gflags.h"
#include "gtest/gtest.h"
DECLARE_int32(tree_depth);
DECLARE_double(beta);
DECLARE_double(lambda);
DECLARE_string(loss_type);
class BoostTest : public SrmTest {
protected:
virtual void SetUp() {
SrmTest::SetUp();
InitializeTreeData(examples_, examples_.size());
}
};
TEST_F(BoostTest, TestAddTreeToModel) {
FLAGS_tree_depth = 1;
FLAGS_beta = 0;
FLAGS_lambda = 0;
FLAGS_loss_type = "exponential";
Model model;
// Train a model with a single tree. The tree's weighted error will be 0.2,
// and it will only get example 3 wrong.
AddTreeToModel(examples_, &model);
// Every example is originally weighted equally.
const float original_wgt = 0.2;
// alpha = 0.5 * log((1 - error) / error), where error = 0.2.
float alpha = 0.69314718056;
// Normalizer is sum of all adjusted weights.
float normalizer = 4 * original_wgt * exp(-alpha) + original_wgt * exp(alpha);
// Adjust weights and normalize.
float correct_wgt = original_wgt * exp(-alpha) / normalizer;
float incorrect_wgt = original_wgt * exp(alpha) / normalizer;
EXPECT_NEAR(correct_wgt, examples_[0].weight, kTolerance);
EXPECT_NEAR(correct_wgt, examples_[1].weight, kTolerance);
EXPECT_NEAR(correct_wgt, examples_[2].weight, kTolerance);
EXPECT_NEAR(incorrect_wgt, examples_[3].weight, kTolerance);
EXPECT_NEAR(correct_wgt, examples_[4].weight, kTolerance);
// Add another tree to the model. The tree's weighted error will be 0.125, and
// it will only get example 4 wrong.
AddTreeToModel(examples_, &model);
// alpha = 0.5 * log((1 - error) / error), where error = 0.125.
alpha = 0.97295507452;
// Normalizer is sum of all adjusted weights.
normalizer = 3 * correct_wgt * exp(-alpha) + correct_wgt * exp(alpha) +
incorrect_wgt * exp(-alpha);
float both_correct_wgt = correct_wgt * exp(-alpha) / normalizer;
float first_correct_wgt = correct_wgt * exp(alpha) / normalizer;
float second_correct_wgt = incorrect_wgt * exp(-alpha) / normalizer;
EXPECT_NEAR(both_correct_wgt, examples_[0].weight, kTolerance);
EXPECT_NEAR(both_correct_wgt, examples_[1].weight, kTolerance);
EXPECT_NEAR(both_correct_wgt, examples_[2].weight, kTolerance);
EXPECT_NEAR(second_correct_wgt, examples_[3].weight, kTolerance);
EXPECT_NEAR(first_correct_wgt, examples_[4].weight, kTolerance);
}
TEST_F(BoostTest, TestClassifyExampleDepthOne) {
FLAGS_tree_depth = 1;
FLAGS_beta = 0;
FLAGS_lambda = 0;
FLAGS_loss_type = "exponential";
Model model;
AddTreeToModel(examples_, &model);
AddTreeToModel(examples_, &model);
// By the previous test, the first tree gets example 3 wrong and has weight
// 0.69314718056, and the second tree has weight gets example 4 wrong and has
// weight 0.97295507452. Since 0.97295507452 > 0.69314718056, the second tree
// outvotes the first on all examples, so their combination gets example 4
// wrong.
EXPECT_EQ(examples_[0].label, ClassifyExample(examples_[0], model));
EXPECT_EQ(examples_[1].label, ClassifyExample(examples_[1], model));
EXPECT_EQ(examples_[2].label, ClassifyExample(examples_[2], model));
EXPECT_EQ(examples_[3].label, ClassifyExample(examples_[3], model));
EXPECT_EQ(-examples_[4].label, ClassifyExample(examples_[4], model));
}
TEST_F(BoostTest, TestClassifyExampleDepthTwo) {
FLAGS_tree_depth = 2;
FLAGS_beta = 0;
FLAGS_lambda = 0;
FLAGS_loss_type = "exponential";
Model model;
AddTreeToModel(examples_, &model);
// Depth 2 trees can classify all examples perfectly.
EXPECT_EQ(examples_[0].label, ClassifyExample(examples_[0], model));
EXPECT_EQ(examples_[1].label, ClassifyExample(examples_[1], model));
EXPECT_EQ(examples_[2].label, ClassifyExample(examples_[2], model));
EXPECT_EQ(examples_[3].label, ClassifyExample(examples_[3], model));
EXPECT_EQ(examples_[4].label, ClassifyExample(examples_[4], model));
const float alpha = model[0].first;
// Won't actually add trees, will just increase weight on current tree.
for (int i = 0; i < 99; ++i) {
AddTreeToModel(examples_, &model);
}
EXPECT_EQ(1, model.size());
EXPECT_NEAR(alpha, model[0].first / 100, kTolerance * 100);
EXPECT_EQ(examples_[0].label, ClassifyExample(examples_[0], model));
EXPECT_EQ(examples_[1].label, ClassifyExample(examples_[1], model));
EXPECT_EQ(examples_[2].label, ClassifyExample(examples_[2], model));
EXPECT_EQ(examples_[3].label, ClassifyExample(examples_[3], model));
EXPECT_EQ(examples_[4].label, ClassifyExample(examples_[4], model));
}
TEST_F(BoostTest, TestClassifyExampleEmptyModel) {
Model model;
// Empty model classifies every example as positive
EXPECT_EQ(1, ClassifyExample(examples_[0], model));
EXPECT_EQ(1, ClassifyExample(examples_[1], model));
EXPECT_EQ(1, ClassifyExample(examples_[2], model));
EXPECT_EQ(1, ClassifyExample(examples_[3], model));
EXPECT_EQ(1, ClassifyExample(examples_[4], model));
}
TEST_F(BoostTest, TestEvaluateModelDepthOne) {
FLAGS_tree_depth = 1;
FLAGS_beta = 0;
FLAGS_lambda = 0;
FLAGS_loss_type = "exponential";
Model model;
AddTreeToModel(examples_, &model);
AddTreeToModel(examples_, &model);
float error, avg_tree_size;
int num_trees;
EvaluateModel(examples_, model, &error, &avg_tree_size, &num_trees);
EXPECT_NEAR(0.2, error, kTolerance);
EXPECT_EQ(2, num_trees);
EXPECT_NEAR(3, avg_tree_size, kTolerance);
}
TEST_F(BoostTest, TestEvaluateModelDepthTwo) {
FLAGS_tree_depth = 2;
FLAGS_beta = 0;
FLAGS_lambda = 0;
FLAGS_loss_type = "exponential";
Model model;
AddTreeToModel(examples_, &model);
float error, avg_tree_size;
int num_trees;
EvaluateModel(examples_, model, &error, &avg_tree_size, &num_trees);
EXPECT_NEAR(0.0, error, kTolerance);
EXPECT_EQ(1, num_trees);
EXPECT_NEAR(5, avg_tree_size, kTolerance);
}
TEST_F(BoostTest, ComputeEtaTest) {
FLAGS_beta = 1;
FLAGS_lambda = 1;
float eta = ComputeEta(1, 10, 1);
EXPECT_NEAR(-1, eta, kTolerance);
FLAGS_beta = 1;
FLAGS_lambda = 0;
eta = ComputeEta(0.1, 5, 2);
float ratio = ComplexityPenalty(5) / 0.1;
EXPECT_NEAR(log(-ratio + sqrt(ratio * ratio + (0.9 / 0.1))), eta, kTolerance);
FLAGS_beta = 0;
FLAGS_lambda = 1;
eta = ComputeEta(0.75, 10, -10);
ratio = ComplexityPenalty(10) / 0.75;
EXPECT_NEAR(log(ratio + sqrt(ratio * ratio + (0.25 / 0.75))),
eta, kTolerance);
}
TEST_F(BoostTest, TestAddTreeToModelLargeBeta) {
// Large beta penalty means two trees with zero weight and depth 0.
FLAGS_tree_depth = 1;
FLAGS_beta = 100;
FLAGS_lambda = 0;
FLAGS_loss_type = "exponential";
Model model;
AddTreeToModel(examples_, &model);
AddTreeToModel(examples_, &model);
EXPECT_EQ(2, model.size());
EXPECT_LT(model[0].first, kTolerance);
EXPECT_LT(model[1].first, kTolerance);
EXPECT_EQ(1, model[0].second.size());
EXPECT_EQ(1, model[1].second.size());
}
TEST_F(BoostTest, TestAddTreeToModelLargeLambda) {
// Large lambda penalty means two trees with zero weight and depth 0.
FLAGS_tree_depth = 1;
FLAGS_beta = 0;
FLAGS_lambda = 100;
FLAGS_loss_type = "exponential";
Model model;
AddTreeToModel(examples_, &model);
AddTreeToModel(examples_, &model);
EXPECT_EQ(2, model.size());
EXPECT_LT(model[0].first, kTolerance);
EXPECT_LT(model[1].first, kTolerance);
EXPECT_EQ(1, model[0].second.size());
EXPECT_EQ(1, model[1].second.size());
}
TEST_F(BoostTest, TestAddTreeToModelLogisticLoss) {
FLAGS_tree_depth = 1;
FLAGS_beta = 0;
FLAGS_lambda = 0;
FLAGS_loss_type = "logistic";
Model model;
// Train a model with a single tree. The tree's weighted error will be 0.2,
// and it will only get example 3 wrong.
AddTreeToModel(examples_, &model);
// alpha1 = 0.5 * log((1 - error) / error), where error = 0.2.
float alpha1 = 0.69314718056;
// Normalizer is sum of all adjusted weights.
float normalizer =
4 * (1 / (1 + exp(alpha1 - 1))) + (1 / (1 + exp(-alpha1 - 1)));
// Adjust weights and normalize.
float correct_wgt = (1 / (1 + exp(alpha1 - 1))) / normalizer;
float incorrect_wgt = (1 / (1 + exp(-alpha1 - 1))) / normalizer;
EXPECT_NEAR(correct_wgt, examples_[0].weight, kTolerance);
EXPECT_NEAR(correct_wgt, examples_[1].weight, kTolerance);
EXPECT_NEAR(correct_wgt, examples_[2].weight, kTolerance);
EXPECT_NEAR(incorrect_wgt, examples_[3].weight, kTolerance);
EXPECT_NEAR(correct_wgt, examples_[4].weight, kTolerance);
// Add another tree to the model. The tree's weighted error will be
// 0.182946235, and it will only get example 4 wrong.
AddTreeToModel(examples_, &model);
// alpha2 = 0.5 * log((1 - error) / error), where error = 0.182946235.
float alpha2 = 0.7482563445;
// Normalizer is sum of all adjusted weights.
normalizer = 3 * (1 / (1 + exp(alpha1 + alpha2 - 1))) +
(1 / (1 + exp(alpha1 - alpha2 - 1))) +
(1 / (1 + exp(-alpha1 + alpha2 - 1)));
float both_correct_wgt = (1 / (1 + exp(alpha1 + alpha2 - 1))) / normalizer;
float first_correct_wgt = (1 / (1 + exp(alpha1 - alpha2 - 1))) / normalizer;
float second_correct_wgt = (1 / (1 + exp(-alpha1 + alpha2 - 1))) / normalizer;
EXPECT_NEAR(both_correct_wgt, examples_[0].weight, kTolerance);
EXPECT_NEAR(both_correct_wgt, examples_[1].weight, kTolerance);
EXPECT_NEAR(both_correct_wgt, examples_[2].weight, kTolerance);
EXPECT_NEAR(second_correct_wgt, examples_[3].weight, kTolerance);
EXPECT_NEAR(first_correct_wgt, examples_[4].weight, kTolerance);
}