forked from chenxiaoyouyou/Bert-BiLSTM-CRF-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
100 lines (88 loc) · 3.13 KB
/
utils.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
# coding=utf-8
import torch
import os
import datetime
import unicodedata
class InputFeatures(object):
def __init__(self, input_id, label_id, input_mask):
self.input_id = input_id
self.label_id = label_id
self.input_mask = input_mask
def load_vocab(vocab_file):
"""Loads a vocabulary file into a dictionary."""
vocab = {}
index = 0
with open(vocab_file, "r", encoding="utf-8") as reader:
while True:
token = reader.readline()
if not token:
break
token = token.strip()
vocab[token] = index
index += 1
return vocab
def read_corpus(path, max_length, label_dic, vocab):
"""
:param path:数据文件路径
:param max_length: 最大长度
:param label_dic: 标签字典
:return:
"""
file = open(path, encoding='utf-8')
content = file.readlines()
file.close()
result = []
for line in content:
text, label = line.strip().split('|||')
tokens = text.split()
label = label.split()
if len(tokens) > max_length-2:
tokens = tokens[0:(max_length-2)]
label = label[0:(max_length-2)]
tokens_f =['[CLS]'] + tokens + ['[SEP]']
label_f = ["<start>"] + label + ['<eos>']
input_ids = [int(vocab[i]) if i in vocab else int(vocab['[UNK]']) for i in tokens_f]
label_ids = [label_dic[i] for i in label_f]
input_mask = [1] * len(input_ids)
while len(input_ids) < max_length:
input_ids.append(0)
input_mask.append(0)
label_ids.append(label_dic['<pad>'])
assert len(input_ids) == max_length
assert len(input_mask) == max_length
assert len(label_ids) == max_length
feature = InputFeatures(input_id=input_ids, input_mask=input_mask, label_id=label_ids)
result.append(feature)
return result
def save_model(model, epoch, path='result', **kwargs):
"""
默认保留所有模型
:param model: 模型
:param path: 保存路径
:param loss: 校验损失
:param last_loss: 最佳epoch损失
:param kwargs: every_epoch or best_epoch
:return:
"""
if not os.path.exists(path):
os.mkdir(path)
if kwargs.get('name', None) is None:
cur_time = datetime.datetime.now().strftime('%Y-%m-%d#%H:%M:%S')
name = cur_time + '--epoch:{}'.format(epoch)
full_name = os.path.join(path, name)
torch.save(model.state_dict(), full_name)
print('Saved model at epoch {} successfully'.format(epoch))
with open('{}/checkpoint'.format(path), 'w') as file:
file.write(name)
print('Write to checkpoint')
def load_model(model, path='result', **kwargs):
if kwargs.get('name', None) is None:
with open('{}/checkpoint'.format(path)) as file:
content = file.read().strip()
name = os.path.join(path, content)
else:
name=kwargs['name']
name = os.path.join(path,name)
model.load_state_dict(torch.load(name, map_location=lambda storage, loc: storage))
print('load model {} successfully'.format(name))
return model