-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
631 lines (535 loc) · 20.9 KB
/
utils.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
from typing import Dict, List, Tuple
import numpy as np
import openml
import pandas as pd
from scipy.stats import rankdata
from sklearn.compose import ColumnTransformer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import (
OrdinalEncoder,
LabelEncoder,
StandardScaler,
OneHotEncoder,
TargetEncoder,
)
import torch
def prepare_data_for_cutmix(
x: torch.Tensor,
y: torch.Tensor,
augmentation_prob: float = 0.5,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""Apply the cutmix augmentation to the data.
Args:
x: The examples.
y: The labels.
augmentation_prob: The probability with which to apply the operation.
Returns:
x: The augmented examples.
y: The labels.
y_shuffled: The shuffled labels.
lam: The lambda value for the augmentation operation.
"""
# Shuffle the data
indices = torch.randperm(x.size(0))
x_shuffled = x[indices]
y_shuffled = y[indices]
# Generate the lambda value
lam = torch.distributions.beta.Beta(1, 1).sample()
if np.random.rand() > augmentation_prob:
lam = 1
else:
# Generate the mixup mask per example and feature
for i in range(x.size(0)):
cut_column_indices = torch.as_tensor(
np.random.choice(
range(x.size(1)),
max(1, np.int32(x.size(1) * (1 - lam))),
replace=False,
),
dtype=torch.int64,
)
x[i, cut_column_indices] = x_shuffled[i, cut_column_indices]
return x, y, y_shuffled, lam
def prepare_data_for_mixup(
x: torch.Tensor,
y: torch.Tensor,
numerical_features: List,
augmentation_prob: float = 0.5,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""Apply the mixup augmentation to the data.
Args:
x: The examples.
y: The labels.
numerical_features: The numerical features.
augmentation_prob: The probability with which to apply the operation.
Returns:
x: The augmented examples.
y: The labels.
y_shuffled: The shuffled labels.
lam: The lambda value for the augmentation operation.
"""
# Shuffle the data
indices = torch.randperm(x.size(0))
x_shuffled = x[indices]
y_shuffled = y[indices]
# Generate the lambda value
lam = torch.distributions.beta.Beta(1, 1).sample()
if np.random.rand() > augmentation_prob:
lam = 1
else:
# Generate the mixup mask per example and numerical feature
for i in range(x.size(0)):
cut_column_indices = torch.as_tensor(
np.random.choice(
numerical_features,
max(1, np.int32(len(numerical_features) * (1 - lam))),
replace=False,
),
dtype=torch.int64,
)
x[i, cut_column_indices] = lam * x[i, cut_column_indices] + (1. - lam) * x_shuffled[i, cut_column_indices]
return x, y, y_shuffled, lam
def prepare_data_for_cutout(
x: torch.Tensor,
y: torch.Tensor,
numerical_features: List,
augmentation_prob: float = 0.5,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""Apply the cutout augmentation to the data.
Args:
x: The examples.
y: The labels.
numerical_features: The numerical features.
augmentation_prob: The probability with which to apply the operation.
Returns:
x: The augmented examples.
y: The labels.
y_shuffled: The shuffled labels.
lam: The lambda value for the augmentation operation.
"""
# Shuffle the data
indices = torch.randperm(x.size(0))
y_shuffled = y[indices]
# Generate the lambda value
lam = torch.distributions.beta.Beta(1, 1).sample()
if np.random.rand() > augmentation_prob:
lam = 1
else:
# Generate the mixup mask per example and feature
for i in range(x.size(0)):
cut_column_indices = np.random.choice(
range(x.size(1)),
max(1, np.int32(x.size(1) * lam)),
replace=False,
)
cut_cat_indices = [i for i in cut_column_indices if i not in numerical_features]
cut_numerical_indices = [i for i in cut_column_indices if i in numerical_features]
if len(cut_cat_indices) > 0:
cut_cat_indices = torch.as_tensor(
cut_cat_indices,
dtype=torch.int64,
)
x[i, cut_cat_indices] = 0
if len(cut_numerical_indices) > 0:
cut_numerical_indices = torch.as_tensor(
cut_numerical_indices,
dtype=torch.int64,
)
x[i, cut_numerical_indices] = 0
return x, y, y_shuffled, lam
def fgsm_attack(
x: torch.Tensor,
y: torch.Tensor,
model: torch.nn.Module,
criterion: torch.nn.Module,
augmentation_prob: float,
epsilon: float,
) -> torch.Tensor:
"""Generate adversarial examples using the FGSM attack.
Args:
x: The examples.
y: The labels.
model: The trained model.
criterion: The criterion with which the model was trained.
augmentation_prob: The probability with which to apply the operation.
epsilon: The epsilon value for the FGSM attack.
Returns:
x: The augmented examples.
"""
if np.random.rand() > augmentation_prob:
return x
else:
# copy tensor to avoid changing the original one
x = x.clone().detach().requires_grad_(True)
# perform the attack
outputs = model(x)
if outputs.shape[1] == 1:
outputs = outputs.squeeze(1)
cost = criterion(outputs, y)
grad = torch.autograd.grad(cost, x, retain_graph=False, create_graph=False)[0]
adv_data = x + epsilon * grad.sign()
return adv_data
def random_noise(
x: torch.Tensor,
y: torch.Tensor,
augmentation_prob: float = 0.5,
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""Apply random noise to the data.
Args:
x: The examples.
y: The labels.
augmentation_prob: The probability with which to apply the operation.
Returns:
x: The augmented examples.
y: The labels.
y: The labels.
lam: The lambda value for the augmentation operation.
"""
# Generate the lambda value
lam = torch.distributions.beta.Beta(1, 1).sample()
if np.random.rand() > augmentation_prob:
pass
else:
# Generate the mixup mask per example and feature
for i in range(x.size(0)):
cut_column_indices = torch.as_tensor(
np.random.choice(
range(x.size(1)),
max(1, np.int32(x.size(1) * (1 - lam))),
replace=False,
),
dtype=torch.int64,
)
x[i, cut_column_indices] = torch.add(
x[i, cut_column_indices],
(0.1 ** 0.5) * torch.randn(x[i, cut_column_indices].shape).to(x.device),
)
return x, y, y, 1
def augment_data(
x: torch.Tensor,
y: torch.Tensor,
numerical_features: List,
model: torch.nn.Module,
criterion: torch.nn.Module,
augmentation_prob: float = 0.5,
) -> Tuple:
"""Perform data augmentation.
Args:
x: The examples.
y: The labels.
numerical_features: The numerical features.
model: The trained model.
criterion: The criterion with which the model was trained.
augmentation_prob: The probability with which to apply the operation.
Returns:
x: The augmented examples.
y: The labels.
y_shuffled: The shuffled labels.
lam: The lambda value for the augmentation operation.
"""
augmentation_types = {
1: "mixup",
2: "cutout",
3: "cutmix",
4: "fgsm",
5: "random_noise",
}
if len(numerical_features) == 0:
"""remove mixup from the list of augmentation types
since it makes more sense for numerical features"""
del augmentation_types[1]
augmentation_type = augmentation_types[np.random.randint(1, len(augmentation_types) + 1)]
if augmentation_type == "cutmix":
return prepare_data_for_cutmix(x, y, augmentation_prob)
elif augmentation_type == "mixup":
return prepare_data_for_mixup(x, y, numerical_features, augmentation_prob)
elif augmentation_type == "cutout":
return prepare_data_for_cutout(x, y, numerical_features, augmentation_prob)
elif augmentation_type == "fgsm":
return x, fgsm_attack(x, y, model, criterion, augmentation_prob, 0.007), y, y, 0.5
elif augmentation_type == "random_noise":
return random_noise(x, y, augmentation_prob)
else:
raise ValueError("The augmentation type must be one of 'mixup', "
"'cutout', 'cutmix', 'fgsm', or 'random_noise'")
def preprocess_dataset(
X: pd.DataFrame,
y: pd.DataFrame,
encode_categorical: bool,
categorical_indicator: List,
attribute_names: List,
test_split_size: float = 0.2,
seed: int = 11,
encoding_type: str = "ordinal",
hpo_tuning: bool = False,
) -> Dict:
"""Preprocess the dataset.
Args:
X: The examples.
y: The labels.
encode_categorical: Whether to encode the categorical features.
categorical_indicator: An indicator for differentiating between categorical
and numerical features.
attribute_names: The names of the features.
test_split_size: The size of the test split.
seed: The random seed.
encoding_type: The encoding type for the categorical features. Whether it should be
'ordinal' or 'one-hot'.
hpo_tuning: Whether to create a validation set for hyperparameter optimization.
Returns:
info_dict: A dictionary with the preprocessed data and additional information
"""
dropped_column_names = []
dropped_column_indices = []
for column_index, column_name in enumerate(X.keys()):
# if more than 90% of the values are missing, mark the column
if X[column_name].isnull().sum() > len(X[column_name]) * 0.9:
dropped_column_names.append(column_name)
dropped_column_indices.append(column_index)
# if the column has only one unique value, mark the column
if X[column_name].nunique() == 1:
dropped_column_names.append(column_name)
dropped_column_indices.append(column_index)
for column_index, column_name in enumerate(X.keys()):
if X[column_name].dtype == 'object' or X[column_name].dtype == 'category' or X[column_name].dtype == 'string':
# if more than 90% of the values are unique, mark the column
if X[column_name].nunique() / len(X[column_name]) > 0.9:
dropped_column_names.append(column_name)
dropped_column_indices.append(column_index)
# drop the marked columns
X = X.drop(dropped_column_names, axis=1)
# account for dropped columns and match the different indicators
attribute_names = [attribute_name for attribute_name in attribute_names if attribute_name not in dropped_column_names]
categorical_indicator = [categorical_indicator[i] for i in range(len(categorical_indicator)) if i not in dropped_column_indices]
column_category_values = []
# take pandas categories into account
for cat_indicator, column_name in zip(categorical_indicator, X.keys()):
if cat_indicator:
column_categories = list(X[column_name].cat.categories)
column_category_values.append(column_categories)
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=test_split_size,
random_state=seed,
stratify=y,
)
if hpo_tuning:
X_train, X_valid, y_train, y_valid = train_test_split(
X_train,
y_train,
test_size=test_split_size / (1 - test_split_size),
random_state=seed,
stratify=y_train,
)
# pandas series number of unique values
nr_classes = y_train.nunique()
# scikit learn label encoder
label_encoder = LabelEncoder()
label_encoder.fit(y_train)
y_train = label_encoder.transform(y_train)
y_test = label_encoder.transform(y_test)
if hpo_tuning:
y_valid = label_encoder.transform(y_valid)
numerical_features = [i for i in range(len(categorical_indicator)) if not categorical_indicator[i]]
categorical_features = [i for i in range(len(categorical_indicator)) if categorical_indicator[i]]
# save the column types
column_types = {}
for column_name in X_train.keys():
if X_train[column_name].dtype == 'object' or X_train[column_name].dtype == 'category' or X_train[column_name].dtype == 'string':
column_types[column_name] = 'category'
elif pd.api.types.is_numeric_dtype(X_train[column_name]):
column_types[column_name] = 'float64'
else:
raise ValueError("The column type must be one of 'object', 'category', 'string', 'int' or 'float'")
dataset_preprocessors = []
if len(numerical_features) > 0:
numerical_preprocessor = ('numerical', StandardScaler(), numerical_features)
dataset_preprocessors.append(numerical_preprocessor)
if len(categorical_features) > 0 and encode_categorical:
if nr_classes > 2:
if encoding_type == "ordinal":
categorical_preprocessor = (
'categorical_encoder',
OrdinalEncoder(
handle_unknown="use_encoded_value",
unknown_value=-1,
categories=column_category_values,
),
categorical_features,
)
else:
categorical_preprocessor = (
'categorical_encoder',
OneHotEncoder(
handle_unknown='ignore',
sparse=False,
categories=column_category_values,
drop='if_binary',
),
categorical_features,
)
else:
categorical_preprocessor = (
'categorical_encoder',
TargetEncoder(random_state=seed),
categorical_features,
)
dataset_preprocessors.append(categorical_preprocessor)
column_transformer = ColumnTransformer(
dataset_preprocessors,
remainder='passthrough',
)
X_train = column_transformer.fit_transform(X_train, y_train)
X_test = column_transformer.transform(X_test)
if hpo_tuning:
X_valid = column_transformer.transform(X_valid)
X_train = pd.DataFrame(X_train)
X_test = pd.DataFrame(X_test)
if hpo_tuning:
X_valid = pd.DataFrame(X_valid)
if len(numerical_features) > 0:
new_categorical_indicator = [False] * len(numerical_features)
new_attribute_names = [attribute_names[i] for i in numerical_features]
else:
new_categorical_indicator = []
new_attribute_names = []
if len(categorical_features) > 0:
if nr_classes == 2:
new_categorical_indicator.extend([True] * len(categorical_features))
new_attribute_names.extend([attribute_names[i] for i in categorical_features])
else:
for i in range(len(column_category_values)):
nr_unique_categories = len(column_category_values[i])
if nr_unique_categories > 2:
new_categorical_indicator.extend([True] * len(column_category_values[i]))
new_attribute_names.extend([attribute_names[categorical_features[i]] + '_' + str(category) for category in column_category_values[i]])
else:
new_categorical_indicator.extend([True])
new_attribute_names.extend([attribute_names[categorical_features[i]]])
if encode_categorical:
X_train = X_train.fillna(0)
X_test = X_test.fillna(0)
else:
for cat_indicator, column_name in zip(categorical_indicator, X_train.keys()):
if not cat_indicator:
X_train[column_name] = X_train[column_name].fillna(0)
X_test[column_name] = X_test[column_name].fillna(0)
if hpo_tuning:
X_valid[column_name] = X_valid[column_name].fillna(0)
else:
X_train[column_name] = X_train[column_name].cat.add_categories('missing')
X_train[column_name].cat.reorder_categories(np.roll(X_train[column_name].cat.categories, 1))
X_train[column_name] = X_train[column_name].fillna('missing')
X_test[column_name] = X_test[column_name].cat.add_categories('missing')
X_test[column_name].cat.reorder_categories(np.roll(X_test[column_name].cat.categories, 1))
X_test[column_name] = X_test[column_name].fillna('missing')
if hpo_tuning:
X_valid[column_name] = X_valid[column_name].cat.add_categories('missing')
X_valid[column_name].cat.reorder_categories(np.roll(X_valid[column_name].cat.categories, 1))
X_valid[column_name] = X_valid[column_name].fillna('missing')
info_dict = {
'X_train': X_train,
'X_test': X_test,
'y_train': y_train,
'y_test': y_test,
'categorical_indicator': new_categorical_indicator,
'attribute_names': new_attribute_names,
}
if hpo_tuning:
info_dict['X_valid'] = X_valid
info_dict['y_valid'] = y_valid
return info_dict
def get_dataset(
dataset_id: int,
test_split_size: float = 0.2,
seed: int = 11,
encode_categorical: bool = True,
encoding_type: str = 'ordinal',
hpo_tuning: bool = False,
) -> Dict:
"""Get/Preprocess the dataset.
Args:
dataset_id: The dataset identifier.
test_split_size: The size of the test split.
seed: The random seed.
encode_categorical: Whether to encode the categorical features.
encoding_type: The encoding type for the categorical features. Whether it should be
'ordinal' or 'one-hot'.
hpo_tuning: Whether to create a validation set for hyperparameter optimization.
Returns:
info_dict: A dictionary with the preprocessed data and additional information
"""
# Get the data
dataset = openml.datasets.get_dataset(dataset_id, download_data=False)
dataset_name = dataset.name
X, y, categorical_indicator, attribute_names = dataset.get_data(
dataset_format='dataframe',
target=dataset.default_target_attribute,
)
info_dict = preprocess_dataset(
X,
y,
encode_categorical,
categorical_indicator,
attribute_names,
test_split_size=test_split_size,
seed=seed,
encoding_type=encoding_type,
hpo_tuning=hpo_tuning,
)
info_dict['dataset_name'] = dataset_name
return info_dict
def make_residual_block(
self,
in_features: int,
output_features: int,
dropout_rate: float = 0.25,
) -> BasicBlock:
"""Creates a residual block.
Args:
in_features: Number of input features to the first
layer of the residual block.
output_features: Number of output features
for the last layer of the residual block.
dropout_rate: Dropout rate for the residual block.
Returns:
A residual block.
"""
return self.BasicBlock(in_features, output_features, dropout_rate)
class BasicBlock(nn.Module):
def __init__(
self,
in_features: int,
output_features: int,
dropout_rate: float,
):
"""A basic residual block.
Args:
in_features: Number of input features to the first
layer of the residual block.
output_features: Number of output features
dropout_rate: Dropout rate for the residual block.
"""
super(HyperNet.BasicBlock, self).__init__()
self.dropout_rate = dropout_rate
self.hidden_state_dropout = nn.Dropout(self.dropout_rate)
self.residual_dropout = nn.Dropout(self.dropout_rate)
self.linear1 = nn.Linear(in_features, output_features)
self.bn1 = nn.BatchNorm1d(output_features)
self.linear2 = nn.Linear(output_features, output_features)
self.bn2 = nn.BatchNorm1d(output_features)
self.gelu = nn.GELU()
def forward(self, x) -> torch.Tensor:
residual = x
residual = self.residual_dropout(residual)
out = self.linear1(x)
out = self.bn1(out)
out = self.gelu(out)
out = self.hidden_state_dropout(out)
out = self.linear2(out)
out = self.bn2(out)
out += residual
out = self.gelu(out)
return out