-
Notifications
You must be signed in to change notification settings - Fork 62
/
train.py
32 lines (28 loc) · 1.05 KB
/
train.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
import os
import pytorch_lightning as pl
from utils import *
from models import LightModel
from args import parser
if __name__ == '__main__':
args = parser.parse_args()
model = LightModel(args)
data = EncoderDecoderData(args, model.tokenizer)
dataloaders = data.get_dataloader()
for fold in range(args.kfold):
pl.seed_everything(args.seed + fold)
train_data, dev_data = dataloaders['train'][fold], dataloaders['dev'][fold]
if fold > 0:
model = LightModel(args)
checkpoint = pl.callbacks.ModelCheckpoint(
dirpath=args.output_dir,
filename='{fold:02d}-{epoch:02d}-{bleu:.4f}-{rouge:.4f}-{rouge-1:.4f}-{rouge-2:.4f}-{rouge-l:.4f}',
save_weights_only=True,
save_on_train_epoch_end=True,
monitor='rouge',
mode='max',
)
trainer = pl.Trainer.from_argparse_args(args, callbacks=[checkpoint], logger=False)
trainer.fit(model, train_data, dev_data)
del model
del trainer
torch.cuda.empty_cache()