-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
155 lines (126 loc) · 5.24 KB
/
model.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
import time
from transformers import AutoModel
import torch
from torch import nn
from torchcrf import CRF
class SqueezeEmbedding(nn.Module):
"""
Squeeze sequence embedding length to the longest one in the batch
当作组件来用就好
"""
def __init__(self, batch_first=True):
super(SqueezeEmbedding, self).__init__()
self.batch_first = batch_first
def forward(self, x, x_len):
"""
sequence -> sort -> pad and pack -> unpack ->unsort
:param x: sequence embedding vectors
:param x_len: numpy/tensor list
:return:
"""
"""sort"""
x_sort_idx = torch.sort(-x_len)[1].long()
x_unsort_idx = torch.sort(x_sort_idx)[1].long()
x_len = x_len[x_sort_idx]
x = x[x_sort_idx]
"""pack"""
x_emb_p = torch.nn.utils.rnn.pack_padded_sequence(x, x_len, batch_first=self.batch_first)
"""unpack: out"""
out = torch.nn.utils.rnn.pad_packed_sequence(x_emb_p, batch_first=self.batch_first) # (sequence, lengths)
out = out[0]
"""unsort"""
out = out[x_unsort_idx]
return out
class BertForAE(nn.Module):
def __init__(self, opt, *args, **kwargs):
super(BertForAE, self).__init__()
self.model_name = 'BertForAE'
self.opt = opt
self.embedding_dim = opt.embedding_dim
self.hidden_dim = opt.hidden_dim
self.tagset_size = opt.tagset_size
self.bert = AutoModel.from_pretrained(opt.model_path)
self.dropout = torch.nn.Dropout(opt.dropout)
self.squeeze_embedding = SqueezeEmbedding()
self.lstm = torch.nn.LSTM(self.embedding_dim, self.hidden_dim // 2, num_layers=1, bidirectional=True, batch_first=True)
self.hidden2tag = torch.nn.Linear(self.hidden_dim*2, self.tagset_size)
self.crf = CRF(self.tagset_size, batch_first=True)
def calculate_cosin(self, context_output, att_hidden):
'''
context_output (batchsize, seqlen, hidden_dim)
att_hidden (batchsize, hidden_dim)
'''
# [128, 64, 312]
batchsize,seqlen,hidden_dim = context_output.size()
# [128, 312] --> [128, 1, 312] --> [128, 64, 312]
att_hidden = att_hidden.unsqueeze(1).repeat(1,seqlen,1)
context_output = context_output.float()
att_hidden = att_hidden.float()
# [128, 64]
# print(torch.sum(context_output*att_hidden, dim=-1).shape)
cos = torch.sum(context_output*att_hidden, dim=-1)/(torch.norm(context_output, dim=-1)*torch.norm(att_hidden, dim=-1))
# [128, 64, 1]
cos = cos.unsqueeze(-1)
cos_output = context_output*cos
outputs = torch.cat([context_output, cos_output], dim=-1)
return outputs
def forward(self, inputs):
context, att, target = inputs[0], inputs[1], inputs[2]
context_len = torch.sum(context != 0, dim=-1).cpu()
att_len = torch.sum(att != 0, dim=-1).cpu()
context = self.squeeze_embedding(context, context_len)
context = self.bert(context)
context = context[0]
# context = self.word_embeds(context)
context_output, _ = self.lstm(context)
att = self.squeeze_embedding(att, att_len)
att = self.bert(att)
att = att[0]
# att = self.word_embeds(att)
_, att_hidden = self.lstm(att)
att_hidden = torch.cat([att_hidden[0][-2],att_hidden[0][-1]], dim=-1)
outputs = self.calculate_cosin(context_output, att_hidden)
outputs = self.dropout(outputs)
outputs = self.hidden2tag(outputs)
#CRF
# outputs = outputs.transpose(0,1).contiguous()
outputs = self.crf.decode(outputs)
return outputs
def log_likelihood(self, inputs):
context, att, target = inputs[0], inputs[1], inputs[2]
context_len = torch.sum(context != 0, dim=-1).cpu()
att_len = torch.sum(att != 0, dim=-1).cpu()
target_len = torch.sum(target != 0, dim=-1).cpu()
target = self.squeeze_embedding(target, target_len)
# target = target.transpose(0,1).contiguous()
context = self.squeeze_embedding(context, context_len)
context = self.bert(context)
context = context[0]
# context = self.word_embeds(context)
context_output, _ = self.lstm(context)
att = self.squeeze_embedding(att, att_len)
att = self.bert(att)
att = att[0]
# att = self.word_embeds(att)
_, att_hidden = self.lstm(att)
att_hidden = torch.cat([att_hidden[0][-2],att_hidden[0][-1]], dim=-1)
outputs = self.calculate_cosin(context_output, att_hidden)
outputs = self.dropout(outputs)
outputs = self.hidden2tag(outputs)
#CRF
# outputs = outputs.transpose(0,1).contiguous()
return - self.crf(outputs, target.long())
def load(self, path):
"""
可加载指定路径的模型
"""
self.load_state_dict(torch.load(path))
def save(self, name=None):
"""
保存模型,默认使用“模型名字+时间”作为文件名
"""
if name is None:
prefix = './checkpoints/' + self.model_name + '_'
name = time.strftime(prefix + '%m%d_%H:%M:%S.pth')
torch.save(self.state_dict(), name)
return name