-
Notifications
You must be signed in to change notification settings - Fork 1
/
transtab.py
492 lines (394 loc) · 15.4 KB
/
transtab.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
import pdb
import os
import constants
from modeling_transtab import TransTabClassifier, TransTabFeatureExtractor, TransTabFeatureProcessor
from modeling_transtab import TransTabForCL
from modeling_transtab import TransTabInputEncoder, TransTabModel
from dataset import load_data
from evaluator import predict, evaluate
from trainer import Trainer
from trainer_utils import TransTabCollatorForCL
from trainer_utils import random_seed
def build_classifier(
categorical_columns=None,
numerical_columns=None,
binary_columns=None,
feature_extractor=None,
num_class=2,
hidden_dim=128,
num_layer=2,
num_attention_head=8,
hidden_dropout_prob=0,
ffn_dim=256,
activation='relu',
device='cuda:0',
checkpoint=None,
**kwargs) -> TransTabClassifier:
'''Build a :class:`transtab.modeling_transtab.TransTabClassifier`.
Parameters
----------
categorical_columns: list
a list of categorical feature names.
numerical_columns: list
a list of numerical feature names.
binary_columns: list
a list of binary feature names, accept binary indicators like (yes,no); (true,false); (0,1).
feature_extractor: TransTabFeatureExtractor
a feature extractor to tokenize the input tables. if not passed the model will build itself.
num_class: int
number of output classes to be predicted.
hidden_dim: int
the dimension of hidden embeddings.
num_layer: int
the number of transformer layers used in the encoder.
num_attention_head: int
the numebr of heads of multihead self-attention layer in the transformers.
hidden_dropout_prob: float
the dropout ratio in the transformer encoder.
ffn_dim: int
the dimension of feed-forward layer in the transformer layer.
activation: str
the name of used activation functions, support ``"relu"``, ``"gelu"``, ``"selu"``, ``"leakyrelu"``.
device: str
the device, ``"cpu"`` or ``"cuda:0"``.
checkpoint: str
the directory to load the pretrained TransTab model.
Returns
-------
A TransTabClassifier model.
'''
model = TransTabClassifier(
categorical_columns = categorical_columns,
numerical_columns = numerical_columns,
binary_columns = binary_columns,
feature_extractor = feature_extractor,
num_class=num_class,
hidden_dim=hidden_dim,
num_layer=num_layer,
num_attention_head=num_attention_head,
hidden_dropout_prob=hidden_dropout_prob,
ffn_dim=ffn_dim,
activation=activation,
device=device,
**kwargs,
)
if checkpoint is not None:
model.load(checkpoint)
return model
def build_extractor(
categorical_columns=None,
numerical_columns=None,
binary_columns=None,
ignore_duplicate_cols=False,
disable_tokenizer_parallel=False,
checkpoint=None,
**kwargs,) -> TransTabFeatureExtractor:
'''Build a feature extractor for TransTab model.
Parameters
----------
categorical_columns: list
a list of categorical feature names.
numerical_columns: list
a list of numerical feature names.
binary_columns: list
a list of binary feature names, accept binary indicators like (yes,no); (true,false); (0,1).
ignore_duplicate_cols: bool
if there is one column assigned to more than one type, e.g., the feature age is both nominated
as categorical and binary columns, the model will raise errors. set True to avoid this error as
the model will ignore this duplicate feature.
disable_tokenizer_parallel: bool
if the returned feature extractor is leveraged by the collate function for a dataloader,
try to set this False in case the dataloader raises errors because the dataloader builds
multiple workers and the tokenizer builds multiple workers at the same time.
checkpoint: str
the directory of the predefined TransTabFeatureExtractor.
Returns
-------
A TransTabFeatureExtractor module.
'''
feature_extractor = TransTabFeatureExtractor(
categorical_columns=categorical_columns,
numerical_columns=numerical_columns,
binary_columns=binary_columns,
disable_tokenizer_parallel=disable_tokenizer_parallel,
ignore_duplicate_cols=ignore_duplicate_cols,
)
if checkpoint is not None:
extractor_path = os.path.join(checkpoint, constants.EXTRACTOR_STATE_DIR)
if os.path.exists(extractor_path):
feature_extractor.load(extractor_path)
else:
feature_extractor.load(checkpoint)
return feature_extractor
def build_encoder(
categorical_columns=None,
numerical_columns=None,
binary_columns=None,
hidden_dim=128,
num_layer=2,
num_attention_head=8,
hidden_dropout_prob=0,
ffn_dim=256,
activation='relu',
device='cuda:0',
checkpoint=None,
**kwargs,
):
'''
Build a feature encoder that maps inputs tabular samples to embeddings.
Parameters
----------
categorical_columns: list
a list of categorical feature names.
numerical_columns: list
a list of numerical feature names.
binary_columns: list
a list of binary feature names, accept binary indicators like (yes,no); (true,false); (0,1).
hidden_dim: int
the dimension of hidden embeddings.
num_layer: int
the number of transformer layers used in the encoder. If set zero, only use the
embedding layer to get token-level embeddings.
num_attention_head: int
the numebr of heads of multihead self-attention layer in the transformers.
Ignored if `num_layer=0` is zero.
hidden_dropout_prob: float
the dropout ratio in the transformer encoder.
Ignored if `num_layer=0` is zero.
ffn_dim: int
the dimension of feed-forward layer in the transformer layer.
Ignored if `num_layer=0` is zero.
activation: str
the name of used activation functions, support ``"relu"``, ``"gelu"``, ``"selu"``, ``"leakyrelu"``.
Ignored if `num_layer=0` is zero.
device: str
the device, ``"cpu"`` or ``"cuda:0"``.
checkpoint: str
the directory to load the pretrained TransTab model.
'''
if num_layer == 0:
feature_extractor = TransTabFeatureExtractor(
categorical_columns=categorical_columns,
numerical_columns=numerical_columns,
binary_columns=binary_columns,
)
feature_processor = TransTabFeatureProcessor(
vocab_size=feature_extractor.vocab_size,
pad_token_id=feature_extractor.pad_token_id,
hidden_dim=hidden_dim,
hidden_dropout_prob=hidden_dropout_prob,
device=device,
)
enc = TransTabInputEncoder(feature_extractor, feature_processor)
enc.load(checkpoint)
else:
enc = TransTabModel(
categorical_columns=categorical_columns,
numerical_columns=numerical_columns,
binary_columns=binary_columns,
hidden_dim=hidden_dim,
num_layer=num_layer,
num_attention_head=num_attention_head,
hidden_dropout_prob=hidden_dropout_prob,
ffn_dim=ffn_dim,
activation=activation,
device=device,
)
if checkpoint is not None:
enc.load(checkpoint)
return enc
def build_contrastive_learner(
categorical_columns=None,
numerical_columns=None,
binary_columns=None,
projection_dim=128,
num_partition=3,
overlap_ratio=0.5,
supervised=True,
hidden_dim=128,
num_layer=2,
num_attention_head=8,
hidden_dropout_prob=0,
ffn_dim=256,
activation='relu',
device='cuda:0',
checkpoint=None,
ignore_duplicate_cols=True,
**kwargs,
):
'''Build a contrastive learner for pretraining based on TransTab.
If no cat/num/bin specified, the model takes ALL as categorical columns,
which may undermine the performance significantly.
If there is one column assigned to more than one type, e.g., the feature age is both nominated
as categorical and binary columns, the model will raise errors. set ``ignore_duplicate_cols=True`` to avoid this error as
the model will ignore this duplicate feature.
Parameters
----------
categorical_columns: list
a list of categorical feature names.
numerical_columns: list
a list of numerical feature names.
binary_columns: list
a list of binary feature names, accept binary indicators like (yes,no); (true,false); (0,1).
feature_extractor: TransTabFeatureExtractor
a feature extractor to tokenize the input tables. if not passed the model will build itself.
hidden_dim: int
the dimension of hidden embeddings.
num_layer: int
the number of transformer layers used in the encoder.
num_attention_head: int
the numebr of heads of multihead self-attention layer in the transformers.
hidden_dropout_prob: float
the dropout ratio in the transformer encoder.
ffn_dim: int
the dimension of feed-forward layer in the transformer layer.
projection_dim: int
the dimension of projection head on the top of encoder.
overlap_ratio: float
the overlap ratio of columns of different partitions when doing subsetting.
num_partition: int
the number of partitions made for vertical-partition contrastive learning.
supervised: bool
whether or not to take supervised VPCL, otherwise take self-supervised VPCL.
temperature: float
temperature used to compute logits for contrastive learning.
base_temperature: float
base temperature used to normalize the temperature.
activation: str
the name of used activation functions, support ``"relu"``, ``"gelu"``, ``"selu"``, ``"leakyrelu"``.
device: str
the device, ``"cpu"`` or ``"cuda:0"``.
checkpoint: str
the directory of the pretrained transtab model.
ignore_duplicate_cols: bool
if there is one column assigned to more than one type, e.g., the feature age is both nominated
as categorical and binary columns, the model will raise errors. set True to avoid this error as
the model will ignore this duplicate feature.
Returns
-------
A TransTabForCL model.
'''
model = TransTabForCL(
categorical_columns = categorical_columns,
numerical_columns = numerical_columns,
binary_columns = binary_columns,
num_partition= num_partition,
hidden_dim=hidden_dim,
num_layer=num_layer,
num_attention_head=num_attention_head,
hidden_dropout_prob=hidden_dropout_prob,
supervised=supervised,
ffn_dim=ffn_dim,
projection_dim=projection_dim,
overlap_ratio=overlap_ratio,
activation=activation,
device=device,
)
if checkpoint is not None:
model.load(checkpoint)
# build collate function for contrastive learning
collate_fn = TransTabCollatorForCL(
categorical_columns=categorical_columns,
numerical_columns=numerical_columns,
binary_columns=binary_columns,
overlap_ratio=overlap_ratio,
num_partition=num_partition,
ignore_duplicate_cols=ignore_duplicate_cols
)
if checkpoint is not None:
collate_fn.feature_extractor.load(os.path.join(checkpoint, constants.EXTRACTOR_STATE_DIR))
return model, collate_fn
def train(model,
trainset,
valset=None,
num_epoch=10,
batch_size=64,
eval_batch_size=256,
lr=1e-4,
weight_decay=0,
patience=5,
warmup_ratio=None,
warmup_steps=None,
eval_metric='auc',
output_dir='./ckpt',
collate_fn=None,
num_workers=0,
balance_sample=False,
load_best_at_last=True,
ignore_duplicate_cols=False,
eval_less_is_better=False,
**kwargs,
):
'''
The shared train function for all TransTabModel based models.
Parameters
----------
model: TransTabModel and its subclass
A subclass of the base model. Should be able to output logits and loss in forward, e.g.,
``logit, loss = model(x, y)``.
trainset: list or tuple
a list of trainsets, or a single trainset consisting of (x, y). x: pd.DataFrame or dict, y: pd.Series.
valset: list or tuple
a list of valsets, or a single valset of consisting of (x, y).
num_epoch: int
number of training epochs.
batch_size: int
training batch size.
eval_batch_size: int
evaluation batch size.
lr: float
training learning rate.
weight_decay: float
training weight decay.
patience: int
early stopping patience, only valid when ``valset`` is given.
warmup_ratio: float
the portion of training steps for learning rate warmup, if `warmup_steps` is set, it will be ignored.
warmup_steps: int
the number of training steps for learning rate warmup.
eval_metric: str
the evaluation metric during training for early stopping, can be ``"acc"``, ``"auc"``, ``"mse"``, ``"val_loss"``.
output_dir: str
the output training model weights and feature extractor configurations.
collate_fn: function
specify training collate function if it is not standard supervised learning, e.g., contrastive learning.
num_workers: int
the number of workers for the dataloader.
balance_sample: bool
balance_sample: whether or not do bootstrapping to maintain in batch samples are in balanced classes, only support binary classification.
load_best_at_last: bool
whether or not load the best checkpoint after the training completes.
ignore_duplicate_cols: bool
whether or not ignore the contradictory of cat/num/bin cols
eval_less_is_better: bool
if the set eval_metric is the less the better. For val_loss, it should be set True.
Returns
-------
None
'''
if isinstance(trainset, tuple): trainset = [trainset]
train_args = {
'num_epoch': num_epoch,
'batch_size': batch_size,
'eval_batch_size': eval_batch_size,
'lr': lr,
'weight_decay':weight_decay,
'patience':patience,
'warmup_ratio':warmup_ratio,
'warmup_steps':warmup_steps,
'eval_metric':eval_metric,
'output_dir':output_dir,
'collate_fn':collate_fn,
'num_workers':num_workers,
'balance_sample':balance_sample,
'load_best_at_last':load_best_at_last,
'ignore_duplicate_cols':ignore_duplicate_cols,
'eval_less_is_better':eval_less_is_better,
}
trainer = Trainer(
model,
trainset,
valset,
**train_args,
)
trainer.train()