-
Notifications
You must be signed in to change notification settings - Fork 2
/
model_process.py
244 lines (195 loc) · 9.83 KB
/
model_process.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
from zhon.hanzi import punctuation
import pandas as pd
from data_loader import id2tag
import torch
import torch.nn as nn
import torch.optim as optim
import sys
from tqdm import tqdm
from util import generateResult
from util import f1_score
import copy
from transformers import AdamW
from util import acquireEntity
def train(net, trainIter, validIter, config):
DEVICE = config['DEVICE']
modelSavePath = config['modelSavePath']
epochNum = config['model']['epochNum']
learningRate = config['model']['learningRate']
earlyStop = config['model']['earlyStop']
#权重初始化
for name, value in net.named_parameters():
if 'pretrainedModel' not in name:
if value.dim() > 1: nn.init.xavier_uniform_(value)
# no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
# bert_param_no = [value for name, value in net.named_parameters() if name in no_decay and 'bertModel' in name]
# bert_param_yes = [value for name, value in net.named_parameters() if name not in no_decay and 'bertModel' in name]
# other_param_no = [value for name, value in net.named_parameters() if name in no_decay and 'bertModel' not in name]
# other_param_yes = [value for name, value in net.named_parameters() if name not in no_decay and 'bertModel' not in name]
# optimizer_grouped_parameters = [
# {'params': bert_param_yes, 'weight_decay': 0.01, 'lr': learningRate},
# {'params': bert_param_no, 'weight_decay': 0.0, 'lr': learningRate},
# {'params': other_param_yes, 'weight_decay': 0.01, 'lr': 0.001},
# {'params': other_param_no, 'weight_decay': 0.0, 'lr': 0.001}]
bert_params = [value for name, value in net.named_parameters() if 'pretrainedModel' in name]
other_params = [value for name, value in net.named_parameters() if 'pretrainedModel' not in name]
params = [{'params':bert_params, 'lr': 5e-5},
{'params':other_params, 'lr':learningRate}]
optimizer = AdamW(params, eps=1e-8)
earlyNumber, beforeLoss = 0, sys.maxsize
trainLossSave, validLossSave, f1ScoreSave, accurateSave, recallSave = 0, 0, 0, 0, 0
for epoch in range(epochNum):
print ('第%d次迭代\n' % (epoch+1))
#训练
net.train()
trainLoss, number = 0, 0
for batchSentence, batchTag, _, _ in tqdm(trainIter):
batchSentence = batchSentence.to(DEVICE)
batchTag = batchTag.to(DEVICE)
net.zero_grad()
loss = net(batchSentence, batchTag)
#多卡训练
if torch.cuda.device_count() > 1: loss = loss.mean()
loss.backward()
#梯度裁剪
nn.utils.clip_grad_norm_(net.parameters(), 1.0)
optimizer.step()
trainLoss += loss.item(); number += 1
trainLoss = trainLoss / number
#验证
net.eval()
validLoss, number = 0, 0
yTrue, yPre, ySentence, probArr = [], [], [], []
with torch.no_grad():
for batchSentence, batchTag, lenList, originSentence in tqdm(validIter):
batchSentence = batchSentence.to(DEVICE)
batchTag = batchTag.to(DEVICE)
loss = net(batchSentence, batchTag)
#多卡训练
if torch.cuda.device_count() > 1:
loss = loss.mean()
tagPre, prob = net.module.decode(batchSentence)
else: tagPre, prob = net.decode(batchSentence)
tagTrue = [element[:length] for element, length in zip(batchTag.cpu().numpy(), lenList)]
yTrue.extend(tagTrue); yPre.extend(tagPre); ySentence.extend(originSentence)
probArr.extend(prob)
validLoss += loss.item(); number += 1
yTrue2tag = [[id2tag[element2] for element2 in element1] for element1 in yTrue]
yPre2tag = [[id2tag[element2] for element2 in element1] for element1 in yPre]
assert len(yTrue2tag) == len(yPre2tag); assert len(ySentence) == len(yTrue2tag)
f1Score, accurate, recall = f1_score(y_true=yTrue2tag, y_pred=yPre2tag)
validLoss = validLoss / number
print ('训练损失为: %f\n' % trainLoss)
print ('验证损失为: %f / %f\n' % (validLoss, beforeLoss))
print ('f1_Score、accurate、recall: %f、%f、%f\n' % (f1Score, accurate, recall))
if validLoss < beforeLoss:
beforeLoss = validLoss
if torch.cuda.device_count() > 1:
torch.save(net.module.state_dict(), modelSavePath)
else: torch.save(net.state_dict(), modelSavePath)
trainLossSave, validLossSave = trainLoss, validLoss
f1ScoreSave, accurateSave, recallSave = f1Score, accurate, recall
if 'validResultPath' in config.keys():
path = config['validResultPath']
f = open(path, 'w', encoding='utf-8', errors='ignore')
for sentence, prob in zip(ySentence, probArr):
for sentenceEle, probEle in zip(sentence, prob):
probEle = '\t'.join([str(element) for element in probEle])
f.write('%s\t%s\n' %(sentenceEle, probEle))
f.write('\n')
f.close()
#早停机制
if validLoss > beforeLoss:
earlyNumber += 1
print('earyStop: %d / %d\n' % (earlyNumber, earlyStop))
else:
earlyNumber = 0
if earlyNumber >= earlyStop:
break
#计算验证集中的实际效果
###临时###
f = open('temp.txt', 'w', encoding='utf-8', errors='ignore')
for sentence, trueTag, preTag in zip(ySentence, yTrue2tag, yPre2tag):
trueEntity = '@'.join(acquireEntity([sentence], [trueTag], method='BIOES'))
preEntity = '@'.join(acquireEntity([sentence], [preTag], method='BIOES'))
if trueEntity != preEntity:
f.write(''.join(sentence) + '\n')
f.write('True:' + trueEntity + '\n')
f.write('Pre:' + preEntity + '\n')
f.close()
return trainLossSave, validLossSave, f1ScoreSave, accurateSave, recallSave
def test(net, testIter, config):
DEVICE = config['DEVICE']
sentenceArr, tagArr, probArr = [], [], []
with torch.no_grad():
for batchSentence, batchOriginSentence, _ in tqdm(testIter):
batchSentence = batchSentence.to(DEVICE)
if torch.cuda.device_count() > 1:
tagPre, prob = net.module.decode(batchSentence)
else: tagPre, prob = net.decode(batchSentence)
tagArr.extend(tagPre)
sentenceArr.extend(batchOriginSentence)
probArr.extend(prob)
tagArr =[[id2tag[element2] for element2 in element1]for element1 in tagArr]
#保存中间结果
if 'resultPath' in config.keys():
path = config['resultPath']
f = open(path, 'w', encoding='utf-8', errors='ignore')
for sentence, prob in zip(sentenceArr, probArr):
for sentenceEle, probEle in zip(sentence, prob):
probEle = '\t'.join([str(element) for element in probEle])
f.write('%s\t%s\n' %(sentenceEle, probEle))
f.write('\n')
f.close()
disappear1, disappear2 = generateResult(sentenceArr, tagArr, config)
return disappear1, disappear2
def valid(net, validIter, config):
def filter_word(w):
import string
for word in w:
if word in ['?','《','🔺','!','#','%',',','Ⅲ','》','丨','、','', '…',
'👍','。','😎','/','】','-','⚠️',':','✅','㊙️','!','🔥',',',
'.','——', '“', '”', '!', ' ']:
return ''
if word in ['(', ')', '(', ')', '?']: continue
if word in punctuation + string.punctuation: return ''
return w
DEVICE = config['DEVICE']
sentenceArr, tagArr, probArr = [], [], []
with torch.no_grad():
for batchSentence, batchTag, lenList, batchOriginSentence in tqdm(validIter):
batchSentence = batchSentence.to(DEVICE)
if torch.cuda.device_count() > 1:
tagPre, prob = net.module.decode(batchSentence)
else: tagPre, prob = net.decode(batchSentence)
tagArr.extend(tagPre)
sentenceArr.extend(batchOriginSentence)
probArr.extend(prob)
tagArr =[[id2tag[element2] for element2 in element1]for element1 in tagArr]
lenPath, comparePath = config['lenPath'], config['comparePath']
validLen = open(lenPath, 'r', encoding='utf-8', errors='ignore')
compare = open(comparePath, 'w', encoding='utf-8', errors='ignore')
lenList, start = [], 0
TP, FP, FN = 0, 0, 0
for line in validLen.readlines():
id, length, trueEntityArr = line.strip('\n').split('\t')[0], int(line.strip('\n').split('\t')[1]), line.strip('\n').split('\t')[2]
trueEntityArr = trueEntityArr.split(';')
sentenceElement, tagElement = sentenceArr[start:start+length], tagArr[start:start+length]
start += length
preEntityArr = acquireEntity(sentenceElement, tagElement)
#过滤无用实体
preEntityArr = [entity for entity in preEntityArr if filter_word(entity) != '']
compare.write(id + '\t' + ';'.join(trueEntityArr) + '\t' + ';'.join(preEntityArr) + '\n')
TPE = 0
for element in preEntityArr:
if element in trueEntityArr: TPE += 1
FPE = len(preEntityArr) - TPE
FNE = len(trueEntityArr) - TPE
TP += TPE; FP += FPE; FN += FNE
validLen.close(); compare.close()
if TP+FP == 0 or TP+FN == 0: return 0, 0, 0
MicroP = TP / (TP + FP); MicroR = TP / (TP + FN)
if MicroP + MicroR == 0: return 0, 0, 0
MicroF = 2 * MicroP * MicroR / (MicroP + MicroR)
print('validF1Score %f, validAccurate %f, validRecall %f\n' %(MicroF, MicroP, MicroR))
return MicroF, MicroP, MicroR