-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
623 lines (564 loc) · 20.5 KB
/
trainer.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
#!/usr/bin/env python3.7
"""Model trainer"""
# region
import json
import math
import os
import sys
import time
import typing as t
import numpy as np
import torch
from torch import nn
from torch import optim
from data_utils import get_data_loader
from data_utils import get_data_loader_full
from mol_spec import MoleculeSpec
from deep_scaffold import DeepScaffold
# endregion
def _check_continuous(ckpt_loc: str) -> bool:
"""Check whether to continue the training or start a new session
Args:
ckpt_loc (str):
The location of the checkpoint file
Returns:
bool:
Whether to continue training
"""
is_continuous = all([os.path.isfile(os.path.join(ckpt_loc, _n))
for _n in ['configs.json',
'log.out',
'mdl.ckpt',
'optimizer.ckpt',
'scheduler.ckpt']])
return is_continuous
# pylint: disable=invalid-name
def _get_loader(network_loc: str,
molecule_loc: str,
exclude_ids_loc: str,
split_by: str,
batch_size: int,
batch_size_test: int,
num_iterations: int,
num_workers: int,
full: bool,
training_only: bool,
k: int,
p: float,
ms: MoleculeSpec
) -> t.Tuple[t.Iterable,
t.Optional[t.Iterable]]:
"""Helper function for getting data loaders
Args:
network_loc (str):
Location of the bipartite network
molecule_loc (str):
Location of molecule SMILES strings
exclude_ids_loc (str):
The location storing the ids to be excluded from the training set
split_by (str):
Whether to split by scaffold or molecule
batch_size (int):
The batch size for training
batch_size_test (int):
The batch size for testing
num_iterations (int):
The number of total iterations for model training
num_workers (int):
The number of workers for loading dataset
full (bool):
Whether to use the full dataset for training
training_only (bool):
Only record training loss
k (int):
The number of importance samples
p (float):
The degree of stochasticity of importance sampling 0.0 for fully
stochastic decoding, 1.0 for fully deterministic decoding
ms (MoleculeSpec)
Returns:
t.Tuple[t.Iterable, t.Iterable]:
DataLoaders for training and test data
"""
if full:
training_only = True
loader_train = \
get_data_loader_full(network_loc,
molecule_loc,
batch_size,
num_iterations,
num_workers,
k, p, ms)
loader_test = None
else:
loader_train, loader_test = \
get_data_loader(network_loc,
molecule_loc,
exclude_ids_loc,
split_by,
batch_size,
batch_size_test,
num_iterations,
num_workers,
k, p, ms)
if training_only:
loader_test = None
return loader_train, loader_test
def _init_mdl(num_atom_embedding: int,
causal_hidden_sizes: t.Iterable,
num_bn_features: int,
num_k_features: int,
num_layers: int,
num_output_features: int,
efficient: bool,
activation: str,
gpu_ids: t.Mapping[int, int]) -> nn.DataParallel:
"""Helper function for initializing model
Args:
num_atom_embedding (int):
The size of the initial node embedding
causal_hidden_sizes (tuple[int] or list[int]):
The size of hidden layers in causal weave blocks
num_bn_features (int):
The number of features used in bottleneck layers in each dense
layer
num_k_features (int):
The growth rate of dense net
num_layers (int):
The number of densenet layers
num_output_features (int):
The number of output features for the densenet
efficient (bool):
Whether to use the memory efficient implementation of densenet
Returns:
nn.DataParallel:
The model intialized
"""
# SECTION Build model
# SECTION Create empty model with config
configs = {
'num_atom_embedding': num_atom_embedding,
'causal_hidden_sizes': causal_hidden_sizes,
'num_bn_features': num_bn_features,
'num_k_features': num_k_features,
'num_layers': num_layers,
'num_output_features': num_output_features,
'efficient': efficient,
'activation': activation,
}
mdl = DeepScaffold(**configs)
# !SECTION
# SECTION Weight initializer
def init_weights(m):
if isinstance(m, nn.Linear):
nn.init.xavier_normal(m.weight)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1.0)
mdl.apply(init_weights)
# !SECTION
# SECTION Wrap into nn.DataParallel and move to gpu
mdl = nn.DataParallel(mdl, device_ids=gpu_ids)
mdl.cuda()
# !SECTION
# !SECTION
return mdl
# pylint: disable=protected-access
def _restore(mdl: nn.Module,
optimizer: optim.Optimizer,
scheduler: optim.lr_scheduler._LRScheduler,
ckpt_loc: str) -> t.Tuple[nn.Module,
optim.Optimizer,
optim.lr_scheduler._LRScheduler,
int, float]:
"""Restore model training state
Args:
mdl (nn.Module):
The randomly initialized model
optimizer (optim.Optimizer):
The optimizer
scheduler (optim.lr_scheduler._LRScheduler):
The scheduler for learning rate
ckpt_loc (str):
Location to store model checkpoints
Returns:
t.Tuple[nn.Module,
optim.Optimizer,
optim.lr_scheduler._LRScheduler,
int, float]:
The restored status
"""
# Restore model checkpoint
mdl.load_state_dict(
torch.load(os.path.join(ckpt_loc, 'mdl.ckpt')))
optimizer.load_state_dict(
torch.load(os.path.join(ckpt_loc, 'optimizer.ckpt')))
scheduler.load_state_dict(
torch.load(os.path.join(ckpt_loc, 'scheduler.ckpt')))
# Restore timer and step counter
with open(os.path.join(ckpt_loc, 'log.out')) as f:
records = f.readlines()
if records[-1] != 'Training finished\n':
final_record = records[-1]
else:
final_record = records[-2]
global_counter, t_final = final_record.split('\t')[:2]
global_counter = int(global_counter)
t_final = float(t_final)
t0 = time.time() - t_final * 60
return mdl, optimizer, scheduler, global_counter, t0
def _save(mdl: nn.Module,
optimizer: optim.Optimizer,
scheduler: optim.lr_scheduler._LRScheduler,
global_counter: int,
t0: float,
loss: float,
current_lr: float,
ckpt_loc: str) -> str:
"""Saving checkpoint to file
Args:
mdl (nn.Module):
The randomly initialized model
optimizer (optim.Optimizer):
The optimizer
scheduler (optim.lr_scheduler._LRScheduler):
The scheduler for learning rate
global_counter (int):
The global counter for training
t0 (float):
The time training was started
loss (float):
The loss of the model
current_lr (float):
The current learning rate
ckpt_loc (str):
Location to store model checkpoints
Return:
str:
The message string
"""
# Save status
torch.save(mdl.state_dict(),
os.path.join(ckpt_loc, 'mdl.ckpt'))
torch.save(optimizer.state_dict(),
os.path.join(ckpt_loc, 'optimizer.ckpt'))
torch.save(scheduler.state_dict(),
os.path.join(ckpt_loc, 'scheduler.ckpt'))
message_str = (f'{global_counter}\t'
f'{float(time.time() - t0) / 60}\t'
f'{loss}\t'
f'{current_lr}\n')
return message_str
def _loss(mol_array: np.ndarray,
log_p: np.ndarray,
mdl: t.Union[nn.DataParallel,
DeepScaffold],
device: torch.device,
) -> torch.Tensor:
"""A helper function for getting loss
Args:
mol_array (torch.Tensor):
The molecule array to calculate the likelihood
log_p (torch.Tensor):
The log-likelihood value of each trajectory
mdl (t.Union[nn.DataParallel,DeepScaffold]):
The model
Returns:
torch.Tensor:
The calculated loss
"""
# Move to gpu
# pylint: disable=not-callable
mol_array = torch.tensor(mol_array,
dtype=torch.long,
device=device)
log_p = torch.tensor(log_p,
dtype=torch.float32,
device=device)
# Get shape and device information
batch_size, k = log_p.shape
# Flatten the first (batch_size) and the second (k) dimension
mol_array = mol_array.view(batch_size * k, -1, 5)
# Shuffle
shuffle = torch.randperm(batch_size * k,
dtype=torch.long,
device=device)
mol_array = mol_array[shuffle, ...]
# Get likelihood
# shape: batch_size * k
ll = mdl(mol_array).sum(-1)
# Shuffle back
unshuffle = torch.argsort(shuffle)
ll = ll[unshuffle]
# Unflatten
ll = ll.view(batch_size, k)
# Get total likelihood
ll = ll.sub(log_p)\
.logsumexp(dim=-1)\
.sub(math.log(float(k)))\
.mean()
# Get final loss
loss = -ll
return loss
# pylint: disable=protected-access
def _train_step(mdl: nn.Module,
optimizer: optim.Optimizer,
scheduler: optim.lr_scheduler._LRScheduler,
min_lr: float,
clip_grad: float,
device: torch.device,
iter_train: t.Iterator):
"""Helper function to perform one step of training
Args:
mdl (nn.Module):
The randomly initialized model
optimizer (optim.Optimizer):
The optimizer
scheduler (optim.lr_scheduler._LRScheduler):
The scheduler for learning rate
min_lr (float):
The minimum learning rate
clip_grad (float):
Gradient clipping
device (torch.device):
The device where tensors should be intialized
iter_train (t.Iterator):
The iterator for trainer
"""
# Prepare for training
optimizer.zero_grad() # Clear gradient
if all([params_group['lr'] > min_lr
for params_group in optimizer.param_groups]):
# Update learning rate if it is still larger than min_lr
scheduler.step()
# Get data
mol_array, log_p = next(iter_train)
loss = _loss(mol_array, log_p, mdl, device)
loss.backward()
# Clip gradient
torch.nn.utils.clip_grad_value_(mdl.parameters(), clip_grad)
optimizer.step()
return loss
# pylint: disable=protected-access
def _test_step(mdl: nn.Module,
device: torch.device,
iter_test: t.Iterator) -> float:
"""Helper function to perform one step of training
Args:
mdl (nn.Module):
The randomly initialized model
iter_test (t.Iterator):
The iterator for tester
"""
with torch.no_grad():
mol_array, log_p = next(iter_test)
mdl.eval()
# Get loss
loss = _loss(mol_array, log_p, mdl, device)
mdl.train()
return loss
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
# pylint: disable=invalid-name
def engine(ckpt_loc: str = 'ckpt/ckpt-default',
molecule_loc: str = 'data_utils/molecules.smi',
network_loc: str = 'data_utils/scaffolds_molecules.pkl.gz',
exclude_ids_loc: str = 'ckpt/ckpt-default/exclude_ids.txt',
full: bool = False,
split_by: str = 'molecule',
training_only: bool = False,
num_workers: int = 2,
num_atom_embedding: int = 16,
causal_hidden_sizes: t.Iterable = (32, 64),
num_bn_features: int = 96,
num_k_features: int = 24,
num_layers: int = 20,
num_output_features: int = 256,
efficient: bool = False,
ms: MoleculeSpec = MoleculeSpec.get_default(),
activation: str = 'elu',
lr: float = 1e-3,
decay: float = 0.01,
decay_step: int = 100,
min_lr: float = 5e-5,
summary_step: int = 200,
clip_grad: float = 3.0,
batch_size: int = 128,
batch_size_test: int = 256,
num_iterations: int = 50000,
k: int = 5,
p: float = 0.5,
gpu_ids: t.Mapping[int, int] = (0, 1, 2, 3)):
"""
Engine for training scaffold based VAE
Args:
ckpt_loc (str):
Location to store model checkpoints
molecule_loc (str):
Location of molecule SMILES strings
network_loc (str):
Location of the bipartite network
exclude_ids_loc (str):
The location storing the ids to be excluded from the training set
full (bool):
Whether to use the full dataset for training, default to False
split_by (str):
Whether to split by scaffold or molecule
training_only (str):
Recording only training loss, default to False
num_workers (int):
Number of workers used during data loading, default to 1
num_atom_embedding (int):
The size of the initial node embedding
causal_hidden_sizes (tuple[int] or list[int]):
The size of hidden layers in causal weave blocks
num_bn_features (int):
The number of features used in bottleneck layers in each dense
layer
num_k_features (int):
The growth rate of dense net
num_layers (int):
The number of densenet layers
num_output_features (int):
The number of output features for the densenet
efficient (bool):
Whether to use the memory efficient implementation of densenet
ms (mol_spec.MoleculeSpec)
activation (str):
The activation function used, default to 'elu'
lr (float):
(Initial) learning rate
decay (float):
The rate of learning rate decay
decay_step (int):
The interval of each learning rate decay
min_lr (float):
The minimum learning rate
summary_step (int):
Interval of summary
clip_grad (float):
Gradient clipping
batch_size (int):
The batch size for training
batch_size_test (int):
The batch size for testing
num_iterations (int):
The number of total iterations for model training
k (int):
The number of importance samples
p (float):
The degree of stochasticity of importance sampling 0.0 for fully
stochastic decoding, 1.0 for fully deterministic decoding
gpu_ids (tuple[int] or list[int]):
Which GPUs are used for training
"""
# ANCHOR Check whether to continue training
is_continuous = _check_continuous(ckpt_loc)
# ANCHOR Create iterators for training and test dataset
loader_train, loader_test = _get_loader(network_loc,
molecule_loc,
exclude_ids_loc,
split_by,
batch_size,
batch_size_test,
num_iterations,
num_workers,
full,
training_only,
k, p, ms)
iter_train = iter(loader_train)
iter_test = iter(loader_test) if loader_test is not None else None
# ANCHOR Initialize model with random params
mdl = _init_mdl(num_atom_embedding,
causal_hidden_sizes,
num_bn_features,
num_k_features,
num_layers,
num_output_features,
efficient,
activation,
gpu_ids)
# ANCHOR Initialize optimizer and scheduler
optimizer = optim.Adam(mdl.parameters(), lr=lr)
scheduler = optim.lr_scheduler.StepLR(optimizer, decay_step, 1.0 - decay)
# ANCHOR Load previously stored states
if is_continuous:
# restore states
(mdl,
optimizer,
scheduler,
t0,
global_counter) = _restore(mdl,
optimizer,
scheduler,
ckpt_loc)
else:
t0 = time.time()
global_counter = 0
device = torch.device(f'cuda:{gpu_ids[0]}')
with open(os.path.join(ckpt_loc, 'log.out'),
mode='a' if is_continuous else 'w') as f:
if not is_continuous:
f.write('global_step\ttime(min)\tloss\tlr\n')
try:
while True:
global_counter += 1 # Update global counter
# Perform one-step of training
loss = _train_step(mdl,
optimizer,
scheduler,
min_lr,
clip_grad,
device,
iter_train)
if global_counter % summary_step == 0:
if not training_only:
try:
loss = _test_step(mdl, device, iter_test)
except StopIteration:
iter_test = iter(loader_test)
loss = _test_step(mdl, device, iter_test)
loss = loss.item()
# Get learning rate
current_lr = [params_group['lr']
for params_group
in optimizer.param_groups][0]
# Save status
message_str = _save(mdl,
optimizer,
scheduler,
global_counter,
t0, loss, current_lr,
ckpt_loc)
f.write(message_str)
f.flush()
except StopIteration:
if not training_only:
try:
loss = _test_step(mdl, device, iter_test)
except StopIteration:
iter_test = iter(loader_test)
loss = _test_step(mdl, device, iter_test)
loss = loss.item()
# Get learning rate
current_lr = [params_group['lr']
for params_group in optimizer.param_groups][0]
# Save status
message_str = _save(mdl,
optimizer,
scheduler,
global_counter,
t0, loss, current_lr,
ckpt_loc)
f.write(message_str)
f.flush()
f.write('Training finished')
def main(ckpt_loc):
"""Program entrypoint"""
with open(os.path.join(ckpt_loc, 'config.json')) as f:
config = json.load(f)
config['ckpt_loc'] = ckpt_loc
engine(**config)
if __name__ == '__main__':
main(sys.argv[1])