-
Notifications
You must be signed in to change notification settings - Fork 3
/
dico_builder.py
174 lines (135 loc) · 5.97 KB
/
dico_builder.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import torch
from utils import get_nn_avg_dist
def get_candidates(emb1, emb2, params):
"""
Get best translation pairs candidates.
"""
bs = 128
all_scores = []
all_targets = []
# number of source words to consider
n_src = emb1.size(0)
if params.dico_max_rank > 0 and not params.dico_method.startswith('invsm_beta_'):
n_src = min(params.dico_max_rank, n_src)
# nearest neighbors
if params.dico_method == 'nn':
# for every source word
for i in range(0, n_src, bs):
# compute target words scores
scores = emb2.mm(emb1[i:min(n_src, i + bs)].transpose(0, 1)).transpose(0, 1)
best_scores, best_targets = scores.topk(2, dim=1, largest=True, sorted=True)
# update scores / potential targets
all_scores.append(best_scores.cpu())
all_targets.append(best_targets.cpu())
all_scores = torch.cat(all_scores, 0)
all_targets = torch.cat(all_targets, 0)
# inverted softmax
elif params.dico_method.startswith('invsm_beta_'):
beta = float(params.dico_method[len('invsm_beta_'):])
# for every target word
for i in range(0, emb2.size(0), bs):
# compute source words scores
scores = emb1.mm(emb2[i:i + bs].transpose(0, 1))
scores.mul_(beta).exp_()
scores.div_(scores.sum(0, keepdim=True).expand_as(scores))
best_scores, best_targets = scores.topk(2, dim=1, largest=True, sorted=True)
# update scores / potential targets
all_scores.append(best_scores.cpu())
all_targets.append((best_targets + i).cpu())
all_scores = torch.cat(all_scores, 1)
all_targets = torch.cat(all_targets, 1)
all_scores, best_targets = all_scores.topk(2, dim=1, largest=True, sorted=True)
all_targets = all_targets.gather(1, best_targets)
# contextual dissimilarity measure
elif params.dico_method.startswith('csls_knn_'):
knn = params.dico_method[len('csls_knn_'):]
assert knn.isdigit()
knn = int(knn)
# average distances to k nearest neighbors
average_dist1 = torch.from_numpy(get_nn_avg_dist(emb2, emb1, knn))
average_dist2 = torch.from_numpy(get_nn_avg_dist(emb1, emb2, knn))
average_dist1 = average_dist1.type_as(emb1)
average_dist2 = average_dist2.type_as(emb2)
# for every source word
for i in range(0, n_src, bs):
# compute target words scores
scores = emb2.mm(emb1[i:min(n_src, i + bs)].transpose(0, 1)).transpose(0, 1)
scores.mul_(2)
scores.sub_(average_dist1[i:min(n_src, i + bs)][:, None] + average_dist2[None, :])
best_scores, best_targets = scores.topk(2, dim=1, largest=True, sorted=True)
# update scores / potential targets
all_scores.append(best_scores.cpu())
all_targets.append(best_targets.cpu())
all_scores = torch.cat(all_scores, 0)
all_targets = torch.cat(all_targets, 0)
all_pairs = torch.cat([
torch.arange(0, all_targets.size(0)).long().unsqueeze(1),
all_targets[:, 0].unsqueeze(1)
], 1)
# sanity check
assert all_scores.size() == all_pairs.size() == (n_src, 2)
# sort pairs by score confidence
diff = all_scores[:, 0] - all_scores[:, 1]
reordered = diff.sort(0, descending=True)[1]
all_scores = all_scores[reordered]
all_pairs = all_pairs[reordered]
# max dico words rank
if params.dico_max_rank > 0:
selected = all_pairs.max(1)[0] <= params.dico_max_rank
mask = selected.unsqueeze(1).expand_as(all_scores).clone()
all_scores = all_scores.masked_select(mask).view(-1, 2)
all_pairs = all_pairs.masked_select(mask).view(-1, 2)
# max dico size
if params.dico_max_size > 0:
all_scores = all_scores[:params.dico_max_size]
all_pairs = all_pairs[:params.dico_max_size]
# min dico size
diff = all_scores[:, 0] - all_scores[:, 1]
if params.dico_min_size > 0:
diff[:params.dico_min_size] = 1e9
# confidence threshold
if params.dico_threshold > 0:
mask = diff > params.dico_threshold
print("Selected %i / %i pairs above the confidence threshold." % (mask.sum(), diff.size(0)))
mask = mask.unsqueeze(1).expand_as(all_pairs).clone()
all_pairs = all_pairs.masked_select(mask).view(-1, 2)
return all_pairs
def build_dictionary(src_emb, tgt_emb, params, s2t_candidates=None, t2s_candidates=None):
"""
Build a training dictionary given current embeddings / mapping.
"""
print("Building the train dictionary ...")
s2t = 'S2T' in params.dico_build
t2s = 'T2S' in params.dico_build
assert s2t or t2s
if s2t:
if s2t_candidates is None:
s2t_candidates = get_candidates(src_emb, tgt_emb, params)
if t2s:
if t2s_candidates is None:
t2s_candidates = get_candidates(tgt_emb, src_emb, params)
t2s_candidates = torch.cat([t2s_candidates[:, 1:], t2s_candidates[:, :1]], 1)
if params.dico_build == 'S2T':
dico = s2t_candidates
elif params.dico_build == 'T2S':
dico = t2s_candidates
else:
s2t_candidates = set([(a, b) for a, b in s2t_candidates.numpy()])
t2s_candidates = set([(a, b) for a, b in t2s_candidates.numpy()])
if params.dico_build == 'S2T|T2S':
final_pairs = s2t_candidates | t2s_candidates
else:
assert params.dico_build == 'S2T&T2S'
final_pairs = s2t_candidates & t2s_candidates
if len(final_pairs) == 0:
print("Empty intersection ...")
return None
dico = torch.LongTensor(list([[int(a), int(b)] for (a, b) in final_pairs]))
print('New train dictionary of %i pairs.' % dico.size(0))
return dico