-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_load_attn.py
65 lines (44 loc) · 1.89 KB
/
save_load_attn.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
import torch
from torchtext.data.metrics import bleu_score
import spacy
def save_checkpoint(state, filename='my_checkpoint.pth.tar'):
print('=> Saving checkpoint...')
torch.save(state, filename)
def load_checkpoint(checkpoint,model,optimizer):
print("=> Loading checkpoint...")
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
def translate_sentence(model, sentence, german,english, device, max_length=50):
spacy_ger = spacy.load('de')
if type(sentence) == str:
tokens = [token.text.lower() for token in spacy_ger(sentence)]
else:
tokens = [token.lower() for token in sentence]
tokens.insert(0, german.init_token)
tokens.append(german.eos_token)
text_to_idx = [german.vocab.stoi[token] for token in tokens]
sentence_tensor = torch.LongTensor(text_to_idx).unsqueeze(1).to(device)
with torch.no_grad():
encoder_states, hidden, cell = model.encoder(sentence_tensor)
outputs = [english.vocab.stoi["<sos>"]]
for i in range(max_length):
previous_word = torch.LongTensor([outputs[-1]]).to(device)
with torch.no_grad():
output, hidden, cell = model.decoder(previous_word, encoder_states,hidden,cell)
best_guess = output.argmax(1).item()
outputs.append(best_guess)
if output.argmax(1).item() == english.vocab.stoi["<eos>"]:
break
translated_sen = [english.vocab.itos[idx] for idx in outputs]
return translated_sen[1:]
def bleu(data, model, german, english, device):
targets= []
outputs= []
for eg in data:
src = vars(eg)['src']
trg = vars(eg)['trg']
prediction = translate_sentence(model, src, german, english, device)
prediction = prediction[:-1] # eos removed
targets.append([trg])
outputs.append(prediction)
return bleu_score(outputs, targets)