-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
240 lines (216 loc) · 10.8 KB
/
main.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
from pprint import pprint
import os
import logging
import shutil
from sklearn.metrics import accuracy_score, f1_score, multilabel_confusion_matrix, classification_report
import torch
import torch.nn as nn
import numpy as np
from torch.utils.data import DataLoader, RandomSampler
from transformers import BertTokenizer
import bert_config
import preprocess
import dataset
import models
import utils
logger = logging.getLogger(__name__)
class Trainer:
def __init__(self, args, train_loader, dev_loader, test_loader):
self.args = args
gpu_ids = args.gpu_ids.split(',')
self.device = torch.device("cpu" if gpu_ids[0] == '-1' else "cuda:" + gpu_ids[0])
self.model = models.BertForMultiLabelClassification(args)
self.optimizer = torch.optim.Adam(params=self.model.parameters(), lr=self.args.lr)
self.criterion = nn.BCEWithLogitsLoss()
self.train_loader = train_loader
self.dev_loader = dev_loader
self.test_loader = test_loader
self.model.to(self.device)
def load_ckp(self, model, optimizer, checkpoint_path):
checkpoint = torch.load(checkpoint_path)
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
epoch = checkpoint['epoch']
loss = checkpoint['loss']
return model, optimizer, epoch, loss
def save_ckp(self, state, checkpoint_path):
torch.save(state, checkpoint_path)
"""
def save_ckp(self, state, is_best, checkpoint_path, best_model_path):
tmp_checkpoint_path = checkpoint_path
torch.save(state, tmp_checkpoint_path)
if is_best:
tmp_best_model_path = best_model_path
shutil.copyfile(tmp_checkpoint_path, tmp_best_model_path)
"""
def train(self):
total_step = len(self.train_loader) * self.args.train_epochs
global_step = 0
eval_step = 100
best_dev_micro_f1 = 0.0
for epoch in range(args.train_epochs):
for train_step, train_data in enumerate(self.train_loader):
self.model.train()
token_ids = train_data['token_ids'].to(self.device)
attention_masks = train_data['attention_masks'].to(self.device)
token_type_ids = train_data['token_type_ids'].to(self.device)
labels = train_data['labels'].to(self.device)
train_outputs = self.model(token_ids, attention_masks, token_type_ids)
loss = self.criterion(train_outputs, labels)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
logger.info(
"【train】 epoch:{} step:{}/{} loss:{:.6f}".format(epoch, global_step, total_step, loss.item()))
global_step += 1
if global_step % eval_step == 0:
dev_loss, dev_outputs, dev_targets = self.dev()
accuracy, micro_f1, macro_f1 = self.get_metrics(dev_outputs, dev_targets)
logger.info(
"【dev】 loss:{:.6f} accuracy:{:.4f} micro_f1:{:.4f} macro_f1:{:.4f}".format(dev_loss, accuracy,
micro_f1, macro_f1))
if macro_f1 > best_dev_micro_f1:
logger.info("------------>保存当前最好的模型")
checkpoint = {
'epoch': epoch,
'loss': dev_loss,
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
}
best_dev_micro_f1 = macro_f1
checkpoint_path = os.path.join(self.args.output_dir, 'best.pt')
self.save_ckp(checkpoint, checkpoint_path)
def dev(self):
self.model.eval()
total_loss = 0.0
dev_outputs = []
dev_targets = []
with torch.no_grad():
for dev_step, dev_data in enumerate(self.dev_loader):
token_ids = dev_data['token_ids'].to(self.device)
attention_masks = dev_data['attention_masks'].to(self.device)
token_type_ids = dev_data['token_type_ids'].to(self.device)
labels = dev_data['labels'].to(self.device)
outputs = self.model(token_ids, attention_masks, token_type_ids)
loss = self.criterion(outputs, labels)
# val_loss = val_loss + ((1 / (dev_step + 1))) * (loss.item() - val_loss)
total_loss += loss.item()
outputs = torch.sigmoid(outputs).cpu().detach().numpy().tolist()
outputs = (np.array(outputs) > 0.6).astype(int)
dev_outputs.extend(outputs.tolist())
dev_targets.extend(labels.cpu().detach().numpy().tolist())
return total_loss, dev_outputs, dev_targets
def test(self, checkpoint_path):
model = self.model
optimizer = self.optimizer
model, optimizer, epoch, loss = self.load_ckp(model, optimizer, checkpoint_path)
model.eval()
model.to(self.device)
total_loss = 0.0
test_outputs = []
test_targets = []
with torch.no_grad():
for test_step, test_data in enumerate(self.test_loader):
token_ids = test_data['token_ids'].to(self.device)
attention_masks = test_data['attention_masks'].to(self.device)
token_type_ids = test_data['token_type_ids'].to(self.device)
labels = test_data['labels'].to(self.device)
outputs = model(token_ids, attention_masks, token_type_ids)
loss = self.criterion(outputs, labels)
# val_loss = val_loss + ((1 / (dev_step + 1))) * (loss.item() - val_loss)
total_loss += loss.item()
outputs = torch.sigmoid(outputs).cpu().detach().numpy().tolist()
outputs = (np.array(outputs) > 0.6).astype(int)
test_outputs.extend(outputs.tolist())
test_targets.extend(labels.cpu().detach().numpy().tolist())
return total_loss, test_outputs, test_targets
def predict(self, tokenizer, text, id2label, args):
model = self.model
optimizer = self.optimizer
checkpoint = os.path.join(args.output_dir, 'best.pt')
model, optimizer, epoch, loss = self.load_ckp(model, optimizer, checkpoint)
model.eval()
model.to(self.device)
with torch.no_grad():
inputs = tokenizer.encode_plus(text=text,
add_special_tokens=True,
max_length=args.max_seq_len,
truncation='longest_first',
padding="max_length",
return_token_type_ids=True,
return_attention_mask=True,
return_tensors='pt')
token_ids = inputs['input_ids'].to(self.device)
attention_masks = inputs['attention_mask'].to(self.device)
token_type_ids = inputs['token_type_ids'].to(self.device)
outputs = model(token_ids, attention_masks, token_type_ids)
outputs = torch.sigmoid(outputs).cpu().detach().numpy().tolist()
outputs = (np.array(outputs) > 0.5).astype(int)
outputs = np.where(outputs[0] == 1)[0].tolist()
if len(outputs) != 0:
outputs = [id2label[i] for i in outputs]
return outputs
else:
return '不好意思,我没有识别出来'
def get_metrics(self, outputs, targets):
accuracy = accuracy_score(targets, outputs)
micro_f1 = f1_score(targets, outputs, average='micro')
macro_f1 = f1_score(targets, outputs, average='macro')
return accuracy, micro_f1, macro_f1
def get_classification_report(self, outputs, targets, labels):
# confusion_matrix = multilabel_confusion_matrix(targets, outputs)
report = classification_report(targets, outputs, target_names=labels)
return report
if __name__ == '__main__':
args = bert_config.Args().get_parser()
utils.utils.set_seed(args.seed)
utils.utils.set_logger(os.path.join(args.log_dir, 'main.log'))
processor = preprocess.Processor()
label2id = {}
id2label = {}
with open('./data/final_data/labels.txt', 'r') as fp:
labels = fp.read().strip().split('\n')
for i, label in enumerate(labels):
label2id[label] = i
id2label[i] = label
print(label2id)
train_out = preprocess.get_out(processor, './data/raw_data/train.json', args, label2id, 'train')
train_features, train_callback_info = train_out
train_dataset = dataset.MLDataset(train_features)
train_sampler = RandomSampler(train_dataset)
train_loader = DataLoader(dataset=train_dataset,
batch_size=args.train_batch_size,
sampler=train_sampler,
num_workers=2)
dev_out = preprocess.get_out(processor, './data/raw_data/dev.json', args, label2id, 'dev')
dev_features, dev_callback_info = dev_out
dev_dataset = dataset.MLDataset(dev_features)
dev_loader = DataLoader(dataset=dev_dataset,
batch_size=args.eval_batch_size,
num_workers=2)
trainer = Trainer(args, train_loader, dev_loader, dev_loader)
# 训练和验证
trainer.train()
# 测试
logger.info('========进行测试========')
checkpoint_path = './checkpoints/best.pt'
total_loss, test_outputs, test_targets = trainer.test(checkpoint_path)
accuracy, micro_f1, macro_f1 = trainer.get_metrics(test_outputs, test_targets)
logger.info("【test】 loss:{:.6f} accuracy:{:.4f} micro_f1:{:.4f} macro_f1:{:.4f}".format(total_loss, accuracy, micro_f1, macro_f1))
report = trainer.get_classification_report(test_outputs, test_targets, labels)
logger.info(report)
# 预测
trainer = Trainer(args, None, None, None)
checkpoint_path = './checkpoints/best.pt'
tokenizer = BertTokenizer.from_pretrained(args.bert_dir)
# 读取test1.json里面的数据
with open(os.path.join('./data/raw_data/test1.json'), 'r') as fp:
lines = fp.read().strip().split('\n')
for line in lines:
text = eval(line)['text']
print(text)
result = trainer.predict(tokenizer, text, id2label, args)
print(result)
# 预测单条
text = '8岁男童海螺沟失联13日,父母悬赏10万寻子,马上就到他9岁生日了'
print(trainer.predict(tokenizer, text, id2label, args))