Word to vec complete_analogy

Kindly help, i am getting the following error;

KeyError Traceback (most recent call last)
in
33 print("\033[92mAll tests passed")
34
—> 35 complete_analogy_test(complete_analogy)

in complete_analogy_test(target)
26 word_to_vec_map[key] = np.array(word_to_vec_map[key])
27
—> 28 assert(target(‘a’, ‘a_nw’, ‘c’, word_to_vec_map) == ‘c_nw’)
29 assert(target(‘a’, ‘a_s’, ‘c’, word_to_vec_map) == ‘c_s’)
30 assert(target(‘a’, ‘synonym_of_a’, ‘c’, word_to_vec_map) != ‘c’), “Best word cannot be input query”

in complete_analogy(word_a, word_b, word_c, word_to_vec_map)
21 ### START CODE HERE ###
22 # Get the word embeddings e_a, e_b and e_c (≈1-3 lines)
—> 23 e_a, e_b, e_c = word_to_vec_map[“a”], word_to_vec_map[“b”], word_to_vec_map[“c”]
24 ### END CODE HERE ###
25

KeyError: ‘b’

1 Like

Hi @Igba

In you call to word_to_vec_map, you are passing hard coded value to the function. It should be the input parameters to the function complete_analogy(), ‘word_a’, ‘word_b’ and ‘word_c’.

Many thanks sir. this really helped. however , on the cosine-similarity i did; cosine_sim = cosine_similarity(e_b - e_a, w - e_c) but i have the error below;

—> 39 cosine_sim = cosine_similarity(e_b-e_a, e_w-e_c)
40
41 # If the cosine_sim is more than the max_cosine_sim seen so far,

TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’

Hi @Igba

What is your e_w ? It should be word_to_vec_map[w], ie the representation of w in the vector map.

1 Like

hi Kic,
the problem still there while i use cosine_sim = cosine_similarity(e_b-e_a, word_to_vec_map[w]-e_c)

could you pls check why?

TypeError Traceback (most recent call last)
in
33 print("\033[92mAll tests passed")
34
—> 35 complete_analogy_test(complete_analogy)

in complete_analogy_test(target)
26 word_to_vec_map[key] = np.array(word_to_vec_map[key])
27
—> 28 assert(target(‘a’, ‘a_nw’, ‘c’, word_to_vec_map) == ‘c_nw’)
29 assert(target(‘a’, ‘a_s’, ‘c’, word_to_vec_map) == ‘c_s’)
30 assert(target(‘a’, ‘synonym_of_a’, ‘c’, word_to_vec_map) != ‘c’), “Best word cannot be input query”

in complete_analogy(word_a, word_b, word_c, word_to_vec_map)
39 ### START CODE HERE ###
40 # Compute cosine similarity between the vector (e_b - e_a) and the vector ((w’s vector representation) - e_c) (≈1 line)
—> 41 cosine_sim = cosine_similarity(e_b-e_a, word_to_vec_map[w]-e_c)
42
43 # If the cosine_sim is more than the max_cosine_sim seen so far,

TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’

Hi @Lucy_Hui

cosine_similarity() is defined to have 2 input vectors, but your code didn’t reflect that. To make it into vector, just add a pair of bracket to each of the parameters. Here is what it would look like:
cosine_similarity((e_b-e_a), (word_to_vec_map[w]-e_c))

Dear mentor @Kic ,
I have done it like your support but my test already can’t run

After that, I turn it into np.subtract but it still cannot run, what should I do now?

Thanks

Hi @Dung_Dung ,

How do you obtain the embedding e_b, e_a, and e_c ?

1 Like

Dear mentor @Kic ,
I have recognized my problem and fixed it complete. Thanks a lot!
Dũng

Hi @Dung_Dung ,

Great to hear you have resolved the problem.