forked from zihangdai/ERAC-VAML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_adequacy_2.py
74 lines (58 loc) · 2.15 KB
/
test_adequacy_2.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
import os
import torch
import torch.nn as nn
from xlm.utils import AttrDict
from xlm.data.dictionary import Dictionary, BOS_WORD, EOS_WORD, PAD_WORD, UNK_WORD, MASK_WORD
from xlm.model.transformer import TransformerModel
import fastBPE
import time
# Reload a pre-trained model
model_path = 'mlm_17_1280.pth'
reloaded = torch.load(model_path)
params = AttrDict(reloaded['params'])
print("Supported languages: %s" % ", ".join(params.lang2id.keys()))
# Build dictionary
dico = Dictionary(reloaded['dico_id2word'], reloaded['dico_word2id'], reloaded['dico_counts'])
params.n_words = len(dico)
params.bos_index = dico.index(BOS_WORD)
params.eos_index = dico.index(EOS_WORD)
params.pad_index = dico.index(PAD_WORD)
params.unk_index = dico.index(UNK_WORD)
params.mask_index = dico.index(MASK_WORD)
# Build model
model = TransformerModel(params, dico, True, True)
model.eval()
model.load_state_dict(reloaded['model'])
sentences = ['Table', 'Katze']
codes_path = 'codes_xnli_17'
vocab_path = 'vocab_xnli_17'
start = time.time()
bpe = fastBPE.fastBPE(codes_path, vocab_path)
sentences = bpe.apply(sentences)
# # bpe-ize sentences
# sentences = to_bpe(sentences)
print('\n\n'.join(sentences))
# check how many tokens are OOV
n_w = len([w for w in ' '.join(sentences).split()])
n_oov = len([w for w in ' '.join(sentences).split() if w not in dico.word2id])
print('Number of out-of-vocab words: %s/%s' % (n_oov, n_w))
# add </s> sentence delimiters
sentences = [(('</s> %s </s>' % sent.strip()).split()) for sent in sentences]
bs = len(sentences)
slen = max([len(sent) for sent in sentences])
word_ids = torch.LongTensor(slen, bs).fill_(params.pad_index)
for i in range(len(sentences)):
sent = torch.LongTensor([dico.index(w) for w in sentences[i]])
word_ids[:len(sent), i] = sent
lengths = torch.LongTensor([len(sent) for sent in sentences])
langs = None
tensor = model('fwd', x=word_ids, lengths=lengths, langs=langs, causal=False).contiguous()
print(tensor.size())
print(tensor[0].size())
embeddings = tensor[0]
en_tensor = embeddings[0].unsqueeze(0)
de_tensor = embeddings[1].unsqueeze(0)
cos = nn.CosineSimilarity()
sim = cos(en_tensor, de_tensor)
print(sim)
print(time.time() - start)