-
Notifications
You must be signed in to change notification settings - Fork 0
/
sapphire_using_bert.py
137 lines (109 loc) · 4.62 KB
/
sapphire_using_bert.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import numpy as np
import torch
from transformers import BertModel, BertTokenizer
from .word_alignment import WordAlign, WordEmbedding, get_similarity_matrix
from .phrase_alignment import PhraseExtract, PhraseAlign
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class BertEmbedding(WordEmbedding):
def __init__(self, model_name):
super().__init__()
self.tokenizer = BertTokenizer.from_pretrained(model_name)
self.model = BertModel.from_pretrained(model_name)
self.model.to(device)
def __call__(self, words):
return self.vectorize(words)
@staticmethod
def _to_word_vectors(words, subwords, subword_vectors):
tokenized = []
word_vectors = []
i = 0
for subword, vector in zip(subwords, subword_vectors):
if '#' not in subword:
tokenized.append(subword)
word_vectors.append(vector)
else:
tokenized[i - 1] += subword.replace('#', '')
word_vectors[i - 1] += vector
if len(words) == len(word_vectors):
return np.array(word_vectors)
_words = [w.lower() for w in words]
tmp = []
j = 0
for i, token in enumerate(tokenized):
if j >= len(_words):
break
if token == _words[j]:
tmp.append(word_vectors[i])
j += 1
else:
for k in range(1, len(tokenized) - i + 1):
cand = ''.join(tokenized[i:i + k + 1])
if cand == _words[j]:
new_vectors = np.array(word_vectors[i:i + k + 1])
new_vector = np.mean(new_vectors, axis=0)
tmp.append(new_vector)
j += 1
break
return np.array(tmp)
def vectorize(self, words):
self.model.eval()
text = '[CLS] ' + ' '.join(words) + ' [SEP]'
tokenized = self.tokenizer.tokenize(text)
index_tokens = self.tokenizer.convert_tokens_to_ids(tokenized)
tokens_tensor = torch.tensor([index_tokens]).to(device)
with torch.no_grad():
encoded_layers, _ = self.model(tokens_tensor)
encoded_layers = encoded_layers[0][1:-1].detach().cpu().numpy()
vectors = self._to_word_vectors(words, tokenized[1:-1],
np.array(encoded_layers))
return vectors
class SapphireBert(object):
def __init__(self, model_name='bert-base-uncased'):
self.vectorizer = BertEmbedding(model_name)
self.lambda_ = 0.6
self.delta = 0.6
self.alpha = 0.01
self.use_hungarian = False
self.prune_k = -1
self.get_score = False
self.epsilon = None
self.word_aligner = WordAlign(self.lambda_, self.use_hungarian)
self.extractor = PhraseExtract(self.delta, self.alpha)
self.phrase_aligner = PhraseAlign(self.prune_k,
self.get_score,
self.epsilon)
def __call__(self, tokens_src, tokens_trg):
return self.align(tokens_src, tokens_trg)
def set_params(self, lambda_=0.6, delta=0.6, alpha=0.01, hungarian=False,
prune_k=-1, get_score=False, epsilon=None):
self.lambda_ = lambda_
self.delta = delta
self.alpha = alpha
self.use_hungarian = hungarian
self.prune_k = prune_k
self.get_score = get_score
self.epsilon = epsilon
self.word_aligner.set_params(self.lambda_, self.use_hungarian)
self.extractor.set_params(self.delta, self.alpha)
self.phrase_aligner.set_params(self.prune_k,
self.get_score,
self.epsilon)
def align(self, tokens_src: list, tokens_trg: list):
try:
len_src = len(tokens_src)
len_trg = len(tokens_trg)
vectors_src = self.vectorizer(tokens_src)
vectors_trg = self.vectorizer(tokens_trg)
sim_matrix = get_similarity_matrix(vectors_src, vectors_trg)
word_alignment = self.word_aligner(sim_matrix)
phrase_pairs = self.extractor(
word_alignment, vectors_src, vectors_trg)
phrase_alignment = self.phrase_aligner(
phrase_pairs, len_src, len_trg)
except ValueError:
word_alignment = []
if self.get_score:
phrase_alignment = ([], 0)
else:
phrase_alignment = []
return word_alignment, phrase_alignment