-
Notifications
You must be signed in to change notification settings - Fork 2
/
translator.py
83 lines (59 loc) · 2.72 KB
/
translator.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
from nmt.translation import TransformerModelConfig
from nmt.data import Dataset
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import logging
logger = logging.getLogger("Translator")
import warnings
warnings.filterwarnings(action='ignore')
class Translator(object):
def __init__(self,
config: TransformerModelConfig):
self.config = config
self.model = config.load_model()
def translate(self,
sentence: str,
max_len: int = 100) -> str:
self.model.eval()
src_indexes = self.config.src_vocab.encode_and_pack(sentence.lower())
# src_indexes = [self.config.src_vocab.vocab.stoi[token] for token in src_tokens]
src_tensor = torch.LongTensor(src_indexes).unsqueeze(0).to(self.config.device)
src_mask = self.model.make_src_mask(src_tensor)
with torch.no_grad():
enc_src, self_attn = self.model.encoder.forward_w_attn(src_tensor, src_mask)
trg_indexes = [self.config.trg_vocab.bos_idx]
for i in range(max_len):
trg_tensor = torch.LongTensor(trg_indexes).unsqueeze(0).to(self.config.device)
trg_mask = self.model.make_trg_mask(trg_tensor)
with torch.no_grad():
output, attention = self.model.decoder(trg_tensor, enc_src, trg_mask, src_mask)
pred_token = output.argmax(2)[:,-1].item()
trg_indexes.append(pred_token)
if pred_token == self.config.trg_vocab.eos_idx:
break
translation = self.config.trg_vocab.decode_with_check(trg_indexes)
trg_tokens = [self.config.trg_vocab.id_to_piece(idx) for idx in trg_indexes]
return translation, trg_tokens[1:], attention, self_attn
def display_attention(self, sentence, translation_tokens, attention, n_cols=4, figure_path = 'attention_figure.png'):
# assert n_rows * n_cols == n_heads
n_heads = self.config.dec_heads
n_rows = n_heads // n_cols
if isinstance(sentence, str):
sentence = [self.config.src_vocab.id_to_piece(idx) for idx in self.config.src_vocab.encode_and_pack(sentence)]
fig = plt.figure(figsize=(16,16))
plt.axis('off')
for i in range(n_heads):
ax = fig.add_subplot(n_rows, n_cols, i+1)
_attention = attention.squeeze(0)[i].cpu().detach().numpy()
cax = ax.matshow(_attention, cmap='bone')
ax.tick_params(labelsize=12)
ax.set_xticklabels([''] + [t.lower() for t in sentence],
rotation=45)
ax.set_yticklabels([''] + translation_tokens)
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
plt.tight_layout()
fig.subplots_adjust(hspace=0.25, wspace=0.25)
plt.savefig(figure_path)