-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_ft.py
149 lines (121 loc) · 4.61 KB
/
train_ft.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
# -*- coding: utf-8 -*-
import os
import time
import argparse
import numpy as np
import torch
from torch import cuda
from transformers import logging
from transformers import BartTokenizerFast
from utils.dataset import BartIterator
from utils.optim import ScheduledOptim
from model import MultiFigurativeGeneration
from tokenization_mflag import MFlagTokenizerFast
logging.set_verbosity_error()
device = 'cuda' if cuda.is_available() else 'cpu'
os.environ["TOKENIZERS_PARALLELISM"] = "false"
def evaluate(model, valid_loader, tokenizer, step):
"""
Evaluation function for model
Args:
model: the BART model.
valid_loader: pytorch valid DataLoader.
tokenizer: BART tokenizer
step: the current training step.
Returns:
the average cross-entropy loss
"""
loss_list = []
with torch.no_grad():
model.eval()
for batch in valid_loader:
src, tgt = map(lambda x: x.to(device), batch)
mask = src.ne(tokenizer.pad_token_id).long()
loss = model(
input_ids=src,
attention_mask=mask,
fig_ids=tgt[:, :1],
labels=tgt)[0]
loss_list.append(loss.item())
model.train()
avg_loss = np.mean(loss_list)
print('[Info] valid {:05d} | loss {:.4f}'.format(step, avg_loss))
return avg_loss
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-seed', default=42, type=int, help='random seed')
parser.add_argument(
'-figs', nargs='+', help='figure tags', required=True)
parser.add_argument(
'-batch_size', default=32, type=int, help='batch size')
parser.add_argument(
'-patience', default=5, type=int, help='early stopping')
parser.add_argument(
'-dataset', default='ParapFG', type=str, help='dataset name')
parser.add_argument(
'-lr', default=1e-5, type=float, help='ini. learning rate')
parser.add_argument(
'-log_step', default=100, type=int, help='log every x step')
parser.add_argument(
'-acc_steps', default=8, type=int, help='accumulation_steps')
parser.add_argument(
'-epoch', default=30, type=int, help='force stop at x epoch')
parser.add_argument(
'-eval_step', default=1000, type=int, help='eval every x step')
opt = parser.parse_args()
print('[Info]', opt)
torch.manual_seed(opt.seed)
tokenizer = MFlagTokenizerFast.from_pretrained('checkpoints/mFLAG')
model = MultiFigurativeGeneration.from_pretrained('checkpoints/mFLAG')
model = model.to(device).train()
# load data for training
train_loader, valid_loader = BartIterator('ft', tokenizer, opt).loader
optimizer = ScheduledOptim(
torch.optim.Adam(filter(lambda x: x.requires_grad, model.parameters()),
betas=(0.9, 0.98), eps=1e-09),
lr=opt.lr, decay_step=1000)
tab = 0
step = 0
avg_loss = 1e9
loss_list = []
start = time.time()
for epoch in range(opt.epoch):
for batch in train_loader:
step += 1
src, tgt = map(lambda x: x.to(device), batch)
mask = src.ne(tokenizer.pad_token_id).long()
loss = model(
input_ids=src,
attention_mask=mask,
fig_ids=tgt[:, :1],
labels=tgt)[0]
loss_list.append(loss.item())
loss = loss / opt.acc_steps
loss.backward()
if step % opt.acc_steps == 0:
optimizer.step()
optimizer.zero_grad()
if step % opt.log_step == 0:
lr = optimizer._optimizer.param_groups[0]['lr']
print('[Info] steps {:05d} | loss {:.4f} | lr {:.6f} '
'| second {:.2f}'.format(step, np.mean(loss_list),
lr, time.time() - start))
loss_list = []
start = time.time()
if ((len(train_loader) > opt.eval_step
and step % opt.eval_step == 0)
or (len(train_loader) < opt.eval_step
and step % len(train_loader) == 0)):
eval_loss = evaluate(model, valid_loader, tokenizer, step)
if avg_loss >= eval_loss:
model.save_pretrained('checkpoints/mFLAG')
print('[Info] The checkpoint file has been updated.')
avg_loss = eval_loss
tab = 0
else:
tab += 1
if tab == opt.patience:
exit()
if __name__ == "__main__":
main()