-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
68 lines (50 loc) · 2.14 KB
/
utils.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from tqdm import tqdm
from torchtext import data
from torchtext import datasets
from torchtext.vocab import GloVe
import spacy
import numpy as np
def torchtext_extract(d=-1, MAX_LEN=20, MIN_FREQ=5, BATCH_SIZE=32):
spacy_de = spacy.load('de')
spacy_en = spacy.load('en')
def tokenize_de(text):
return [tok.text for tok in spacy_de.tokenizer(text)]
def tokenize_en(text):
return [tok.text for tok in spacy_en.tokenizer(text)]
BOS_WORD = '<s>'
EOS_WORD = '</s>'
DE = data.Field(tokenize=tokenize_de)
EN = data.Field(tokenize=tokenize_en, init_token=BOS_WORD, eos_token=EOS_WORD) # only target needs BOS/EOS
train, val, test = datasets.IWSLT.splits(exts=('.de', '.en'), fields=(DE, EN),
filter_pred=lambda x: len(vars(x)['src']) <= MAX_LEN and
len(vars(x)['trg']) <= MAX_LEN)
DE.build_vocab(train.src, min_freq=MIN_FREQ)
EN.build_vocab(train.trg, min_freq=MIN_FREQ)
train_iter, val_iter = data.BucketIterator.splits((train, val), batch_size=BATCH_SIZE, device=d,
repeat=False, sort_key=lambda x: len(x.src))
return train_iter, val_iter, test, DE, EN
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def perplexity(model, val_iter, gpu=True):
model.eval()
loss = nn.NLLLoss(ignore_index=1) # ignore <pad> TODO check that this is the right index for pad
val_loss = 0
for batch in tqdm(val_iter):
if gpu:
src, trg = batch.src.cuda(), batch.trg.cuda()
else:
src, trg = batch.src, batch.trg
ll, _ = model.forward(src, trg)
# we have to eliminate the <s> start of sentence token in the trg, otherwise it will not be aligned
nll = loss(ll[:-1, :, :].view(-1, ll.size(2)), trg[1:, :].view(-1))
val_loss += nll.data.cpu().numpy()[0]
val_loss /= len(val_iter)
model.train()
return np.exp(val_loss)
# TODO
def bleu():
pass