-
Notifications
You must be signed in to change notification settings - Fork 10
/
training.py
141 lines (117 loc) · 5.09 KB
/
training.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
"""PyTorch Lightning module for standard training."""
import math
import argparse
import torch
import numpy as np
from torch import nn
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.callbacks import LearningRateMonitor
from data_module import CodonDataModule
from checkpointing import PeriodicCheckpoint
from calm.sequence import CodonSequence
from calm.alphabet import Alphabet
from calm.model import ProteinBertModel
class CodonModel(pl.LightningModule):
"""PyTorch Lightning module for standard training."""
def __init__(self, args, alphabet):
super().__init__()
self.args = args
self.alphabet = alphabet
self.model = ProteinBertModel(args, alphabet)
def init_weights(module):
if isinstance(module, (torch.nn.Linear, torch.nn.Embedding)):
torch.nn.init.normal_(module.weight, std=.02)
if isinstance(module, (torch.nn.Linear)):
module.bias.data.fill_(0)
self.model.apply(init_weights)
self.loss_fn = nn.CrossEntropyLoss(reduction='mean', ignore_index=-100)
self.save_hyperparameters()
def forward(self, x):
return self.model(x)
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.parameters(), lr=self.args.learning_rate,
weight_decay=self.args.weight_decay)
if self.args.lr_scheduler == 'none':
return optimizer
elif self.args.lr_scheduler == 'warmup_sqrt':
def schedule(global_step):
if global_step < self.args.warmup_steps:
return (global_step+1) / self.args.warmup_steps
else:
return np.sqrt(self.args.warmup_steps / global_step)
elif self.args.lr_scheduler == 'warmup_cosine':
def schedule(global_step):
if global_step < self.args.warmup_steps:
return (global_step+1) / self.args.warmup_steps
else:
progress = (global_step - self.args.warmup_steps) / self.args.num_steps
return max(0., .5 * (1. + math.cos(math.pi * progress)))
else:
raise ValueError('Unrecognised learning rate scheduler')
scheduler = {
'scheduler': torch.optim.lr_scheduler.LambdaLR(optimizer, schedule),
'name': 'learning_rate',
'interval': 'step',
'frequency': 1
}
return [optimizer], [scheduler]
def training_step(self, train_batch, batch_idx):
data, labels = \
train_batch['input'].to(), \
train_batch['labels'].to(dtype=torch.int64)
output = self.model(data)
likelihoods = output['logits']
loss = self.loss_fn(
likelihoods.view(-1, len(self.alphabet.all_toks)),
labels.view(-1)
)
if batch_idx % self.args.accumulate_gradients == 0:
self.log('train_loss', loss)
return loss
def validation_step(self, val_batch, batch_idx):
data, labels = \
val_batch['input'].to(), \
val_batch['labels'].to(dtype=torch.int64)
output = self.model(data)
likelihoods = output['logits']
loss = self.loss_fn(
likelihoods.view(-1, len(self.alphabet.all_toks)),
labels.view(-1)
)
self.log('val_loss', loss)
return loss
if __name__ == '__main__':
# parsing
parser = argparse.ArgumentParser()
parser.add_argument('--max_positions', type=int, default=1024)
parser.add_argument('--batch_size', type=int, default=46)
parser.add_argument('--accumulate_gradients', type=int, default=40)
parser.add_argument('--mask_proportion', type=float, default=.25)
parser.add_argument('--leave_percent', type=float, default=.1)
parser.add_argument('--mask_percent', type=float, default=.8)
parser.add_argument('--warmup_steps', type=int, default=1000)
parser.add_argument('--weight_decay', type=float, default=0.1)
parser.add_argument('--lr_scheduler', type=str, default='warmup_cosine')
parser.add_argument('--learning_rate', type=float, default=4e-4)
parser.add_argument('--num_steps', type=int, default=121000)
ProteinBertModel.add_args(parser)
args = parser.parse_args()
# data
alphabet = Alphabet.from_architecture('CodonModel')
datamodule = CodonDataModule(args, alphabet,
'training_data.fasta', args.batch_size)
# model
model = CodonModel(args, alphabet)
# training
name = 'production-run'
logger = WandbLogger(name=name, project='12layers', version='restart3')
trainer = pl.Trainer(gpus=4, num_nodes=1, precision=16,
max_steps=args.num_steps, logger=logger, log_every_n_steps=1,
val_check_interval=100*args.accumulate_gradients,
accumulate_grad_batches=args.accumulate_gradients,
limit_val_batches=0.25, accelerator='dp',
callbacks=[PeriodicCheckpoint(1000, name),
LearningRateMonitor(logging_interval='step')])
trainer.fit(model, datamodule=datamodule,
ckpt_path='production-run/latest-56000.ckpt')