-
Notifications
You must be signed in to change notification settings - Fork 0
/
gbdd.py
36 lines (25 loc) · 902 Bytes
/
gbdd.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
import numpy as np
def get_bias_direction(equality_sets, vecs, vecs_norm, vocab, vocab_inv):
dir_vecs = []
for eq in equality_sets:
if eq[0] in vocab and eq[1] in vocab:
dir_vecs.append(vecs_norm[vocab[eq[0]]] - vecs_norm[vocab[eq[1]]])
q = np.array(dir_vecs)
u, sigma, v = np.linalg.svd(q)
v_b = v[0]
return v_b
# scores = np.dot(v_b, np.transpose(vecs_norm))
# order = np.argsort(scores)
# sorted_pairs = [(vocab_inv[i], scores[i]) for i in order]
# return sorted_pairs
def get_pis(v_b, vecs_norm):
dots = np.dot(v_b, np.transpose(vecs_norm))
dots = np.reshape(dots, (len(vecs_norm), 1))
v_b_tiled = np.tile(v_b, (len(vecs_norm), 1))
pis = np.multiply(dots, v_b_tiled)
return pis
def debias_direction_sub(v_b, vecs_norm):
return vecs_norm - v_b
def debias_direction_linear(v_b, vecs_norm):
pis = get_pis(v_b, vecs_norm)
return vecs_norm - pis