-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
278 lines (240 loc) · 11.5 KB
/
translate.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
import torch.nn as nn
import torch.nn.functional as F
import torch.nn as nn
import torch
import sys
import random
import os
from subprocess import Popen, PIPE
from nltk.translate import bleu_score
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from util import cudaw
class Translator(object):
def __init__(self, corpus, sentence_bleu=None, valid_all=False, mode='train'):
self.corpus = corpus
self.test = corpus.test
self.ref = {"test": corpus.ref_test}
if mode == 'train':
self.valid = corpus.valid
self.ref['valid'] = corpus.ref_valid
self.train = corpus.train
self.ref['train'] = corpus.ref_train
random.seed('masaru')
if mode == 'train':
if not valid_all:
self.random_samples = random.sample(range(min(len(self.valid), len(self.test))), 10)
else:
self.random_samples = list(range(len(self.valid)))
def bleu(self, hyp, mode, nltk=None):
"""mode can be 'valid' or 'test' """
if nltk == 'sentence':
score = 0
num_hyp = 0
for (word, desc) in hyp:
bleu = bleu_score.sentence_bleu([ref.split() for ref in self.ref[mode][word]],
desc,
smoothing_function=bleu_score.SmoothingFunction().method2)
score += bleu
num_hyp += 1
return score / num_hyp
elif nltk == 'corpus':
refs = []
for (word, desc) in hyp:
refs.append([ref.split() for ref in self.ref[mode][word]])
bleu = bleu_score.corpus_bleu(refs,
[word_desc[1] for word_desc in hyp],
smoothing_function=bleu_score.SmoothingFunction().method2)
return bleu
else:
return -1
def top1_copy(self, mode="valid", ignore_duplicates=False):
if mode == "valid":
data = self.valid
elif mode == "test":
data = self.test
if ignore_duplicates:
data_new = []
words = set()
for i in range(len(data)):
word = data[i][0].split('%', 1)[0]
if word not in words:
data_new.append(data[i])
words.add(word)
data = data_new
top1_copied = [(data[i][0], data[i][4][0][1]) for i in range(len(data))]
return top1_copied
def draw_att_weights(self, att_mat_np, word, hyp, nns, path):
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.matshow(att_mat_np, cmap='bone', vmin=0, vmax=1)
fig.colorbar(cax)
# Set up axes
ax.set_xticklabels([''] + hyp, rotation=90, fontdict={'size': 8})
ax.set_yticklabels([''] + nns, fontdict={'size': 8})
# Show label at every tick
ax.xaxis.set_ticks_position('bottom')
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
ax.set_xlabel('Generated definition')
ax.set_ylabel('Retrieved definitions')
# plt.show()
plt.savefig(path + '/' + word + '.pdf', bbox_inches='tight')
return
def visualize_att_weights(self, att_weights_batches, mode, topk, results, path):
if mode == "valid":
data = self.valid
elif mode == "test":
data = self.test
# remove duplicates
data_new = []
words = set()
for i in range(len(data)):
word = data[i][0].split('%', 1)[0]
if word not in words:
data_new.append(data[i])
words.add(word)
data = data_new
i = 0
for batch in att_weights_batches:
max_src_len = batch.size(2) / topk
for att_mat in batch:
word, hyp = results[i]
hyp_buf = hyp + ['<eos>']
for j in range(len(hyp_buf)):
if hyp_buf[j] not in self.corpus.word2id:
hyp_buf[j] = '[' + hyp_buf[j] + ']'
nns = []
sliced_att_mat = []
for k in range(topk):
nn = data[i][4][k][3]
nns.append(nn)
sliced_att_mat.append(att_mat[:(len(hyp_buf)), int(max_src_len * k): int(max_src_len * k + len(nn))])
att_mat_np = torch.cat(sliced_att_mat, dim=1).permute(1, 0).cpu().data.numpy()
nns_concat = []
for nn in nns:
for w in nn:
if w not in self.corpus.word2id:
nns_concat.append('[' + w + ']')
else:
nns_concat.append(w)
self.draw_att_weights(att_mat_np, word, hyp_buf, nns_concat, path)
i += 1
return 0
def greedy(self, model, mode="valid", max_batch_size=128, cuda=True, max_len=60):
if mode == "valid":
data = self.valid
elif mode == "test":
data = self.test
elif mode == "train":
# test 1/5 of training examples,for saving time
data = self.train[:len(self.train) // 5]
model.eval()
results = []
batch_iter = self.corpus.batch_iterator(data, max_batch_size, cuda=cuda, mode=mode, seed_feeding=model.seed_feeding)
with torch.no_grad():
for i, elems in enumerate(batch_iter):
batch_size = len(elems[0])
decoded_words = [[] for x in range(batch_size)]
hidden = model.init_hidden(batch_size, model.dhid)
char_emb = None
if model.CH:
if model.use_formation:
char_emb = model.get_char_embedding(elems[1], elems[7]) # batch, d_char_emb;
else:
char_emb = model.get_char_embedding(elems[1]) # batch, d_char_emb;
input_word = torch.LongTensor([[self.corpus.word2id['<bos>']] * batch_size]) # 1, batch
if cuda:
input_word = input_word.cuda()
# Decode the first word
(word, chars, vec, src, trg, eg, eg_mask, fms, mor1, mor1_len, mor1_mask, mor2, mor2_len, mor2_mask, sm_vecs) = elems
eg_emb = model.get_nn_embedding(eg)
Wh_enc = model.attention.map_enc_states(eg_emb) # (batch, len, dim)
output, hidden = model(input_word, hidden, vec, eg_emb, eg_mask, Wh_enc, fms, mor1, mor1_len, mor1_mask, mor2, mor2_len, mor2_mask, sm_vecs, cuda, seed_feeding=model.seed_feeding, char_emb=char_emb) # 2, batch, vocab
max_ids = output[-1].max(-1)[1] # (batch); there may be two outputs if we use seed. Here we want the newest one
input_word[0] = max_ids # 1, batch
keep_decoding = [1] * batch_size # batch
for k in range(len(keep_decoding)):
word_id = max_ids[k].data.item()
if self.corpus.id2word[word_id] != '<eos>':
decoded_words[k].append(self.corpus.id2word[word_id])
else:
keep_decoding[k] = 0
# decode the subsequent words batch by batch
for j in range(max_len):
output, hidden = model(input_word, hidden, vec, eg_emb, eg_mask, Wh_enc, fms, mor1, mor1_len, mor1_mask, mor2, mor2_len, mor2_mask, sm_vecs, cuda, seed_feeding=False, char_emb=char_emb)
# map id to word
max_ids = output[0].max(-1)[1] # (batch);
for k in range(len(keep_decoding)):
word_id = max_ids[k].data.item()
if keep_decoding[k]:
if self.corpus.id2word[word_id] != '<eos>':
decoded_words[k].append(self.corpus.id2word[word_id])
else:
keep_decoding[k] = 0
else:
pass
# feedback to the next step
input_word[0] = max_ids # 1, batch
if max(keep_decoding) == 0:
break
for k in range(len(decoded_words)):
results.append((word[k], decoded_words[k]))
print('Decoded ' + mode + ' samples:')
for sample in self.random_samples:
sentence = ' '.join(results[sample][1])
print(results[sample][0] + '\t' + sentence)
return results
def eval_loss(self, model, mode="valid", max_batch_size=128, cuda=True):
if mode == 'valid':
ntoken = self.corpus.valid_ntoken
elif mode == 'test':
ntoken = self.corpus.test_ntoken
# Turn on evaluation mode which disables dropout.
model.eval()
criterion = nn.CrossEntropyLoss(reduction='sum', ignore_index=model.corpus.word2id['<pad>'])
total_loss = 0
batch_iter = self.corpus.batch_iterator(self.valid if mode == 'valid' else self.test, max_batch_size, cuda=cuda, mode='valid', seed_feeding=model.seed_feeding)
with torch.no_grad():
for i, elems in enumerate(batch_iter):
hidden = model.init_hidden(len(elems[0]), model.dhid)
char_emb = None
if model.CH:
if model.use_formation:
char_emb = model.get_char_embedding(elems[1], elems[7]) # batch, d_char_emb;
else:
char_emb = model.get_char_embedding(elems[1]) # batch, d_char_emb;
(word, chars, vec, src, trg, eg, eg_mask, fms, mor1, mor1_len, mor1_mask, mor2, mor2_len, mor2_mask, sm_vecs) = elems
eg_emb = model.get_nn_embedding(eg)
Wh_enc = model.attention.map_enc_states(eg_emb) # (topk*batch, len, dim)
output, hidden = model(src, hidden, vec, eg_emb, eg_mask, Wh_enc, fms, mor1, mor1_len, mor1_mask, mor2, mor2_len, mor2_mask, sm_vecs, cuda, seed_feeding=model.seed_feeding, char_emb=char_emb)
output_flat = output.view(output.size(0) * output.size(1), -1) # (trgLen, vocab)
total_loss += criterion(output_flat, trg).data
returns = [total_loss.item() / ntoken]
return returns
def print_log_loss(self, word, logloss, ref):
# logloss: (trgLen), ref: [w1, w2, ..., <eos>]
print(word + ':', end='')
ref_buf = []
for w in ref:
if w not in self.corpus.word2id:
ref_buf.append('[' + w + ']')
else:
ref_buf.append(w)
print('\t{:>6s}'.format(ref_buf[-1]), end='')
print('\n' + ' ' * (len(word) + 1), end='')
for j, loss in enumerate(logloss):
format = '\t{:>' + str(max(len(ref_buf[j]), 6)) + '.2f}'
print(format.format(loss), end='')
print()
def print_log_loss_matrix(self, logloss_matrix, mode):
# logloss_matrix (dataSize, trgLen)
ref = [(self.corpus.test[k][0], self.corpus.test[k][5]) for k in range(len(self.corpus.test))]
# logloss_matrix: (dataSize, trgLen)
print('\n' + '-' * 150)
for i, data in enumerate(logloss_matrix):
words = ref[i][1][1:]
logloss = data[1:len(ref[i][1])].tolist()
self.print_log_loss(ref[i][0], logloss, words)
print('-' * 150)