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
|
def equalize(pair, bias_axis, word_to_vec_map): """ Debias gender specific words by following the equalize method described in t he figure above. Arguments: pair -- pair of strings of gender specific words to debias, e.g. ("actress", "actor") bias_axis -- numpy-array of shape (50,), vector corresponding to the bias ax is, e.g. gender word_to_vec_map -- dictionary mapping words to their corresponding vectors Returns e_1 -- word vector corresponding to the first word e_2 -- word vector corresponding to the second word """ (≈ 2 lines) w1, w2 = pair e_w1, e_w2 = word_to_vec_map[w1], word_to_vec_map[w2]
mu = (e_w1 + e_w2)/2
mu_B = np.dot(mu, bias_axis) / np.dot(bias_axis, bias_axis) * bias_axis mu_orth = mu - mu_B
e_w1B = np.dot(e_w1, bias_axis) / np.dot(bias_axis, bias_axis) * bias_axis e_w2B = np.dot(e_w2, bias_axis) / np.dot(bias_axis, bias_axis) * bias_axis
corrected_e_w1B = np.sqrt(np.abs(1-np.dot(mu_orth, mu_orth)))*(e_w1B-mu_B)/n p.linalg.norm(e_w1 - mu_orth - mu_B) corrected_e_w2B = np.sqrt(np.abs(1-np.dot(mu_orth, mu_orth)))*(e_w2B-mu_B)/n p.linalg.norm(e_w2 - mu_orth - mu_B)
e1 = corrected_e_w1B + mu_orth e2 = corrected_e_w2B + mu_orth return e1, e2
|
近期评论