-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTwoStageTrAdaBoostR2.py
385 lines (313 loc) · 16 KB
/
TwoStageTrAdaBoostR2.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"""
TwoStageTrAdaBoostR2 algorithm
based on algorithm 3 in paper "Boosting for Regression Transfer".
"""
import numpy as np
import copy
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold
################################################################################
## the second stage
################################################################################
class Stage2_TrAdaBoostR2:
def __init__(self,
base_estimator = DecisionTreeRegressor(max_depth=4),
sample_size = None,
n_estimators = 50,
learning_rate = 1.,
loss = 'linear',
random_state = np.random.mtrand._rand):
self.base_estimator = base_estimator
self.sample_size = sample_size
self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.loss = loss
self.random_state = random_state
def fit(self, X, y, sample_weight=None):
# Check parameters
if self.learning_rate <= 0:
raise ValueError("learning_rate must be greater than zero")
if sample_weight is None:
# Initialize weights to 1 / n_samples
sample_weight = np.empty(X.shape[0], dtype=np.float64)
sample_weight[:] = 1. / X.shape[0]
else:
# Normalize existing weights
sample_weight = sample_weight / sample_weight.sum(dtype=np.float64)
# Check that the sample weights sum is positive
if sample_weight.sum() <= 0:
raise ValueError(
"Attempting to fit with a non-positive "
"weighted number of samples.")
if self.sample_size is None:
raise ValueError("Additional input required: sample size of source and target is missing")
elif np.array(self.sample_size).sum() != X.shape[0]:
raise ValueError("Input error: the specified sample size does not equal to the input size")
# Clear any previous fit results
self.estimators_ = []
self.estimator_weights_ = np.zeros(self.n_estimators, dtype=np.float64)
self.estimator_errors_ = np.ones(self.n_estimators, dtype=np.float64)
for iboost in range(self.n_estimators): # this for loop is sequential and does not support parallel(revison is needed if making parallel)
# Boosting step
sample_weight, estimator_weight, estimator_error = self._stage2_adaboostR2(
iboost,
X, y,
sample_weight)
# Early termination
if sample_weight is None:
break
self.estimator_weights_[iboost] = estimator_weight
self.estimator_errors_[iboost] = estimator_error
# Stop if error is zero
if estimator_error == 0:
break
sample_weight_sum = np.sum(sample_weight)
# Stop if the sum of sample weights has become non-positive
if sample_weight_sum <= 0:
break
if iboost < self.n_estimators - 1:
# Normalize
sample_weight /= sample_weight_sum
return self
def _stage2_adaboostR2(self, iboost, X, y, sample_weight):
estimator = copy.deepcopy(self.base_estimator) # some estimators allow for specifying random_state estimator = base_estimator(random_state=random_state)
## using sampling method to account for sample_weight as discussed in Drucker's paper
# Weighted sampling of the training set with replacement
cdf = np.cumsum(sample_weight)
cdf /= cdf[-1]
uniform_samples = self.random_state.random_sample(X.shape[0])
bootstrap_idx = cdf.searchsorted(uniform_samples, side='right')
# searchsorted returns a scalar
bootstrap_idx = np.array(bootstrap_idx, copy=False)
# Fit on the bootstrapped sample and obtain a prediction
# for all samples in the training set
estimator.fit(X[bootstrap_idx], y[bootstrap_idx])
y_predict = estimator.predict(X)
self.estimators_.append(estimator) # add the fitted estimator
error_vect = np.abs(y_predict - y)
error_max = error_vect.max()
if error_max != 0.:
error_vect /= error_max
if self.loss == 'square':
error_vect **= 2
elif self.loss == 'exponential':
error_vect = 1. - np.exp(- error_vect)
# Calculate the average loss
estimator_error = (sample_weight * error_vect).sum()
if estimator_error <= 0:
# Stop if fit is perfect
return sample_weight, 1., 0.
elif estimator_error >= 0.5:
# Discard current estimator only if it isn't the only one
if len(self.estimators_) > 1:
self.estimators_.pop(-1)
return None, None, None
beta = estimator_error / (1. - estimator_error)
# avoid overflow of np.log(1. / beta)
if beta < 1e-308:
beta = 1e-308
estimator_weight = self.learning_rate * np.log(1. / beta)
# Boost weight using AdaBoost.R2 alg except the weight of the source data
# the weight of the source data are remained
source_weight_sum= np.sum(sample_weight[:-self.sample_size[-1]]) / np.sum(sample_weight)
target_weight_sum = np.sum(sample_weight[-self.sample_size[-1]:]) / np.sum(sample_weight)
if not iboost == self.n_estimators - 1:
sample_weight[-self.sample_size[-1]:] *= np.power(
beta,
(1. - error_vect[-self.sample_size[-1]:]) * self.learning_rate)
# make the sum weight of the source data not changing
source_weight_sum_new = np.sum(sample_weight[:-self.sample_size[-1]]) / np.sum(sample_weight)
target_weight_sum_new = np.sum(sample_weight[-self.sample_size[-1]:]) / np.sum(sample_weight)
if source_weight_sum_new != 0. and target_weight_sum_new != 0.:
sample_weight[:-self.sample_size[-1]] = sample_weight[:-self.sample_size[-1]]*source_weight_sum/source_weight_sum_new
sample_weight[-self.sample_size[-1]:] = sample_weight[-self.sample_size[-1]:]*target_weight_sum/target_weight_sum_new
return sample_weight, estimator_weight, estimator_error
def predict(self, X):
# Evaluate predictions of all estimators
predictions = np.array([
est.predict(X) for est in self.estimators_[:len(self.estimators_)]]).T
# Sort the predictions
sorted_idx = np.argsort(predictions, axis=1)
# Find index of median prediction for each sample
weight_cdf = np.cumsum(self.estimator_weights_[sorted_idx], axis=1)
median_or_above = weight_cdf >= 0.5 * weight_cdf[:, -1][:, np.newaxis]
median_idx = median_or_above.argmax(axis=1)
median_estimators = sorted_idx[np.arange(X.shape[0]), median_idx]
# Return median predictions
return predictions[np.arange(X.shape[0]), median_estimators]
################################################################################
## the whole two stages
################################################################################
class TwoStageTrAdaBoostR2:
def __init__(self,
base_estimator = DecisionTreeRegressor(max_depth=4),
sample_size = None,
n_estimators = 50,
steps = 10,
fold = 5,
learning_rate = 1.,
loss = 'linear',
random_state = np.random.mtrand._rand):
self.base_estimator = base_estimator
self.sample_size = sample_size
self.n_estimators = n_estimators
self.steps = steps
self.fold = fold
self.learning_rate = learning_rate
self.loss = loss
self.random_state = random_state
def fit(self, X, y, sample_weight=None):
# Check parameters
if self.learning_rate <= 0:
raise ValueError("learning_rate must be greater than zero")
if sample_weight is None:
# Initialize weights to 1 / n_samples
sample_weight = np.empty(X.shape[0], dtype=np.float64)
sample_weight[:] = 1. / X.shape[0]
else:
# Normalize existing weights
sample_weight = sample_weight / sample_weight.sum(dtype=np.float64)
# Check that the sample weights sum is positive
if sample_weight.sum() <= 0:
raise ValueError(
"Attempting to fit with a non-positive "
"weighted number of samples.")
if self.sample_size is None:
raise ValueError("Additional input required: sample size of source and target is missing")
elif np.array(self.sample_size).sum() != X.shape[0]:
raise ValueError("Input error: the specified sample size does not equal to the input size")
X_source = X[:-self.sample_size[-1]]
y_source = y[:-self.sample_size[-1]]
X_target = X[-self.sample_size[-1]:]
y_target = y[-self.sample_size[-1]:]
self.models_ = []
self.errors_ = []
for istep in range(self.steps):
model = Stage2_TrAdaBoostR2(self.base_estimator,
sample_size = self.sample_size,
n_estimators = self.n_estimators,
learning_rate = self.learning_rate, loss = self.loss,
random_state = self.random_state)
model.fit(X, y, sample_weight = sample_weight)
self.models_.append(model)
# cv training
kf = KFold(n_splits = self.fold)
error = []
target_weight = sample_weight[-self.sample_size[-1]:]
source_weight = sample_weight[:-self.sample_size[-1]]
for train, test in kf.split(X_target):
sample_size = [self.sample_size[0], len(train)]
model = Stage2_TrAdaBoostR2(self.base_estimator,
sample_size = sample_size,
n_estimators = self.n_estimators,
learning_rate = self.learning_rate, loss = self.loss,
random_state = self.random_state)
X_train = np.concatenate((X_source, X_target[train]))
y_train = np.concatenate((y_source, y_target[train]))
X_test = X_target[test]
y_test = y_target[test]
# make sure the sum weight of the target data do not change with CV's split sampling
target_weight_train = target_weight[train]*np.sum(target_weight)/np.sum(target_weight[train])
model.fit(X_train, y_train, sample_weight = np.concatenate((source_weight, target_weight_train)))
y_predict = model.predict(X_test)
error.append(mean_squared_error(y_predict, y_test))
self.errors_.append(np.array(error).mean())
sample_weight = self._twostage_adaboostR2(istep, X, y, sample_weight)
if sample_weight is None:
break
if np.array(error).mean() == 0:
break
sample_weight_sum = np.sum(sample_weight)
# Stop if the sum of sample weights has become non-positive
if sample_weight_sum <= 0:
break
if istep < self.steps - 1:
# Normalize
sample_weight /= sample_weight_sum
return self
def _twostage_adaboostR2(self, istep, X, y, sample_weight):
estimator = copy.deepcopy(self.base_estimator) # some estimators allow for specifying random_state estimator = base_estimator(random_state=random_state)
## using sampling method to account for sample_weight as discussed in Drucker's paper
# Weighted sampling of the training set with replacement
cdf = np.cumsum(sample_weight)
cdf /= cdf[-1]
uniform_samples = self.random_state.random_sample(X.shape[0])
bootstrap_idx = cdf.searchsorted(uniform_samples, side='right')
# searchsorted returns a scalar
bootstrap_idx = np.array(bootstrap_idx, copy=False)
# Fit on the bootstrapped sample and obtain a prediction
# for all samples in the training set
estimator.fit(X[bootstrap_idx], y[bootstrap_idx])
y_predict = estimator.predict(X)
error_vect = np.abs(y_predict - y)
error_max = error_vect.max()
if error_max != 0.:
error_vect /= error_max
if self.loss == 'square':
error_vect **= 2
elif self.loss == 'exponential':
error_vect = 1. - np.exp(- error_vect)
# Update the weight vector
beta = self._beta_binary_search(istep, sample_weight, error_vect, stp = 1e-30)
if not istep == self.steps - 1:
sample_weight[:-self.sample_size[-1]] *= np.power(
beta,
(error_vect[:-self.sample_size[-1]]) * self.learning_rate)
return sample_weight
def _beta_binary_search(self, istep, sample_weight, error_vect, stp):
# calculate the specified sum of weight for the target data
n_target = self.sample_size[-1]
n_source = np.array(self.sample_size).sum() - n_target
theoretical_sum = n_target/(n_source+n_target) + istep/(self.steps-1)*(1-n_target/(n_source+n_target))
# for the last iteration step, beta is 0.
if istep == self.steps - 1:
beta = 0.
return beta
# binary search for beta
L = 0.
R = 1.
beta = (L+R)/2
sample_weight_ = copy.deepcopy(sample_weight)
sample_weight_[:-n_target] *= np.power(
beta,
(error_vect[:-n_target]) * self.learning_rate)
sample_weight_ /= np.sum(sample_weight_, dtype=np.float64)
updated_weight_sum = np.sum(sample_weight_[-n_target:], dtype=np.float64)
while np.abs(updated_weight_sum - theoretical_sum) > 0.01:
if updated_weight_sum < theoretical_sum:
R = beta - stp
if R > L:
beta = (L+R)/2
sample_weight_ = copy.deepcopy(sample_weight)
sample_weight_[:-n_target] *= np.power(
beta,
(error_vect[:-n_target]) * self.learning_rate)
sample_weight_ /= np.sum(sample_weight_, dtype=np.float64)
updated_weight_sum = np.sum(sample_weight_[-n_target:], dtype=np.float64)
else:
print("At step:", istep+1)
print("Binary search's goal not meeted! Value is set to be the available best!")
print("Try reducing the search interval. Current stp interval:", stp)
break
elif updated_weight_sum > theoretical_sum:
L = beta + stp
if L < R:
beta = (L+R)/2
sample_weight_ = copy.deepcopy(sample_weight)
sample_weight_[:-n_target] *= np.power(
beta,
(error_vect[:-n_target]) * self.learning_rate)
sample_weight_ /= np.sum(sample_weight_, dtype=np.float64)
updated_weight_sum = np.sum(sample_weight_[-n_target:], dtype=np.float64)
else:
print("At step:", istep+1)
print("Binary search's goal not meeted! Value is set to be the available best!")
print("Try reducing the search interval. Current stp interval:", stp)
break
return beta
def predict(self, X):
# select the model with the least CV error
fmodel = self.models_[np.array(self.errors_).argmin()]
predictions = fmodel.predict(X)
return predictions