forked from gaohongkui/GlobalPointer_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
284 lines (231 loc) · 11.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
Date: 2021-05-31 19:50:58
LastEditors: GodK
"""
import os
import config
import sys
import torch
import json
from transformers import BertTokenizerFast, BertModel
from common.utils import Preprocessor, multilabel_categorical_crossentropy
from models.GlobalPointer import DataMaker, MyDataset, GlobalPointer, MetricsCalculator
from torch.utils.data import DataLoader, Dataset
from tqdm import tqdm
import glob
import wandb
from evaluate import evaluate
import time
config = config.train_config
hyper_parameters = config["hyper_parameters"]
os.environ["TOKENIZERS_PARALLELISM"] = "true"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
config["num_workers"] = 6 if sys.platform.startswith("linux") else 0
# for reproductivity
torch.manual_seed(hyper_parameters["seed"]) # pytorch random seed
torch.backends.cudnn.deterministic = True
if config["logger"] == "wandb" and config["run_type"] == "train":
# init wandb
wandb.init(project="GlobalPointer_" + config["exp_name"],
config=hyper_parameters # Initialize config
)
wandb.run.name = config["run_name"] + "_" + wandb.run.id
model_state_dict_dir = wandb.run.dir
logger = wandb
else:
model_state_dict_dir = os.path.join(config["path_to_save_model"], config["exp_name"],
time.strftime("%Y-%m-%d_%H.%M.%S", time.gmtime()))
if not os.path.exists(model_state_dict_dir):
os.makedirs(model_state_dict_dir)
tokenizer = BertTokenizerFast.from_pretrained(config["bert_path"], add_special_tokens=True, do_lower_case=False)
def load_data(data_path, data_type="train"):
"""读取数据集
Args:
data_path (str): 数据存放路径
data_type (str, optional): 数据类型. Defaults to "train".
Returns:
(json): train和valid中一条数据格式:{"text":"","entity_list":[(start, end, label), (start, end, label)...]}
"""
if data_type == "train" or data_type == "valid":
datas = []
with open(data_path, encoding="utf-8") as f:
for line in f:
line = json.loads(line)
item = {}
item["text"] = line["text"]
item["entity_list"] = []
for k, v in line['label'].items():
for spans in v.values():
for start, end in spans:
item["entity_list"].append((start, end, k))
datas.append(item)
return datas
else:
return json.load(open(data_path, encoding="utf-8"))
ent2id_path = os.path.join(config["data_home"], config["exp_name"], config["ent2id"])
ent2id = load_data(ent2id_path, "ent2id")
ent_type_size = len(ent2id)
def data_generator(data_type="train"):
"""
读取数据,生成DataLoader。
"""
if data_type == "train":
train_data_path = os.path.join(config["data_home"], config["exp_name"], config["train_data"])
train_data = load_data(train_data_path, "train")
valid_data_path = os.path.join(config["data_home"], config["exp_name"], config["valid_data"])
valid_data = load_data(valid_data_path, "valid")
elif data_type == "valid":
valid_data_path = os.path.join(config["data_home"], config["exp_name"], config["valid_data"])
valid_data = load_data(valid_data_path, "valid")
train_data = []
all_data = train_data + valid_data
# TODO:句子截取
max_tok_num = 0
for sample in all_data:
tokens = tokenizer(sample["text"])["input_ids"]
max_tok_num = max(max_tok_num, len(tokens))
assert max_tok_num <= hyper_parameters[
"max_seq_len"], f'数据文本最大token数量{max_tok_num}超过预设{hyper_parameters["max_seq_len"]}'
max_seq_len = min(max_tok_num, hyper_parameters["max_seq_len"])
data_maker = DataMaker(tokenizer)
if data_type == "train":
# train_inputs = data_maker.generate_inputs(train_data, max_seq_len, ent2id)
# valid_inputs = data_maker.generate_inputs(valid_data, max_seq_len, ent2id)
train_dataloader = DataLoader(MyDataset(train_data),
batch_size=hyper_parameters["batch_size"],
shuffle=True,
num_workers=config["num_workers"],
drop_last=False,
collate_fn=lambda x: data_maker.generate_batch(x, max_seq_len, ent2id)
)
valid_dataloader = DataLoader(MyDataset(valid_data),
batch_size=hyper_parameters["batch_size"],
shuffle=True,
num_workers=config["num_workers"],
drop_last=False,
collate_fn=lambda x: data_maker.generate_batch(x, max_seq_len, ent2id)
)
# for batch in train_dataloader:
# print(batch[1].shape)
# print(hyper_parameters["batch_size"])
# break
return train_dataloader, valid_dataloader
elif data_type == "valid":
# valid_inputs = data_maker.generate_inputs(valid_data, max_seq_len, ent2id)
valid_dataloader = DataLoader(MyDataset(valid_data),
batch_size=hyper_parameters["batch_size"],
shuffle=True,
num_workers=config["num_workers"],
drop_last=False,
collate_fn=lambda x: data_maker.generate_batch(x, max_seq_len, ent2id)
)
return valid_dataloader
metrics = MetricsCalculator()
def train_step(batch_train, model, optimizer, criterion):
# batch_input_ids:(batch_size, seq_len) batch_labels:(batch_size, ent_type_size, seq_len, seq_len)
batch_samples, batch_input_ids, batch_attention_mask, batch_token_type_ids, batch_labels = batch_train
batch_input_ids, batch_attention_mask, batch_token_type_ids, batch_labels = (batch_input_ids.to(device),
batch_attention_mask.to(device),
batch_token_type_ids.to(device),
batch_labels.to(device)
)
logits = model(batch_input_ids, batch_attention_mask, batch_token_type_ids)
loss = criterion(logits, batch_labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
return loss.item()
encoder = BertModel.from_pretrained(config["bert_path"])
model = GlobalPointer(encoder, ent_type_size, 64)
model = model.to(device)
if config["logger"] == "wandb" and config["run_type"] == "train":
wandb.watch(model)
def train(model, dataloader, epoch, optimizer):
model.train()
# loss func
def loss_fun(y_true, y_pred):
"""
y_true:(batch_size, ent_type_size, seq_len, seq_len)
y_pred:(batch_size, ent_type_size, seq_len, seq_len)
"""
batch_size, ent_type_size = y_pred.shape[:2]
y_true = y_true.reshape(batch_size * ent_type_size, -1)
y_pred = y_pred.reshape(batch_size * ent_type_size, -1)
loss = multilabel_categorical_crossentropy(y_true, y_pred)
return loss
# scheduler
if hyper_parameters["scheduler"] == "CAWR":
T_mult = hyper_parameters["T_mult"]
rewarm_epoch_num = hyper_parameters["rewarm_epoch_num"]
scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer,
len(train_dataloader) * rewarm_epoch_num,
T_mult)
elif hyper_parameters["scheduler"] == "Step":
decay_rate = hyper_parameters["decay_rate"]
decay_steps = hyper_parameters["decay_steps"]
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=decay_steps, gamma=decay_rate)
pbar = tqdm(enumerate(dataloader), total=len(dataloader))
total_loss = 0.
for batch_ind, batch_data in pbar:
loss = train_step(batch_data, model, optimizer, loss_fun)
total_loss += loss
avg_loss = total_loss / (batch_ind + 1)
scheduler.step()
pbar.set_description(f'Project:{config["exp_name"]}, Epoch: {epoch + 1}/{hyper_parameters["epochs"]}, Step: {batch_ind + 1}/{len(dataloader)}')
pbar.set_postfix(loss=avg_loss, lr=optimizer.param_groups[0]["lr"])
if config["logger"] == "wandb" and batch_ind % config["log_interval"] == 0:
logger.log({
"epoch": epoch,
"train_loss": avg_loss,
"learning_rate": optimizer.param_groups[0]['lr'],
})
def valid_step(batch_valid, model):
batch_samples, batch_input_ids, batch_attention_mask, batch_token_type_ids, batch_labels = batch_valid
batch_input_ids, batch_attention_mask, batch_token_type_ids, batch_labels = (batch_input_ids.to(device),
batch_attention_mask.to(device),
batch_token_type_ids.to(device),
batch_labels.to(device)
)
with torch.no_grad():
logits = model(batch_input_ids, batch_attention_mask, batch_token_type_ids)
sample_f1, sample_precision, sample_recall = metrics.get_evaluate_fpr(logits, batch_labels)
return sample_f1, sample_precision, sample_recall
def valid(model, dataloader):
model.eval()
total_f1, total_precision, total_recall = 0., 0., 0.
for batch_data in tqdm(dataloader, desc="Validating"):
f1, precision, recall = valid_step(batch_data, model)
total_f1 += f1
total_precision += precision
total_recall += recall
avg_f1 = total_f1 / (len(dataloader))
avg_precision = total_precision / (len(dataloader))
avg_recall = total_recall / (len(dataloader))
print("******************************************")
print(f'avg_precision: {avg_precision}, avg_recall: {avg_recall}, avg_f1: {avg_f1}')
print("******************************************")
if config["logger"] == "wandb":
logger.log({"valid_precision": avg_precision, "valid_recall": avg_recall, "valid_f1": avg_f1})
return avg_f1
if __name__ == '__main__':
if config["run_type"] == "train":
train_dataloader, valid_dataloader = data_generator()
# optimizer
init_learning_rate = float(hyper_parameters["lr"])
optimizer = torch.optim.Adam(model.parameters(), lr=init_learning_rate)
max_f1 = 0.
for epoch in range(hyper_parameters["epochs"]):
train(model, train_dataloader, epoch, optimizer)
valid_f1 = valid(model, valid_dataloader)
if valid_f1 > max_f1:
max_f1 = valid_f1
if valid_f1 > config["f1_2_save"]: # save the best model
model_state_num = len(glob.glob(model_state_dict_dir + "/model_state_dict_*.pt"))
torch.save(model.state_dict(),
os.path.join(model_state_dict_dir, "model_state_dict_{}.pt".format(model_state_num)))
print(f"Best F1: {max_f1}")
print("******************************************")
if config["logger"] == "wandb":
logger.log({"Best_F1": max_f1})
elif config["run_type"] == "eval":
evaluate()