-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
241 lines (211 loc) · 9.36 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
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
# coding=utf8
import torch
import os
import time
import math
import json
import jieba
import logging
import argparse
from tqdm import tqdm
from dataset import read_file, \
get_dataloader, get_test_dataloader
from evaluate import rouge_l
from tokenizer import Tokenizer, load_vocab
from seq2seq_model import Seq2SeqModel
from transformers import BertConfig
from torch.optim import Adam
from utils import set_logger, set_seed
from fgm import FGM
from transformers import AdamW, get_linear_schedule_with_warmup
class Trainer(object):
def __init__(self, args):
# 加载数据
self.args = args
self.check_path_exist(args)
pretrain_model_path = args.pretrain_model_path
recent_model_path = args.recent_model_path # 用于把已经训练好的模型继续训练
self.model_save_path = args.model_save_path
self.device = torch.device(args.device)
token_dict = load_vocab(
os.path.join(pretrain_model_path, 'vocab.txt'),
# simplified=False,
startswith=['[PAD]', '[UNK]', '[CLS]', '[SEP]']
)
tokenizer = Tokenizer(
token_dict=token_dict,
do_lower_case=True,
pre_tokenize=lambda s: jieba.cut(s, HMM=False)
)
self.tokenizer = tokenizer
self.train_dataloader, self.eval_dataloader = self.get_dataloader()
eval_file = os.path.join(self.args.data_dir, 'dev.json')
dev_examples = read_file(eval_file)
self.golds = [e.question for e in dev_examples]
# 从预训练模型加载
if args.recent_model_path:
config = BertConfig.from_pretrained(recent_model_path)
seq2seq_model = Seq2SeqModel.from_pretrained(recent_model_path, config=config,
tokenizer=tokenizer,
output_max_length=args.output_max_sequence_length)
else:
config = BertConfig.from_pretrained(pretrain_model_path)
seq2seq_model = Seq2SeqModel.from_pretrained(pretrain_model_path, config=config,
tokenizer=tokenizer,
output_max_length=args.output_max_sequence_length)
seq2seq_model = seq2seq_model.to(self.device)
print(seq2seq_model)
logging.info(seq2seq_model)
self.seq2seq_model = seq2seq_model
self.optimizer = self.init_optimizer()
self.best_score = 0.0
def check_path_exist(self, args):
# 检查args中的各个路径是否存在,如果不存在则创建
if not os.path.exists(args.model_save_path):
os.makedirs(args.model_save_path)
def get_dataloader(self):
train_file = os.path.join(args.data_dir, "train.json")
dev_file = os.path.join(args.data_dir, "dev.json")
train_dataloader = get_dataloader(train_file, self.tokenizer,
max_length=self.args.max_length,
batch_size=self.args.train_batch_size,
shuffle=True)
dev_dataloader = get_test_dataloader(dev_file, self.tokenizer,
max_length=self.args.max_length-self.args.output_max_sequence_length,
batch_size=self.args.eval_batch_size)
return train_dataloader, dev_dataloader
def init_optimizer(self):
no_decay = ['bias', 'LayerNorm.weight', 'transitions']
optimizer_grouped_parameters = [
{'params': [p for n, p in self.seq2seq_model.named_parameters() \
if not any(nd in n for nd in no_decay)], 'weight_decay': self.args.weight_decay},
{'params': [p for n, p in self.seq2seq_model.named_parameters() \
if any(nd in n for nd in no_decay)], 'weight_decay': 0.0},
]
# t_total = len(self.train_dataloader) // self.args.gradient_accumulation_steps * self.args.epochs
optimizer = AdamW(optimizer_grouped_parameters, lr=self.args.learning_rate, eps=1e-8)
# scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=self.args.warmup_rate * t_total,
# num_training_steps=t_total)
return optimizer
def decode_ids(self, input_ids):
pred_ids = []
for idx in input_ids:
if self.tokenizer.token_to_id(self.tokenizer._token_end) == idx:
break
else:
pred_ids.append(idx)
return self.tokenizer.decode(pred_ids)
def eval(self, check=False):
start_time = time.time() ## 得到当前时间
self.seq2seq_model.eval()
preds, golds = [], []
for batch in tqdm(self.eval_dataloader, position=0, leave=True):
inputs = {}
with torch.no_grad():
for k, v in batch.items():
inputs[k] = v.to(self.device)
pred = self.seq2seq_model.predict(**inputs)
import random
if random.random() < 0.1:
print(self.decode_ids(pred[0]))
preds += pred
if check:
break
preds = [self.decode_ids(pred) for pred in preds]
print(preds[:5])
logging.info(preds[:5])
golds = self.golds[:len(preds)]
score = rouge_l(preds=preds, golds=golds)
self.seq2seq_model.train()
end_time = time.time()
spend_time = end_time - start_time
report_string = "eval_score = {}, best rouge-l = {}, spend_time = {}".format(
score, self.best_score, spend_time)
promote = ""
if score > self.best_score:
promote = " *"
self.save(self.model_save_path)
self.best_score = score
print(report_string + promote)
logging.info(report_string + promote)
return score
def save(self, save_path):
"""
保存模型
"""
self.seq2seq_model.save_pretrained(self.model_save_path)
print("{} saved!".format(save_path))
logging.info("{} saved!".format(save_path))
def train(self):
step = 0
report_loss = 0
report_step = 0
total_step = len(self.train_dataloader)
eval_step = int(total_step * self.args.val_check_interval)
self.eval(check=self.args.check)
self.seq2seq_model.train()
fgm = FGM(self.seq2seq_model)
for e in range(self.args.epochs):
for batch in tqdm(self.train_dataloader, position=0, leave=True):
step += 1
# 转移到device上
inputs = {}
for k, v in batch.items():
inputs[k] = v.to(self.device)
loss = self.seq2seq_model(**inputs)
loss.backward()
report_loss += loss.item()
report_step += 1
# # fgm 攻击
# fgm.attack()
# loss_adv = self.seq2seq_model(**inputs)
# loss_adv.backward() # 反向传播,并在正常的grad基础上,累加对抗训练的梯度
# fgm.restore() # 恢复embedding参数
# # fgm 攻击 end
if step % self.args.gradient_accumulation_steps == 0:
torch.nn.utils.clip_grad_norm_(self.seq2seq_model.parameters(), self.args.max_grad_norm)
self.optimizer.step()
# self.scheduler.step() # 更新学习率
self.seq2seq_model.zero_grad()
if step % 50 == 0:
print('epoch:{}, step:{}, train loss:{}'.format(e, step, report_loss / report_step))
logging.info('epoch:{}, step:{}, train loss:{}'.format(e, step, report_loss / report_step))
report_step = 0
report_loss = 0
# if step % eval_step == 0 or step == total_step:
print('epoch:{}, step:{}, train loss:{}'.format(e, step, report_loss / report_step))
logging.info('epoch:{}, step:{}, train loss:{}'.format(e, step, report_loss / report_step))
self.eval()
if __name__ == '__main__':
args_dict = dict(
data_dir='user_data/tmp_data',
pretrain_model_path='user_data/model_data/pretrain_model/new_wobert',
# recent_model_path='user_data/model_data/output_model/seq2seq_model',
recent_model_path=None,
model_save_path='user_data/model_data/output_model/seq2seq_model6',
train_batch_size=8,
eval_batch_size=1,
gradient_accumulation_steps=1,
warmup_rate=0.0,
max_grad_norm=1.0,
max_length=400,
output_max_sequence_length=65,
learning_rate=1e-5,
epochs=20,
weight_decay=0.0,
val_check_interval=0.5,
device='cuda:7',
seed=2021,
check=True,
)
args = argparse.Namespace(**args_dict)
set_logger('unilm6.log')
print("***** Running training *****")
logging.info("***** Running training *****")
for k, v in args.__dict__.items():
print(" {:18s} = {}".format(str(k), str(v)))
logging.info(" {:18s} = {}".format(str(k), str(v)))
trainer = Trainer(args)
set_seed(args.seed)
trainer.train()
print("this is a test...")