-
Notifications
You must be signed in to change notification settings - Fork 0
/
scikit_wrappers.py
608 lines (555 loc) · 25 KB
/
scikit_wrappers.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import math
import numpy
import torch
import sklearn
import utils
import losses
import networks
class TimeSeriesEncoderClassifier(sklearn.base.BaseEstimator,
sklearn.base.ClassifierMixin):
"""
"Virtual" class to wrap an encoder of time series as a PyTorch module and
a SVM classifier with RBF kernel on top of its computed representations in
a scikit-learn class.
All inheriting classes should implement the get_params and set_params
methods, as in the recommendations of scikit-learn.
@param compared_length Maximum length of randomly chosen time series. If
None, this parameter is ignored.
@param nb_random_samples Number of randomly chosen intervals to select the
final negative sample in the loss.
@param negative_penalty Multiplicative coefficient for the negative sample
loss.
@param batch_size Batch size used during the training of the encoder.
@param nb_steps Number of optimization steps to perform for the training of
the encoder.
@param lr learning rate of the Adam optimizer used to train the encoder.
@param penalty Penalty term for the SVM classifier. If None and if the
number of samples is high enough, performs a hyperparameter search
to find a suitable constant.
@param early_stopping Enables, if not None, early stopping heuristic
for the training of the representations, based on the final
score. Representations are still learned unsupervisedly in this
case. If the number of samples per class is no more than 10,
disables this heuristic. If not None, accepts an integer
representing the patience of the early stopping strategy.
@param encoder Encoder PyTorch module.
@param params Dictionaries of the parameters of the encoder.
@param in_channels Number of input channels of the time series.
@param cuda Transfers, if True, all computations to the GPU.
@param gpu GPU index to use, if CUDA is enabled.
"""
def __init__(self, compared_length, nb_random_samples, negative_penalty,
batch_size, nb_steps, lr, penalty, early_stopping,
encoder, params, in_channels, cuda=False, gpu=0):
self.architecture = ''
self.cuda = cuda
self.gpu = gpu
self.batch_size = batch_size
self.nb_steps = nb_steps
self.lr = lr
self.penalty = penalty
self.early_stopping = early_stopping
self.encoder = encoder
self.params = params
self.in_channels = in_channels
self.loss = losses.triplet_loss.TripletLoss(
compared_length, nb_random_samples, negative_penalty
)
self.loss_varying = losses.triplet_loss.TripletLossVaryingLength(
compared_length, nb_random_samples, negative_penalty
)
self.classifier = sklearn.svm.SVC()
self.optimizer = torch.optim.Adam(self.encoder.parameters(), lr=lr)
def save_encoder(self, prefix_file):
"""
Saves the encoder and the SVM classifier.
@param prefix_file Path and prefix of the file where the models should
be saved (at '$(prefix_file)_$(architecture)_encoder.pth').
"""
torch.save(
self.encoder.state_dict(),
prefix_file + '_' + self.architecture + '_encoder.pth'
)
def save(self, prefix_file):
"""
Saves the encoder and the SVM classifier.
@param prefix_file Path and prefix of the file where the models should
be saved (at '$(prefix_file)_$(architecture)_classifier.pkl' and
'$(prefix_file)_$(architecture)_encoder.pth').
"""
self.save_encoder(prefix_file)
sklearn.externals.joblib.dump(
self.classifier,
prefix_file + '_' + self.architecture + '_classifier.pkl'
)
def load_encoder(self, prefix_file):
"""
Loads an encoder.
@param prefix_file Path and prefix of the file where the model should
be loaded (at '$(prefix_file)_$(architecture)_encoder.pth').
"""
if self.cuda:
self.encoder.load_state_dict(torch.load(
prefix_file + '_' + self.architecture + '_encoder.pth',
map_location=lambda storage, loc: storage.cuda(self.gpu)
))
else:
self.encoder.load_state_dict(torch.load(
prefix_file + '_' + self.architecture + '_encoder.pth',
map_location=lambda storage, loc: storage
))
def load(self, prefix_file):
"""
Loads an encoder and an SVM classifier.
@param prefix_file Path and prefix of the file where the models should
be loaded (at '$(prefix_file)_$(architecture)_classifier.pkl'
and '$(prefix_file)_$(architecture)_encoder.pth').
"""
self.load_encoder(prefix_file)
self.classifier = sklearn.externals.joblib.load(
prefix_file + '_' + self.architecture + '_classifier.pkl'
)
def fit_classifier(self, features, y):
"""
Trains the classifier using precomputed features. Uses an SVM
classifier with RBF kernel.
@param features Computed features of the training set.
@param y Training labels.
"""
nb_classes = numpy.shape(numpy.unique(y, return_counts=True)[1])[0]
train_size = numpy.shape(features)[0]
self.classifier = sklearn.svm.SVC(
C=1 / self.penalty
if self.penalty is not None and self.penalty > 0
else numpy.inf,
gamma='scale'
)
if train_size // nb_classes < 5 or train_size < 50:
return self.classifier.fit(features, y)
else:
if self.penalty is None:
grid_search = sklearn.model_selection.GridSearchCV(
self.classifier, {
'C': [
0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000,
numpy.inf
],
'kernel': ['rbf'],
'degree': [3],
'gamma': ['scale'],
'coef0': [0],
'shrinking': [True],
'probability': [False],
'tol': [0.001],
'cache_size': [200],
'class_weight': [None],
'verbose': [False],
'max_iter': [10000000],
'decision_function_shape': ['ovr'],
'random_state': [None]
},
cv=5, iid=False, n_jobs=5
)
if train_size <= 10000:
grid_search.fit(features, y)
else:
# If the training set is too large, subsample 10000 train
# examples
split = sklearn.model_selection.train_test_split(
features, y,
train_size=10000, random_state=0, stratify=y
)
grid_search.fit(split[0], split[2])
self.classifier = grid_search.best_estimator_
return self.classifier
def fit_encoder(self, X, y=None, save_memory=False, verbose=False):
"""
Trains the encoder unsupervisedly using the given training data.
@param X Training set.
@param y Training labels, used only for early stopping, if enabled. If
None, disables early stopping in the method.
@param save_memory If True, enables to save GPU memory by propagating
gradients after each loss term of the encoder loss, instead of
doing it after computing the whole loss.
@param verbose Enables, if True, to monitor which epoch is running in
the encoder training.
"""
# Check if the given time series have unequal lengths
varying = bool(numpy.isnan(numpy.sum(X)))
train = torch.from_numpy(X)
if self.cuda:
train = train.cuda(self.gpu)
if y is not None:
nb_classes = numpy.shape(numpy.unique(y, return_counts=True)[1])[0]
train_size = numpy.shape(X)[0]
ratio = train_size // nb_classes
train_torch_dataset = utils.Dataset(X)
train_generator = torch.utils.data.DataLoader(
train_torch_dataset, batch_size=self.batch_size, shuffle=True
)
max_score = 0
i = 0 # Number of performed optimization steps
epochs = 0 # Number of performed epochs
count = 0 # Count of number of epochs without improvement
# Will be true if, by enabling epoch_selection, a model was selected
# using cross-validation
found_best = False
# Encoder training
while i < self.nb_steps:
if verbose:
print('Epoch: ', epochs + 1)
for batch in train_generator:
if self.cuda:
batch = batch.cuda(self.gpu)
self.optimizer.zero_grad()
if not varying:
loss = self.loss(
batch, self.encoder, train, save_memory=save_memory
)
else:
loss = self.loss_varying(
batch, self.encoder, train, save_memory=save_memory
)
loss.backward()
self.optimizer.step()
i += 1
if i >= self.nb_steps:
break
epochs += 1
# Early stopping strategy
if self.early_stopping is not None and y is not None and (
ratio >= 5 and train_size >= 50
):
# Computes the best regularization parameters
features = self.encode(X)
self.classifier = self.fit_classifier(features, y)
# Cross validation score
score = numpy.mean(sklearn.model_selection.cross_val_score(
self.classifier, features, y=y, cv=5, n_jobs=5
))
count += 1
# If the model is better than the previous one, update
if score > max_score:
count = 0
found_best = True
max_score = score
best_encoder = type(self.encoder)(**self.params)
best_encoder.double()
if self.cuda:
best_encoder.cuda(self.gpu)
best_encoder.load_state_dict(self.encoder.state_dict())
if count == self.early_stopping:
break
# If a better model was found, use it
if found_best:
self.encoder = best_encoder
return self.encoder
def fit(self, X, y, save_memory=False, verbose=False):
"""
Trains sequentially the encoder unsupervisedly and then the classifier
using the given labels over the learned features.
@param X Training set.
@param y Training labels.
@param save_memory If True, enables to save GPU memory by propagating
gradients after each loss term of the encoder loss, instead of
doing it after computing the whole loss.
@param verbose Enables, if True, to monitor which epoch is running in
the encoder training.
"""
# Fitting encoder
self.encoder = self.fit_encoder(
X, y=y, save_memory=save_memory, verbose=verbose
)
# SVM classifier training
features = self.encode(X)
self.classifier = self.fit_classifier(features, y)
return self
def encode(self, X, batch_size=50):
"""
Outputs the representations associated to the input by the encoder.
@param X Testing set.
@param batch_size Size of batches used for splitting the test data to
avoid out of memory errors when using CUDA. Ignored if the
testing set contains time series of unequal lengths.
"""
# Check if the given time series have unequal lengths
varying = bool(numpy.isnan(numpy.sum(X)))
test = utils.Dataset(X)
test_generator = torch.utils.data.DataLoader(
test, batch_size=batch_size if not varying else 1
)
features = numpy.zeros((numpy.shape(X)[0], self.out_channels))
self.encoder = self.encoder.eval()
count = 0
with torch.no_grad():
if not varying:
for batch in test_generator:
if self.cuda:
batch = batch.cuda(self.gpu)
features[
count * batch_size: (count + 1) * batch_size
] = self.encoder(batch)
count += 1
else:
for batch in test_generator:
if self.cuda:
batch = batch.cuda(self.gpu)
length = batch.size(2) - torch.sum(
torch.isnan(batch[0, 0])
).data.cpu().numpy()
features[count: count + 1] = self.encoder(
batch[:, :, :length]
)
count += 1
self.encoder = self.encoder.train()
return features
def encode_window(self, X, window, batch_size=50, window_batch_size=10000):
"""
Outputs the representations associated to the input by the encoder,
for each subseries of the input of the given size (sliding window
representations).
@param X Testing set.
@param window Size of the sliding window.
@param batch_size Size of batches used for splitting the test data to
avoid out of memory errors when using CUDA.
@param window_batch_size Size of batches of windows to compute in a
run of encode, to save RAM.
"""
features = numpy.empty((
numpy.shape(X)[0], self.out_channels,
numpy.shape(X)[2] - window + 1
))
masking = numpy.empty((
min(window_batch_size, numpy.shape(X)[2] - window + 1),
numpy.shape(X)[1], window
))
for b in range(numpy.shape(X)[0]):
for i in range(math.ceil(
(numpy.shape(X)[2] - window + 1) / window_batch_size)
):
for j in range(
i * window_batch_size,
min(
(i + 1) * window_batch_size,
numpy.shape(X)[2] - window + 1
)
):
j0 = j - i * window_batch_size
masking[j0, :, :] = X[b, :, j: j + window]
features[
b, :, i * window_batch_size: (i + 1) * window_batch_size
] = numpy.swapaxes(
self.encode(masking[:j0 + 1], batch_size=batch_size), 0, 1
)
return features
def predict(self, X, batch_size=50):
"""
Outputs the class predictions for the given test data.
@param X Testing set.
@param batch_size Size of batches used for splitting the test data to
avoid out of memory errors when using CUDA. Ignored if the
testing set contains time series of unequal lengths.
"""
features = self.encode(X, batch_size=batch_size)
return self.classifier.predict(features)
def score(self, X, y, batch_size=50):
"""
Outputs accuracy of the SVM classifier on the given testing data.
@param X Testing set.
@param y Testing labels.
@param batch_size Size of batches used for splitting the test data to
avoid out of memory errors when using CUDA. Ignored if the
testing set contains time series of unequal lengths.
"""
features = self.encode(X, batch_size=batch_size)
return self.classifier.score(features, y)
class CausalCNNEncoderClassifier(TimeSeriesEncoderClassifier):
"""
Wraps a causal CNN encoder of time series as a PyTorch module and a
SVM classifier on top of its computed representations in a scikit-learn
class.
@param compared_length Maximum length of randomly chosen time series. If
None, this parameter is ignored.
@param nb_random_samples Number of randomly chosen intervals to select the
final negative sample in the loss.
@param negative_penalty Multiplicative coefficient for the negative sample
loss.
@param batch_size Batch size used during the training of the encoder.
@param nb_steps Number of optimization steps to perform for the training of
the encoder.
@param lr learning rate of the Adam optimizer used to train the encoder.
@param penalty Penalty term for the SVM classifier. If None and if the
number of samples is high enough, performs a hyperparameter search
to find a suitable constant.
@param early_stopping Enables, if not None, early stopping heuristic
for the training of the representations, based on the final
score. Representations are still learned unsupervisedly in this
case. If the number of samples per class is no more than 10,
disables this heuristic. If not None, accepts an integer
representing the patience of the early stopping strategy.
@param channels Number of channels manipulated in the causal CNN.
@param depth Depth of the causal CNN.
@param reduced_size Fixed length to which the output time series of the
causal CNN is reduced.
@param out_channels Number of features in the final output.
@param kernel_size Kernel size of the applied non-residual convolutions.
@param in_channels Number of input channels of the time series.
@param cuda Transfers, if True, all computations to the GPU.
@param gpu GPU index to use, if CUDA is enabled.
"""
def __init__(self, compared_length=50, nb_random_samples=10,
negative_penalty=1, batch_size=1, nb_steps=2000, lr=0.001,
penalty=1, early_stopping=None, channels=10, depth=1,
reduced_size=10, out_channels=10, kernel_size=4,
in_channels=1, cuda=False, gpu=0):
super(CausalCNNEncoderClassifier, self).__init__(
compared_length, nb_random_samples, negative_penalty, batch_size,
nb_steps, lr, penalty, early_stopping,
self.__create_encoder(in_channels, channels, depth, reduced_size,
out_channels, kernel_size, cuda, gpu),
self.__encoder_params(in_channels, channels, depth, reduced_size,
out_channels, kernel_size),
in_channels, cuda, gpu
)
self.architecture = 'CausalCNN'
self.channels = channels
self.depth = depth
self.reduced_size = reduced_size
self.out_channels = out_channels
self.kernel_size = kernel_size
def __create_encoder(self, in_channels, channels, depth, reduced_size,
out_channels, kernel_size, cuda, gpu):
encoder = networks.causal_cnn.CausalCNNEncoder(
in_channels, channels, depth, reduced_size, out_channels,
kernel_size
)
encoder.double()
if cuda:
encoder.cuda(gpu)
return encoder
def __encoder_params(self, in_channels, channels, depth, reduced_size,
out_channels, kernel_size):
return {
'in_channels': in_channels,
'channels': channels,
'depth': depth,
'reduced_size': reduced_size,
'out_channels': out_channels,
'kernel_size': kernel_size
}
def encode_sequence(self, X, batch_size=50):
"""
Outputs the representations associated to the input by the encoder,
from the start of the time series to each time step (i.e., the
evolution of the representations of the input time series with
repect to time steps).
Takes advantage of the causal CNN (before the max pooling), wich
ensures that its output at time step i only depends on time step i and
previous time steps.
@param X Testing set.
@param batch_size Size of batches used for splitting the test data to
avoid out of memory errors when using CUDA. Ignored if the
testing set contains time series of unequal lengths.
"""
# Check if the given time series have unequal lengths
varying = bool(numpy.isnan(numpy.sum(X)))
test = utils.Dataset(X)
test_generator = torch.utils.data.DataLoader(
test, batch_size=batch_size if not varying else 1
)
length = numpy.shape(X)[2]
features = numpy.full(
(numpy.shape(X)[0], self.out_channels, length), numpy.nan
)
self.encoder = self.encoder.eval()
causal_cnn = self.encoder.network[0]
linear = self.encoder.network[3]
count = 0
with torch.no_grad():
if not varying:
for batch in test_generator:
if self.cuda:
batch = batch.cuda(self.gpu)
# First applies the causal CNN
output_causal_cnn = causal_cnn(batch)
after_pool = torch.empty(
output_causal_cnn.size(), dtype=torch.double
)
if self.cuda:
after_pool = after_pool.cuda(self.gpu)
after_pool[:, :, 0] = output_causal_cnn[:, :, 0]
# Then for each time step, computes the output of the max
# pooling layer
for i in range(1, length):
after_pool[:, :, i] = torch.max(
torch.cat([
after_pool[:, :, i - 1: i],
output_causal_cnn[:, :, i: i+1]
], dim=2),
dim=2
)[0]
features[
count * batch_size: (count + 1) * batch_size, :, :
] = torch.transpose(linear(
torch.transpose(after_pool, 1, 2)
), 1, 2)
count += 1
else:
for batch in test_generator:
if self.cuda:
batch = batch.cuda(self.gpu)
length = batch.size(2) - torch.sum(
torch.isnan(batch[0, 0])
).data.cpu().numpy()
output_causal_cnn = causal_cnn(batch)
after_pool = torch.empty(
output_causal_cnn.size(), dtype=torch.double
)
if self.cuda:
after_pool = after_pool.cuda(self.gpu)
after_pool[:, :, 0] = output_causal_cnn[:, :, 0]
for i in range(1, length):
after_pool[:, :, i] = torch.max(
torch.cat([
after_pool[:, :, i - 1: i],
output_causal_cnn[:, :, i: i+1]
], dim=2),
dim=2
)[0]
features[
count: count + 1, :, :
] = torch.transpose(linear(
torch.transpose(after_pool, 1, 2)
), 1, 2)
count += 1
self.encoder = self.encoder.train()
return features
def get_params(self, deep=True):
return {
'compared_length': self.loss.compared_length,
'nb_random_samples': self.loss.nb_random_samples,
'negative_penalty': self.loss.negative_penalty,
'batch_size': self.batch_size,
'nb_steps': self.nb_steps,
'lr': self.lr,
'penalty': self.penalty,
'early_stopping': self.early_stopping,
'channels': self.channels,
'depth': self.depth,
'reduced_size': self.reduced_size,
'kernel_size': self.kernel_size,
'in_channels': self.in_channels,
'out_channels': self.out_channels,
'cuda': self.cuda,
'gpu': self.gpu
}
def set_params(self, compared_length, nb_random_samples, negative_penalty,
batch_size, nb_steps, lr, penalty, early_stopping,
channels, depth, reduced_size, out_channels, kernel_size,
in_channels, cuda, gpu):
self.__init__(
compared_length, nb_random_samples, negative_penalty, batch_size,
nb_steps, lr, penalty, early_stopping, channels, depth,
reduced_size, out_channels, kernel_size, in_channels, cuda, gpu
)
return self