forked from lingyongyan/Neural-Machine-Translation
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheval.py
252 lines (204 loc) · 8.38 KB
/
eval.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
import argparse
import etl
import helpers
import torch
from attention_decoder import AttentionDecoderRNN
from topk_decode import TopKDecode
from encoder import EncoderRNN
from language import Language
from beam import Beam
# Parse argument for input sentence
parser = argparse.ArgumentParser()
parser.add_argument('--attn_model', type=str, help='attention type: dot, general, concat')
parser.add_argument('--embedding_size', type=int)
parser.add_argument('--hidden_size', type=int)
parser.add_argument('--n_layers', type=int)
parser.add_argument('--dropout', type=float)
parser.add_argument('--language', type=str, help='specific which language.')
parser.add_argument('--input', type=str, help='src -> tgt')
parser.add_argument('--max_len', type=int)
parser.add_argument('--beam_size', type=int)
parser.add_argument('--batch_size', type=int)
parser.add_argument('--device', type=str, help='cpu or cuda')
parser.add_argument('--seed', type=str, help='random seed')
args = parser.parse_args()
helpers.validate_language_params(args.language)
input_lang, output_lang, pairs = etl.prepare_data(args.language)
torch.random.manual_seed(args.seed)
device = torch.device(args.device)
print('input: %s' % args.input)
# Initialize models
encoder = EncoderRNN(
input_lang.n_words,
args.embedding_size,
args.hidden_size,
args.n_layers,
args.dropout
)
decoder = AttentionDecoderRNN(
output_lang.n_words,
args.embedding_size,
args.hidden_size,
args.attn_model,
args.n_layers,
args.dropout
)
# Load model parameters
encoder.load_state_dict(torch.load('./data/encoder_params_{}'.format(args.language)))
decoder.load_state_dict(torch.load('./data/decoder_params_{}'.format(args.language)))
decoder.attention.load_state_dict(torch.load('./data/attention_params_{}'.format(args.language)))
# Move models to device
encoder = encoder.to(device)
decoder = decoder.to(device)
def evaluate(sentence, max_len=10):
input = etl.tensor_from_sentence(input_lang, sentence, device)
input_length = input.size()[0]
# Run through encoder
encoder_hidden = encoder.init_hidden(device)
encoder_outputs, encoder_hidden = encoder(input, encoder_hidden)
# Create starting vectors for decoder
decoder_context = torch.zeros(1, 1, decoder.hidden_size).to(device)
decoder_hidden = encoder_hidden
topk_decoder = TopKDecode(
decoder,
decoder.hidden_size,
args.beam_size,
output_lang.n_words,
Language.sos_token,
Language.eos_token,
device
)
topk_decoder = topk_decoder.to(device)
decoder_outputs, _, metadata = topk_decoder(
decoder_context,
decoder_hidden,
encoder_outputs,
args.max_len,
args.batch_size,
)
beam_words = torch.stack(metadata['topk_sequence'], dim=0)
# print(beam_words.shape)
beam_words = beam_words.squeeze(3).squeeze(1).transpose(0, 1)
beam_length = metadata['topk_length']
print_sentence(beam_words, beam_length[0], 'beam')
"""
beam_words, _, _= beam_decode(
decoder_context,
decoder_hidden,
encoder_outputs,
max_len,
beam_size=5
)
# [batch_size, beam_size, max_len] -> [beam_size, max_len] because we
# batch_size if 1.
beam_words = beam_words[0]
# print(beam_words)
print_sentence(beam_words, 'beam')
"""
greedy_words, greedy_attention = greedy_decode(
decoder_context,
decoder_hidden,
encoder_outputs,
max_len
)
print_sentence(greedy_words)
def greedy_decode(decoder_context,
decoder_hidden,
encoder_outputs,
max_len):
# Run through decoder
decoded_words = []
decoder_attentions = torch.zeros(max_len, max_len)
decoder_input = torch.LongTensor([[Language.sos_token]]).to(device) # SOS
for di in range(max_len):
decoder_output, decoder_context, decoder_hidden, decoder_attention = decoder(decoder_input,
decoder_context,
decoder_hidden,
encoder_outputs)
decoder_attentions[di, :decoder_attention.size(2)] += decoder_attention.squeeze(0).squeeze(0).cpu().data
# Choose top word from output
topv, topi = decoder_output.data.topk(1)
ni = topi.item()
if ni == Language.eos_token:
decoded_words.append('<EOS>')
break
else:
decoded_words.append(output_lang.index2word[ni])
# Next input is chosen word
decoder_input = topi
return decoded_words, decoder_attentions[:di + 1, :encoder_outputs.size(0)]
def beam_decode(decoder_context,
decoder_hidden,
encoder_outputs,
max_len,
beam_size=5):
batch_size = args.beam_size
vocab_size = output_lang.n_words
# [1, batch_size x beam_size]
decoder_input = torch.ones(batch_size * beam_size, dtype=torch.long, device=device) * Language.sos_token
# [num_layers, batch_size x beam_size, hidden_size]
decoder_hidden = decoder_hidden.repeat(1, beam_size, 1)
decoder_context = decoder_context.repeat(1, beam_size, 1)
encoder_outputs = encoder_outputs.repeat(1, beam_size, 1)
# [batch_size] [0, beam_size * 1, ..., beam_size * (batch_size - 1)]
batch_position = torch.arange(0, batch_size, dtype=torch.long, device=device) * beam_size
score = torch.ones(batch_size * beam_size, device=device) * -float('inf')
score.index_fill_(0, torch.arange(0, batch_size, dtype=torch.long, device=device) * beam_size, 0.0)
# Initialize Beam that stores decisions for backtracking
beam = Beam(
batch_size,
beam_size,
max_len,
batch_position,
Language.eos_token
)
for i in range(max_len):
decoder_output, decoder_context, decoder_hidden, _ = decoder(decoder_input,
decoder_context,
decoder_hidden,
encoder_outputs)
# output: [1, batch_size * beam_size, vocab_size]
# -> [batch_size * beam_size, vocab_size]
log_prob = decoder_output
# score: [batch_size * beam_size, vocab_size]
score = score.view(-1, 1) + log_prob
# score [batch_size, beam_size]
score, top_k_idx = score.view(batch_size, -1).topk(beam_size, dim=1)
# decoder_input: [batch_size x beam_size]
decoder_input = (top_k_idx % vocab_size).view(-1)
# beam_idx: [batch_size, beam_size]
beam_idx = top_k_idx / vocab_size # [batch_size, beam_size]
# top_k_pointer: [batch_size * beam_size]
top_k_pointer = (beam_idx + batch_position.unsqueeze(1)).view(-1)
# [num_layers, batch_size * beam_size, hidden_size]
decoder_hidden = decoder_hidden.index_select(1, top_k_pointer)
decoder_context = decoder_context.index_select(1, top_k_pointer)
# Update sequence scores at beam
beam.update(score.clone(), top_k_pointer, decoder_input)
# Erase scores for EOS so that they are not expanded
# [batch_size, beam_size]
eos_idx = decoder_input.data.eq(Language.eos_token).view(batch_size, beam_size)
if eos_idx.nonzero().dim() > 0:
score.data.masked_fill_(eos_idx, -float('inf'))
prediction, final_score, length = beam.backtrack()
return prediction, final_score, length
def assemble_sentence(words):
final_words = list()
for word in words:
if word in ['<SOS>', '<PAD>']:
continue
elif word == '<EOS>':
break
final_words.append(word)
sentence = ' '.join(final_words)
return sentence
def print_sentence(words, lengths=None, mode='greedy'):
if mode == 'greedy':
print('greedy > %s' % assemble_sentence(words))
elif mode == 'beam':
for i, (length, ids) in enumerate(zip(lengths, words.tolist())):
cur_words = [output_lang.index2word[id] for id in ids[:length]]
sentence = assemble_sentence(cur_words)
print('beam %d > %s' % (i, sentence))
input_sentence = helpers.normalize_string(args.input)
evaluate(input_sentence, args.max_len)