-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode_copy.py
295 lines (228 loc) · 13.4 KB
/
decode_copy.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# coding=utf-8
from itertools import chain
import torch
import torch.nn as nn
import torch.nn.utils
from torch.autograd import Variable
import torch.nn.functional as F
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
from model import nn_utils
from model.pointer_net import PointerNet
from model.seq2seq import Seq2SeqModel
class Seq2SeqWithCopy(Seq2SeqModel):
def __init__(self, src_vocab, tgt_vocab, embed_size, hidden_size,
dropout=0.,
cuda=False,
src_embed_layer=None, tgt_embed_layer=None):
super(Seq2SeqWithCopy, self).__init__(src_vocab, tgt_vocab,
embed_size, hidden_size,
dropout=dropout,
src_embed_layer=src_embed_layer, tgt_embed_layer=tgt_embed_layer,
cuda=cuda)
# pointer net to the source
self.src_pointer_net = PointerNet(src_encoding_size=hidden_size * 2,
query_vec_size=hidden_size)
self.tgt_token_predictor = nn.Linear(hidden_size, 2)
def encode(self, src_sents_var, src_sents_len):
"""
encode the source sequence
:return:
src_encodings: Variable(src_sent_len, batch_size, hidden_size * 2)
dec_init_state, dec_init_cell: Variable(batch_size, hidden_size)
"""
# (tgt_query_len, batch_size, embed_size)
src_token_embed = self.src_embed(src_sents_var)
packed_src_token_embed = pack_padded_sequence(src_token_embed, src_sents_len)
# src_encodings: (tgt_query_len, batch_size, hidden_size)
src_encodings, (last_state, last_cell) = self.encoder_lstm(packed_src_token_embed)
src_encodings, _ = pad_packed_sequence(src_encodings)
# (batch_size, query_len, hidden_size * 2)
src_encodings = src_encodings.permute(1, 0, 2)
# (batch_size, hidden_size * 2)
last_state = torch.cat([last_state[0], last_state[1]], 1)
last_cell = torch.cat([last_cell[0], last_cell[1]], 1)
return src_encodings, (last_state, last_cell)
def decode(self, src_encodings, src_sent_masks, dec_init_vec, tgt_sents_var):
new_tensor = src_encodings.data.new
batch_size = src_encodings.size(0)
h_tm1 = dec_init_vec
# (batch_size, query_len, hidden_size)
src_encodings_att_linear = self.att_src_linear(src_encodings)
# initialize the attentional vector
att_tm1 = Variable(new_tensor(batch_size, self.hidden_size).zero_(), requires_grad=False)
# (tgt_sent_len, batch_size, embed_size)
tgt_token_embed = self.tgt_embed(tgt_sents_var)
att_ves = []
# start from `<s>`, until y_{T-1}
for t, y_tm1_embed in list(enumerate(tgt_token_embed.split(split_size=1)))[:-1]:
# input feeding: concate y_tm1 and previous attentional vector
# split() keeps the first dim
y_tm1_embed = y_tm1_embed.squeeze(0)
x = torch.cat([y_tm1_embed, att_tm1], 1)
(h_t, cell_t), att_t = self.step(x, h_tm1,
src_encodings, src_encodings_att_linear,
src_sent_masks=src_sent_masks)
att_ves.append(att_t)
att_tm1 = att_t
h_tm1 = (h_t, cell_t)
# (src_sent_len, batch_size, tgt_vocab_size)
att_ves = torch.stack(att_ves)
return att_ves
def forward(self, src_sents_var, src_sents_len, tgt_sents_var, tgt_token_copy_idx_mask, tgt_token_gen_mask):
"""
compute log p(y|x)
:param tgt_token_copy_idx_mask: Variable(tgt_action_len, batch_size, src_seq_len)
:return: Variable(batch_size)
"""
src_encodings, (last_state, last_cell) = self.encode(src_sents_var, src_sents_len)
dec_init_vec = self.init_decoder_state(last_state, last_cell)
# (batch_size, src_sent_len)
src_sent_masks = nn_utils.length_array_to_mask_tensor(src_sents_len, cuda=self.cuda)
# (tgt_sent_len - 1, batch_size, hidden_size)
att_vecs = self.decode(src_encodings, src_sent_masks, dec_init_vec, tgt_sents_var)
# (tgt_sent_len - 1, batch_size, 2)
tgt_token_predictor = F.softmax(self.tgt_token_predictor(att_vecs), dim=-1)
# (tgt_sent_len - 1, batch_size, tgt_vocab_size)
token_gen_prob = F.softmax(self.readout(att_vecs), dim=-1)
# (tgt_sent_len - 1, batch_size, src_sent_len)
token_copy_prob = self.src_pointer_net(src_encodings, src_sent_masks, att_vecs)
tgt_token_idx = tgt_sents_var[1:] # remove leading <s>
tgt_token_gen_mask = tgt_token_gen_mask[1:]
tgt_token_copy_idx_mask = tgt_token_copy_idx_mask[1:]
# (tgt_sent_len - 1, batch_size)
tgt_token_gen_prob = torch.gather(token_gen_prob, dim=2,
index=tgt_token_idx.unsqueeze(2)).squeeze(2) * tgt_token_gen_mask
# (tgt_sent_len - 1, batch_size)
tgt_token_copy_prob = torch.sum(token_copy_prob * tgt_token_copy_idx_mask, dim=-1)
# tgt_token_copy_prob = torch.gather(token_copy_prob, dim=2, index=tgt_token_copy_pos.unsqueeze(2)).squeeze(2) * tgt_token_copy_mask
tgt_token_mask = torch.gt(tgt_token_gen_mask + tgt_token_copy_idx_mask.sum(dim=-1), 0.).float()
tgt_token_prob = torch.log(tgt_token_predictor[:, :, 0] * tgt_token_gen_prob +
tgt_token_predictor[:, :, 1] * tgt_token_copy_prob +
1.e-7 * (1. - tgt_token_mask))
tgt_token_prob = tgt_token_prob * tgt_token_mask
# (batch_size)
scores = tgt_token_prob.sum(dim=0)
return scores
def step(self, x, h_tm1, src_encodings, src_encodings_att_linear, src_sent_masks=None):
"""
a single LSTM decoding step
"""
# h_t: (batch_size, hidden_size)
h_t, cell_t = self.decoder_lstm(x, h_tm1)
ctx_t, alpha_t = nn_utils.dot_prod_attention(h_t,
src_encodings, src_encodings_att_linear,
mask=src_sent_masks)
att_t = torch.tanh(self.att_vec_linear(torch.cat([h_t, ctx_t], 1))) # E.q. (5)
att_t = self.dropout(att_t)
return (h_t, cell_t), att_t
def sample(self, src_sent, sample_size, decode_max_time_step, cuda=False, mode='sample'):
new_float_tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
new_long_tensor = torch.cuda.LongTensor if cuda else torch.LongTensor
src_sent_var = nn_utils.to_input_variable([src_sent], self.src_vocab,
cuda=cuda, training=False)
# analyze which tokens can be copied from the source
src_token_tgt_vocab_ids = [self.tgt_vocab[token] for token in src_sent]
src_unk_pos_list = [pos for pos, token_id in enumerate(src_token_tgt_vocab_ids) if
token_id == self.tgt_vocab.unk_id]
# sometimes a word may appear multi-times in the source, in this case,
# we just copy its first appearing position. Therefore we mask the words
# appearing second and onwards to -1
token_set = set()
for i, tid in enumerate(src_token_tgt_vocab_ids):
if tid in token_set:
src_token_tgt_vocab_ids[i] = -1
else:
token_set.add(tid)
src_encodings, (last_state, last_cell) = self.encode(src_sent_var, [len(src_sent)])
h_tm1 = self.init_decoder_state(last_state, last_cell)
# (batch_size, 1, hidden_size)
src_encodings_att_linear = self.att_src_linear(src_encodings)
t = 0
eos_id = self.tgt_vocab['</s>']
completed_hypotheses = []
completed_hypothesis_scores = []
if mode == 'beam_search':
hypotheses = [['<s>']]
hypotheses_word_ids = [[self.tgt_vocab['<s>']]]
else:
hypotheses = [['<s>'] for _ in range(sample_size)]
hypotheses_word_ids = [[self.tgt_vocab['<s>']] for _ in range(sample_size)]
att_tm1 = Variable(new_float_tensor(len(hypotheses), self.hidden_size).zero_(), volatile=True)
hyp_scores = Variable(new_float_tensor(len(hypotheses)).zero_(), volatile=True)
while len(completed_hypotheses) < sample_size and t < decode_max_time_step:
t += 1
hyp_num = len(hypotheses)
expanded_src_encodings = src_encodings.expand(hyp_num, src_encodings.size(1), src_encodings.size(2))
expanded_src_encodings_att_linear = src_encodings_att_linear.expand(hyp_num,
src_encodings_att_linear.size(1),
src_encodings_att_linear.size(2))
y_tm1 = Variable(new_long_tensor([hyp[-1] for hyp in hypotheses_word_ids]), volatile=True)
y_tm1_embed = self.tgt_embed(y_tm1)
x = torch.cat([y_tm1_embed, att_tm1], 1)
(h_t, cell_t), att_t = self.step(x, h_tm1,
expanded_src_encodings, expanded_src_encodings_att_linear)
# (batch_size, 2)
tgt_token_predictor = F.softmax(self.tgt_token_predictor(att_t), dim=-1)
# (batch_size, tgt_vocab_size)
token_gen_prob = F.softmax(self.readout(att_t), dim=-1)
# (batch_size, src_sent_len)
token_copy_prob = self.src_pointer_net(src_encodings, src_token_mask=None, query_vec=att_t.unsqueeze(0)).squeeze(0)
# (batch_size, tgt_vocab_size)
token_gen_prob = tgt_token_predictor[:, 0].unsqueeze(1) * token_gen_prob
for token_pos, token_vocab_id in enumerate(src_token_tgt_vocab_ids):
if token_vocab_id != -1 and token_vocab_id != self.tgt_vocab.unk_id:
p_copy = tgt_token_predictor[:, 1] * token_copy_prob[:, token_pos]
token_gen_prob[:, token_vocab_id] = token_gen_prob[:, token_vocab_id] + p_copy
# second, add the probability of copying the most probable unk word
gentoken_new_hyp_unks = []
if src_unk_pos_list:
for hyp_id in range(hyp_num):
unk_pos = token_copy_prob[hyp_id][src_unk_pos_list].data.cpu().numpy().argmax()
unk_pos = src_unk_pos_list[unk_pos]
token = src_sent[unk_pos]
gentoken_new_hyp_unks.append(token)
unk_copy_score = tgt_token_predictor[hyp_id, 1] * token_copy_prob[hyp_id, unk_pos]
token_gen_prob[hyp_id, self.tgt_vocab.unk_id] = unk_copy_score
live_hyp_num = sample_size - len(completed_hypotheses)
if mode == 'beam_search':
log_token_gen_prob = torch.log(token_gen_prob)
new_hyp_scores = (hyp_scores.unsqueeze(1).expand_as(token_gen_prob) + log_token_gen_prob).view(-1)
top_new_hyp_scores, top_new_hyp_pos = torch.topk(new_hyp_scores, k=live_hyp_num)
prev_hyp_ids = (top_new_hyp_pos / len(self.tgt_vocab)).cpu().data
word_ids = (top_new_hyp_pos % len(self.tgt_vocab)).cpu().data
top_new_hyp_scores = top_new_hyp_scores.cpu().data
else:
word_ids = torch.multinomial(token_gen_prob, num_samples=1)
prev_hyp_ids = range(live_hyp_num)
top_new_hyp_scores = hyp_scores + torch.log(torch.gather(token_gen_prob, dim=1, index=word_ids)).squeeze(1)
top_new_hyp_scores = top_new_hyp_scores.cpu().data
word_ids = word_ids.view(-1).cpu().data
new_hypotheses = []
new_hypotheses_word_ids = []
live_hyp_ids = []
new_hyp_scores = []
for prev_hyp_id, word_id, new_hyp_score in zip(prev_hyp_ids, word_ids, top_new_hyp_scores):
if word_id == eos_id:
hyp_tgt_words = hypotheses[prev_hyp_id][1:]
completed_hypotheses.append(hyp_tgt_words) # remove <s> and </s> in completed hypothesis
completed_hypothesis_scores.append(new_hyp_score)
else:
if word_id == self.tgt_vocab.unk_id:
if gentoken_new_hyp_unks: word = gentoken_new_hyp_unks[prev_hyp_id]
else: word = self.tgt_vocab.id2word[self.tgt_vocab.unk_id]
else:
word = self.tgt_vocab.id2word[word_id]
hyp_tgt_words = hypotheses[prev_hyp_id] + [word]
new_hypotheses.append(hyp_tgt_words)
new_hypotheses_word_ids.append(hypotheses_word_ids[prev_hyp_id] + [word_id])
live_hyp_ids.append(prev_hyp_id)
new_hyp_scores.append(new_hyp_score)
if len(completed_hypotheses) == sample_size:
break
live_hyp_ids = new_long_tensor(live_hyp_ids)
h_tm1 = (h_t[live_hyp_ids], cell_t[live_hyp_ids])
att_tm1 = att_t[live_hyp_ids]
hyp_scores = Variable(new_float_tensor(new_hyp_scores), volatile=True) # new_hyp_scores[live_hyp_ids]
hypotheses = new_hypotheses
hypotheses_word_ids = new_hypotheses_word_ids
return completed_hypotheses