Implementing e_biascomponent

C5 W2 A1 ungraded Ex 3 (Neutralize) about “Neutralize Bias for Non-Gender Specific Words”
I was trying to get this formula in code to get the bias component
image
But whether I used
e_biascomponent = np.dot(e,g)/ np.linalg.norm(g) * g
or
e_biascomponent = np.dot(np.dot(e,g)/ np.sqrt(np.sum(g**2)),g)
I got a wrong output: cosine similarity after neutralization: 0.153
Should I have used this ?

The paper assumes all word vectors to have L2 norm as 1 and hence the need for this calculation

from tqdm import tqdm
word_to_vec_map_unit_vectors = {
word: embedding / np.linalg.norm(embedding)
for word, embedding in tqdm(word_to_vec_map.items())
}
g_unit = word_to_vec_map_unit_vectors[‘woman’] - word_to_vec_map_unit_vectors[‘man’]
If so, I did’t understand how. Can you please give me a hint?

The denominator there is the square of the 2-norm of g, right? You’ve just used the 2-norm.

And in the second formula you’ve done two dot products. In the math formula, there is only one dot product, right? You’re computing a factor and then multiplying it by the vector g.

Oh, sorry, your np.dot usage in the second formula is actually ok. I’d never tried that before, but if you dot a scalar with a vector it just multiplies the scalar by the vector:

v = np.ones((1,3))
print(v)
print(np.dot(2,v))
[[1. 1. 1.]]
[[2. 2. 2.]]

So it works, but that’s a confusing way to write it.

Understood, thank you.
So it’s not necessary to use the formulas given on the next cell (from the paper)? I was confused by that infomation.

בתאריך יום ו׳, 17 בנוב׳ 2023, 1:34, מאת Paul Mielke via DeepLearning.AI ‏<notifications@dlai.discoursemail.com>:

You don’t need to do anything with that section of code that creates the word map with unit vectors. Notice that they use the “unit vector” version of the map to pass as the word_to_vec_map argument to the various functions that you write.