-
Notifications
You must be signed in to change notification settings - Fork 2
/
nn_baselines.py
514 lines (452 loc) · 17.1 KB
/
nn_baselines.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
# Feed-forward network[s].
import math
import statistics
from pathlib import Path
from typing import Any
import delu
import numpy as np
import rtdl_num_embeddings
import torch
import torch.nn as nn
import torch.utils.tensorboard
from loguru import logger
from torch import Tensor
from tqdm import tqdm
from typing_extensions import NotRequired, TypedDict
import lib
from lib import KWArgs, PartKey
class SNN(nn.Module):
"""SNN from 'Self-Normalizing Neural Networks'."""
def __init__(
self, *, d_in: int, d_out: int, n_blocks: int, d_block: int, dropout: float
) -> None:
super().__init__()
self.blocks = nn.ModuleList(
[
nn.Sequential(
nn.Linear(d_block if i else d_in, d_block),
nn.SELU(),
nn.AlphaDropout(dropout),
)
for i in range(n_blocks)
]
)
self.output = nn.Linear(d_block, d_out)
for module in self.modules():
if isinstance(module, nn.Linear):
# The initialization follows the official implementation:
# https://github.com/bioinf-jku/SNNs/blob/b578499301fcb801f8d4135dbd7cebb246722bfc/Pytorch/SelfNormalizingNetworks_MLP_MNIST.ipynb
nn.init.kaiming_normal_(
module.weight, mode='fan_in', nonlinearity='linear'
)
nn.init.zeros_(module.bias)
def forward(self, x: Tensor) -> Tensor:
for block in self.blocks:
x = block(x)
if self.output is not None:
x = self.output(x)
return x
class DCNv2(nn.Module):
def __init__(
self,
d_in: int,
d_deep: int,
d_out: int,
n_cross_layers: int,
n_deep_layers: int,
dropout_p: float,
k_low_rank_cross: int | None = None,
nonlin_cross: bool = False,
):
super().__init__()
def get_cross_layer():
"Cross layer variations from the paper, no MoE variation"
if k_low_rank_cross is None:
m = nn.Linear(d_in, d_in, bias=True)
torch.nn.init.zeros_(m.bias)
elif nonlin_cross:
d_low_rank_cross = d_in // k_low_rank_cross
m = nn.Sequential(
nn.Linear(d_in, d_low_rank_cross, bias=False),
nn.ReLU(True),
nn.Linear(d_low_rank_cross, d_low_rank_cross, bias=False),
nn.ReLU(True),
nn.Linear(d_low_rank_cross, d_in),
)
torch.nn.init.zeros_(m[-1].bias)
else:
d_low_rank_cross = d_in // k_low_rank_cross
m = nn.Sequential(
nn.Linear(d_in, d_low_rank_cross, bias=False),
nn.Linear(d_low_rank_cross, d_in),
)
torch.nn.init.zeros_(m[-1].bias)
return m
self.cross_layers = nn.ModuleList(
[get_cross_layer() for _ in range(n_cross_layers)]
)
def get_dnn_layer(d_in=None):
return nn.Sequential(
nn.Linear(d_in if d_in is not None else d_deep, d_deep),
nn.ReLU(True),
nn.Dropout(dropout_p),
)
self.deep_layers = nn.Sequential(
*[
get_dnn_layer(d_in=d_in if i == 0 else None)
for i in range(n_deep_layers)
]
)
self.head = nn.Linear(d_deep, d_out)
def forward(self, x):
x0 = x
for c in self.cross_layers:
x = x0 * c(x)
x = self.deep_layers(x)
x = self.head(x)
return x
lib.deep.register_module(DCNv2.__name__, DCNv2)
lib.deep.register_module(SNN.__name__, SNN)
class Model(nn.Module):
def __init__(
self,
*,
n_num_features: int,
n_bin_features: int,
cat_cardinalities: list[int],
n_classes: None | int,
bins: None | list[Tensor],
num_embeddings: None | dict = None,
backbone: dict,
) -> None:
assert n_num_features or n_bin_features or cat_cardinalities
super().__init__()
self.flat = backbone['type'] != 'FTTransformerBackbone'
if not self.flat and n_bin_features > 0:
self.m_bin = lib.deep.make_module("LinearEmbeddings", n_bin_features, backbone['d_block'])
else:
self.m_bin = None
if num_embeddings is None:
assert bins is None
self.m_num = None
d_num = n_num_features
else:
if not self.flat:
num_embeddings['d_embedding'] = backbone['d_block']
assert n_num_features > 0
if num_embeddings['type'] in (
rtdl_num_embeddings.PiecewiseLinearEmbeddings.__name__,
rtdl_num_embeddings.PiecewiseLinearEncoding.__name__,
):
assert bins is not None
self.m_num = lib.deep.make_module(**num_embeddings, bins=bins)
d_num = (
sum(len(x) - 1 for x in bins)
if num_embeddings['type'].startswith(
rtdl_num_embeddings.PiecewiseLinearEncoding.__name__
)
else n_num_features * num_embeddings['d_embedding']
)
else:
assert bins is None
self.m_num = lib.deep.make_module(
**num_embeddings, n_features=n_num_features
)
d_num = n_num_features * num_embeddings['d_embedding']
if backbone['type'] in ['DCNv2', 'FTTransformerBackbone']:
d_cat_embedding = backbone.pop('d_cat_embedding') if self.flat else backbone['d_block']
self.m_cat = (
lib.deep.CategoricalEmbeddings1d(cat_cardinalities, d_cat_embedding)
if cat_cardinalities
else None
)
d_cat = len(cat_cardinalities) * d_cat_embedding
else:
self.m_cat = (
lib.deep.OneHotEncoding0d(cat_cardinalities)
if cat_cardinalities
else None
)
d_cat = sum(cat_cardinalities)
if self.flat:
backbone['d_in'] = d_num + n_bin_features + d_cat
else:
self.cls_embedding = lib.deep.CLSEmbedding(backbone['d_block'])
self.backbone = lib.deep.make_module(
**backbone,
d_out=lib.deep.get_d_out(n_classes),
)
def forward(
self,
*,
x_num: None | Tensor = None,
x_bin: None | Tensor = None,
x_cat: None | Tensor = None,
) -> Tensor:
x = []
if x_num is not None:
x.append(x_num if self.m_num is None else self.m_num(x_num))
if x_bin is not None:
x.append(x_bin if self.m_bin is None else self.m_bin(x_bin))
if x_cat is None:
assert self.m_cat is None
else:
assert self.m_cat is not None
x.append(
self.m_cat(x_cat).flatten(-2)
if isinstance(self.backbone, DCNv2)
else self.m_cat(x_cat)
)
if self.flat:
x = torch.column_stack([x_.flatten(1, -1) for x_ in x])
else:
x = torch.cat([self.cls_embedding(x[0].shape[:1])] + x, dim=1)
x = self.backbone(x)
return x
class Config(TypedDict):
seed: int
data: KWArgs
bins: NotRequired[KWArgs]
model: KWArgs
optimizer: KWArgs
n_lr_warmup_epochs: NotRequired[int]
batch_size: int
patience: int
n_epochs: int
gradient_clipping_norm: NotRequired[float]
parameter_statistics: NotRequired[bool]
def main(
config: Config, output: str | Path, *, force: bool = False
) -> None | lib.JSONDict:
# >>> start
assert set(config) >= Config.__required_keys__
assert set(config) <= Config.__required_keys__ | Config.__optional_keys__
if not lib.start(output, force=force):
return None
lib.show_config(config) # type: ignore[code]
output = Path(output)
delu.random.seed(config['seed'])
device = lib.get_device()
report = lib.create_report(config) # type: ignore[code]
# >>> dataset
dataset = lib.data.build_dataset(**config['data'])
if dataset.task.is_regression:
dataset.data['y'], regression_label_stats = lib.data.standardize_labels(
dataset.data['y']
)
else:
regression_label_stats = None
dataset = dataset.to_torch(device)
Y_train = dataset.data['y']['train'].to(
torch.long if dataset.task.is_multiclass else torch.float
)
# >>> model
if 'bins' in config:
compute_bins_kwargs = (
{
'y': Y_train.to(
torch.long if dataset.task.is_classification else torch.float
),
'regression': dataset.task.is_regression,
'verbose': True,
}
if 'tree_kwargs' in config['bins']
else {}
)
bin_edges = rtdl_num_embeddings.compute_bins(
dataset['x_num']['train'], **config['bins'], **compute_bins_kwargs
)
logger.info(f'Bin counts: {[len(x) - 1 for x in bin_edges]}')
else:
bin_edges = None
model = Model(
n_num_features=dataset.n_num_features,
n_bin_features=dataset.n_bin_features,
cat_cardinalities=dataset.compute_cat_cardinalities(),
n_classes=dataset.task.try_compute_n_classes(),
**config['model'],
bins=bin_edges,
)
report['n_parameters'] = lib.deep.get_n_parameters(model)
logger.info(f'n_parameters = {report["n_parameters"]}')
report['prediction_type'] = 'labels' if dataset.task.is_regression else 'logits'
model.to(device)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
# >>> training
optimizer = lib.deep.make_optimizer(
**config['optimizer'], params=lib.deep.make_parameter_groups(model)
)
loss_fn = lib.deep.get_loss_fn(dataset.task.type_)
gradient_clipping_norm = config.get('gradient_clipping_norm')
step = 0
batch_size = config['batch_size']
report['epoch_size'] = epoch_size = math.ceil(dataset.size('train') / batch_size)
eval_batch_size = 32768
chunk_size = None
generator = torch.Generator(device).manual_seed(config['seed'])
report['metrics'] = {'val': {'score': -math.inf}}
if 'n_lr_warmup_epochs' in config:
n_warmup_steps = min(10000, config['n_lr_warmup_epochs'] * epoch_size)
n_warmup_steps = max(1, math.trunc(n_warmup_steps / epoch_size)) * epoch_size
logger.info(f'{n_warmup_steps=}')
lr_scheduler = torch.optim.lr_scheduler.LinearLR(
optimizer, start_factor=0.01, total_iters=n_warmup_steps
)
else:
lr_scheduler = None
timer = delu.tools.Timer()
early_stopping = delu.tools.EarlyStopping(config['patience'], mode='max')
parameter_statistics = config.get('parameter_statistics', config['seed'] == 1)
training_log = []
writer = torch.utils.tensorboard.SummaryWriter(output) # type: ignore[code]
def apply_model(part: PartKey, idx: Tensor) -> Tensor:
return model(
**{
key: dataset.data[key][part][idx] # type: ignore[index]
for key in ['x_num', 'x_bin', 'x_cat']
if key in dataset # type: ignore[index]
}
).squeeze(-1)
@torch.inference_mode()
def evaluate(
parts: list[PartKey], eval_batch_size: int
) -> tuple[dict[PartKey, Any], dict[PartKey, np.ndarray], int]:
model.eval()
predictions: dict[PartKey, np.ndarray] = {}
for part in parts:
while eval_batch_size:
try:
predictions[part] = (
torch.cat(
[
apply_model(part, idx)
for idx in torch.arange(
len(dataset.data['y'][part]),
device=device,
).split(eval_batch_size)
]
)
.cpu()
.numpy()
)
except RuntimeError as err:
if not lib.is_oom_exception(err):
raise
eval_batch_size //= 2
logger.warning(f'eval_batch_size = {eval_batch_size}')
else:
break
if not eval_batch_size:
RuntimeError('Not enough memory even for eval_batch_size=1')
if regression_label_stats is not None:
predictions = {
k: v * regression_label_stats.std + regression_label_stats.mean
for k, v in predictions.items()
}
metrics = (
dataset.task.calculate_metrics(predictions, report['prediction_type'])
if lib.are_valid_predictions(predictions)
else {x: {'score': -999999.0} for x in predictions}
)
return metrics, predictions, eval_batch_size
def save_checkpoint() -> None:
lib.dump_checkpoint(
output,
{
'step': step,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'generator': generator.get_state(),
'random_state': delu.random.get_state(),
'early_stopping': early_stopping,
'report': report,
'timer': timer,
'training_log': training_log,
}
| (
{}
if lr_scheduler is None
else {'lr_scheduler': lr_scheduler.state_dict()}
),
)
lib.dump_report(output, report)
lib.backup_output(output)
print()
timer.run()
while config['n_epochs'] == -1 or step // epoch_size < config['n_epochs']:
print(f'[...] {output} | {timer}')
# >>>
model.train()
epoch_losses = []
for batch_idx in tqdm(
torch.randperm(
len(dataset.data['y']['train']), generator=generator, device=device
).split(batch_size),
desc=f'Epoch {step // epoch_size} Step {step}',
):
loss, new_chunk_size = lib.deep.zero_grad_forward_backward(
optimizer,
lambda idx: loss_fn(apply_model('train', idx), Y_train[idx]),
batch_idx,
chunk_size or batch_size,
)
if parameter_statistics and (
step % epoch_size == 0 # The first batch of the epoch.
or step // epoch_size == 0 # The first epoch.
):
for k, v in lib.deep.compute_parameter_stats(model).items():
writer.add_scalars(k, v, step, timer.elapsed())
del k, v
if gradient_clipping_norm is not None:
nn.utils.clip_grad.clip_grad_norm_(
model.parameters(), gradient_clipping_norm
)
optimizer.step()
if lr_scheduler is not None:
lr_scheduler.step()
step += 1
epoch_losses.append(loss.detach())
if new_chunk_size and new_chunk_size < (chunk_size or batch_size):
chunk_size = new_chunk_size
logger.warning(f'chunk_size = {chunk_size}')
epoch_losses = torch.stack(epoch_losses).tolist()
mean_loss = statistics.mean(epoch_losses)
metrics, predictions, eval_batch_size = evaluate(
['val', 'test'], eval_batch_size
)
training_log.append(
{'epoch-losses': epoch_losses, 'metrics': metrics, 'time': timer.elapsed()}
)
lib.print_metrics(mean_loss, metrics)
writer.add_scalars('loss', {'train': mean_loss}, step, timer.elapsed())
for part in metrics:
writer.add_scalars(
'score', {part: metrics[part]['score']}, step, timer.elapsed()
)
if metrics['val']['score'] > report['metrics']['val']['score']:
print('🌸 New best epoch! 🌸')
report['best_step'] = step
report['metrics'] = metrics
save_checkpoint()
lib.dump_predictions(output, predictions)
early_stopping.update(metrics['val']['score'])
if early_stopping.should_stop() or not lib.are_valid_predictions(predictions):
break
print()
report['time'] = str(timer)
# >>> finish
model.load_state_dict(lib.load_checkpoint(output)['model'])
report['metrics'], predictions, _ = evaluate(
['train', 'val', 'test'], eval_batch_size
)
report['chunk_size'] = chunk_size
report['eval_batch_size'] = eval_batch_size
lib.dump_predictions(output, predictions)
lib.dump_summary(output, lib.summarize(report))
save_checkpoint()
lib.finish(output, report)
return report
if __name__ == '__main__':
lib.configure_libraries()
lib.run_MainFunction_cli(main)