-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
102 lines (76 loc) · 3.45 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
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
import argparse
import logging
import os
from pytorch_lightning import loggers as pl_loggers
from model.rhyme_generator import *
from utils.loader import RhymeDataModule
parser = argparse.ArgumentParser(description='Korean Rhyme')
parser.add_argument('--checkpoint_path',
type=str,
help='checkpoint path')
parser.add_argument('--rhyme',
action='store_true',
default=False,
help='response generation on given user input')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class ArgsBase():
@staticmethod
def add_model_specific_args(parent_parser):
parser = argparse.ArgumentParser(
parents=[parent_parser], add_help=False)
parser.add_argument('--train_file',
type=str,
default='hiphop_data/train.csv',
help='train file')
parser.add_argument('--test_file',
type=str,
default='hiphop_data/test.csv',
help='test file')
parser.add_argument('--tokenizer_path',
type=str,
default='tokenizer',
help='tokenizer')
parser.add_argument('--batch_size',
type=int,
default=14,
help='')
parser.add_argument('--max_seq_len',
type=int,
default=36,
help='max seq len')
return parser
if __name__ == '__main__':
parser = Base.add_model_specific_args(parser)
parser = ArgsBase.add_model_specific_args(parser)
parser = RhymeDataModule.add_model_specific_args(parser)
parser = pl.Trainer.add_argparse_args(parser)
args = parser.parse_args()
logging.info(args)
model = KoBARTConditionalGeneration(args)
dm = RhymeDataModule(args.train_file,
args.test_file,
os.path.join(args.tokenizer_path, 'model.json'),
max_seq_len=args.max_seq_len,
num_workers=args.num_workers)
checkpoint_callback = pl.callbacks.ModelCheckpoint(monitor='val_loss',
dirpath=args.default_root_dir,
filename='model_chp/{epoch:02d}-{val_loss:.3f}',
verbose=True,
save_last=True,
mode='min',
save_top_k=-1,
prefix='rhyme-kobart-full')
tb_logger = pl_loggers.TensorBoardLogger(os.path.join(args.default_root_dir, 'tb_logs'))
lr_logger = pl.callbacks.LearningRateMonitor()
trainer = pl.Trainer.from_argparse_args(args, logger=tb_logger,
callbacks=[checkpoint_callback, lr_logger])
trainer.fit(model, dm)
model.model.save_pretrained('pretrained_dir/rhyme-kobart-model')
if args.rhyme:
model.model.eval()
while 1:
q = input('context > ').strip()
if q == 'quit':
break
print("Rhyme > {}".format(model.rhyme(q)))