-
Notifications
You must be signed in to change notification settings - Fork 32
/
model.py
216 lines (182 loc) · 8.94 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
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
import config
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
from torch_scatter import scatter_max
from data_utils import UNK_ID
INF = 1e12
class Encoder(nn.Module):
def __init__(self, embeddings, vocab_size, embedding_size, hidden_size, num_layers, dropout):
super(Encoder, self).__init__()
self.embedding = nn.Embedding(vocab_size, embedding_size)
self.tag_embedding = nn.Embedding(3, 3)
lstm_input_size = embedding_size + 3
if embeddings is not None:
self.embedding = nn.Embedding(vocab_size, embedding_size). \
from_pretrained(embeddings, freeze=config.freeze_embedding)
self.num_layers = num_layers
if self.num_layers == 1:
dropout = 0.0
self.lstm = nn.LSTM(lstm_input_size, hidden_size, dropout=dropout,
num_layers=num_layers, bidirectional=True, batch_first=True)
self.linear_trans = nn.Linear(2 * hidden_size, 2 * hidden_size)
self.update_layer = nn.Linear(
4 * hidden_size, 2 * hidden_size, bias=False)
self.gate = nn.Linear(4 * hidden_size, 2 * hidden_size, bias=False)
def gated_self_attn(self, queries, memories, mask):
# queries: [b,t,d]
# memories: [b,t,d]
# mask: [b,t]
energies = torch.matmul(queries, memories.transpose(1, 2)) # [b, t, t]
mask = mask.unsqueeze(1)
energies = energies.masked_fill(mask == 0, value=-1e12)
scores = F.softmax(energies, dim=2)
context = torch.matmul(scores, queries)
inputs = torch.cat([queries, context], dim=2)
f_t = torch.tanh(self.update_layer(inputs))
g_t = torch.sigmoid(self.gate(inputs))
updated_output = g_t * f_t + (1 - g_t) * queries
return updated_output
def forward(self, src_seq, src_len, tag_seq):
total_length = src_seq.size(1)
embedded = self.embedding(src_seq)
tag_embedded = self.tag_embedding(tag_seq)
embedded = torch.cat((embedded, tag_embedded), dim=2)
packed = pack_padded_sequence(embedded,
src_len,
batch_first=True,
enforce_sorted=False)
outputs, states = self.lstm(packed) # states : tuple of [4, b, d]
outputs, _ = pad_packed_sequence(outputs,
batch_first=True,
total_length=total_length) # [b, t, d]
h, c = states
# self attention
mask = torch.sign(src_seq)
memories = self.linear_trans(outputs)
outputs = self.gated_self_attn(outputs, memories, mask)
_, b, d = h.size()
h = h.view(2, 2, b, d) # [n_layers, bi, b, d]
h = torch.cat((h[:, 0, :, :], h[:, 1, :, :]), dim=-1)
c = c.view(2, 2, b, d)
c = torch.cat((c[:, 0, :, :], c[:, 1, :, :]), dim=-1)
concat_states = (h, c)
return outputs, concat_states
class Decoder(nn.Module):
def __init__(self, embeddings, vocab_size,
embedding_size, hidden_size, num_layers, dropout):
super(Decoder, self).__init__()
self.vocab_size = vocab_size
self.embedding = nn.Embedding(vocab_size, embedding_size)
if embeddings is not None:
self.embedding = nn.Embedding(vocab_size, embedding_size). \
from_pretrained(embeddings, freeze=config.freeze_embedding)
if num_layers == 1:
dropout = 0.0
self.encoder_trans = nn.Linear(hidden_size, hidden_size)
self.reduce_layer = nn.Linear(
embedding_size + hidden_size, embedding_size)
self.lstm = nn.LSTM(embedding_size, hidden_size, batch_first=True,
num_layers=num_layers, bidirectional=False, dropout=dropout)
self.concat_layer = nn.Linear(2 * hidden_size, hidden_size)
self.logit_layer = nn.Linear(hidden_size, vocab_size)
@staticmethod
def attention(query, memories, mask):
# query : [b, 1, d]
energy = torch.matmul(query, memories.transpose(1, 2)) # [b, 1, t]
energy = energy.squeeze(1).masked_fill(mask == 0, value=-1e12)
attn_dist = F.softmax(energy, dim=1).unsqueeze(dim=1) # [b, 1, t]
context_vector = torch.matmul(attn_dist, memories) # [b, 1, d]
return context_vector, energy
def get_encoder_features(self, encoder_outputs):
return self.encoder_trans(encoder_outputs)
def forward(self, trg_seq, ext_src_seq, init_states, encoder_outputs, encoder_mask):
# trg_seq : [b,t]
# init_states : [2,b,d]
# encoder_outputs : [b,t,d]
# init_states : a tuple of [2, b, d]
device = trg_seq.device
batch_size, max_len = trg_seq.size()
hidden_size = encoder_outputs.size(-1)
memories = self.get_encoder_features(encoder_outputs)
logits = []
# init decoder hidden states and context vector
prev_states = init_states
prev_context = torch.zeros((batch_size, 1, hidden_size))
prev_context = prev_context.to(device)
for i in range(max_len):
y_i = trg_seq[:, i].unsqueeze(1) # [b, 1]
embedded = self.embedding(y_i) # [b, 1, d]
lstm_inputs = self.reduce_layer(
torch.cat([embedded, prev_context], 2))
output, states = self.lstm(lstm_inputs, prev_states)
# encoder-decoder attention
context, energy = self.attention(output, memories, encoder_mask)
concat_input = torch.cat((output, context), dim=2).squeeze(dim=1)
logit_input = torch.tanh(self.concat_layer(concat_input))
logit = self.logit_layer(logit_input) # [b, |V|]
# maxout pointer network
if config.use_pointer:
num_oov = max(torch.max(ext_src_seq - self.vocab_size + 1), 0)
zeros = torch.zeros((batch_size, num_oov),
device=config.device)
extended_logit = torch.cat([logit, zeros], dim=1)
out = torch.zeros_like(extended_logit) - INF
out, _ = scatter_max(energy, ext_src_seq, out=out)
out = out.masked_fill(out == -INF, 0)
logit = extended_logit + out
logit = logit.masked_fill(logit == 0, -INF)
logits.append(logit)
# update prev state and context
prev_states = states
prev_context = context
logits = torch.stack(logits, dim=1) # [b, t, |V|]
return logits
def decode(self, y, ext_x, prev_states, prev_context, encoder_features, encoder_mask):
# forward one step lstm
# y : [b]
embedded = self.embedding(y.unsqueeze(1))
lstm_inputs = self.reduce_layer(torch.cat([embedded, prev_context], 2))
output, states = self.lstm(lstm_inputs, prev_states)
context, energy = self.attention(output,
encoder_features,
encoder_mask)
concat_input = torch.cat((output, context), 2).squeeze(1)
logit_input = torch.tanh(self.concat_layer(concat_input))
logit = self.logit_layer(logit_input) # [b, |V|]
if config.use_pointer:
batch_size = y.size(0)
num_oov = max(torch.max(ext_x - self.vocab_size + 1), 0)
zeros = torch.zeros((batch_size, num_oov), device=config.device)
extended_logit = torch.cat([logit, zeros], dim=1)
out = torch.zeros_like(extended_logit) - INF
out, _ = scatter_max(energy, ext_x, out=out)
out = out.masked_fill(out == -INF, 0)
logit = extended_logit + out
logit = logit.masked_fill(logit == -INF, 0)
# forcing UNK prob 0
logit[:, UNK_ID] = -INF
return logit, states, context
class Seq2seq(nn.Module):
def __init__(self, embedding=None):
super(Seq2seq, self).__init__()
self.encoder = Encoder(embedding,
config.vocab_size,
config.embedding_size,
config.hidden_size,
config.num_layers,
config.dropout)
self.decoder = Decoder(embedding, config.vocab_size,
config.embedding_size,
2 * config.hidden_size,
config.num_layers,
config.dropout)
def forward(self, src_seq, tag_seq, ext_src_seq, trg_seq):
enc_mask = torch.sign(src_seq)
src_len = torch.sum(enc_mask, 1)
enc_outputs, enc_states = self.encoder(src_seq, src_len, tag_seq)
sos_trg = trg_seq[:, :-1].contiguous()
logits = self.decoder(sos_trg, ext_src_seq,
enc_states, enc_outputs, enc_mask)
return logits