-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
87 lines (73 loc) · 3.39 KB
/
inference.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
import torch
import torch.nn as nn
from torch import optim
import torch.nn.functional as F
from data_prep import tensorFromSentence
def greedy_search(decoder, decoder_input, hidden, max_length):
translation = []
for i in range(max_length):
next_word_softmax, hidden = decoder(decoder_input, hidden)
best_idx = torch.max(next_word_softmax, 1)[1].squeeze().item()
# convert idx to word
best_word = target_lang.index2word[best_idx]
translation.append(best_word)
decoder_input = torch.tensor([[best_idx]], device=device)
if best_word == 'EOS':
break
return translation
def beam_search(decoder, decoder_input, hidden, max_length, k):
candidates = [(decoder_input, 0, hidden)]
potential_candidates = []
completed_translations = []
# put a cap on the length of generated sentences
for m in range(max_length):
for c in candidates:
# unpack the tuple
c_sequence = c[0]
c_score = c[1]
c_hidden = c[2]
# EOS token
if c_sequence[-1] == 1:
completed_translations.append((c_sequence, c_score))
k = k - 1
else:
next_word_probs, hidden = decoder(c_sequence[-1], c_hidden)
# in the worst-case, one sequence will have the highest k probabilities
# so to save computation, only grab the k highest_probability from each candidate sequence
top_probs, top_idx = torch.topk(next_word_probs, k)
for i in range(len(top_probs[0])):
word = torch.from_numpy(np.array([top_idx[0][i]]).reshape(1, 1)).to(device)
new_score = c_score + top_probs[0][i]
potential_candidates.append((torch.cat((c_sequence, word)).to(device), new_score, hidden))
candidates = sorted(potential_candidates, key= lambda x: x[1], reverse=True)[0:k]
potential_candidates = []
completed = completed_translations + candidates
completed = sorted(completed, key= lambda x: x[1], reverse=True)[0]
final_translation = []
for x in completed[0]:
final_translation.append(target_lang.index2word[x.squeeze().item()])
return final_translation
def generate_translation(encoder, decoder, sentence, max_length, search="greedy", k = None):
"""
@param max_length: the max # of words that the decoder can return
@returns decoded_words: a list of words in target language
"""
with torch.no_grad():
input_tensor = tensorFromSentence(input_lang, sentence)
input_length = input_tensor.size()[0]
# encode the source sentence
encoder_hidden = encoder.initHidden()
for ei in range(input_length):
encoder_output, encoder_hidden = encoder(input_tensor[ei],
encoder_hidden)
# start decoding
decoder_input = torch.tensor([[SOS_token]], device=device) # SOS
decoder_hidden = encoder_hidden
decoded_words = []
if search == 'greedy':
decoded_words = greedy_search(decoder, decoder_input, decoder_hidden, max_length)
elif search == 'beam':
if k == None:
k = 2
decoded_words = beam_search(decoder, decoder_input, decoder_hidden, max_length, k)
return decoded_words