C5W2A1 - exercise 2 cosine_similarity gives the same result for 2 different inputs

Hi,

I wrote a cosine_simularity function that passed the test. But in Exercise 2 I get a assertion error, because the test gave the same result for:

Word a: "a", vector: [3 3]
Word b: "a_s", vector: [3 2]
Word c: "c", vector: [-2  1]
Word w: "c_se", vector: [-1  0]

Cosine_simularity result: 1.0

And the supposed solution

Word a: "a", vector: [3 3]
Word b: "a_s", vector: [3 2]
Word c: "c", vector: [-2  1]
Word w: "c_s", vector: [-2  0]

Cosine_simularity result: 1

Because the option “c_se” comes first, the solution “c_s” is disgarded.

Can you help me in where I go wrong?

Thanks, Steven

The key “c_se” is unexpected, and the key “c_s” is not present, so it fails the test.

Hi TMosh,

I do not understand what you mean. the Public test cell is:

PUBLIC TEST

def complete_analogy_test(target):
a = [3, 3] # Center at a
a_nw = [2, 4] # North-West oriented vector from a
a_s = [3, 2] # South oriented vector from a

c = [-2, 1] # Center at c
# Create a controlled word to vec map
word_to_vec_map = {'a': a,
                   'synonym_of_a': a,
                   'a_nw': a_nw, 
                   'a_s': a_s, 
                   'c': c, 
                   'c_n': [-2, 2], # N
                   'c_ne': [-1, 2], # NE
                   'c_e': [-1, 1], # E
                   'c_se': [-1, 0], # SE
                   'c_s': [-2, 0], # S
                   'c_sw': [-3, 0], # SW
                   'c_w': [-3, 1], # W
                   'c_nw': [-3, 2] # NW
                  }

both “c_se” and “c_s” are defined.

What do you mean with “unexpected” and “not present” ?

I was comparing the information you posted:

Hi TMosh,

When I change the value of “c_se” in the word_to_vec_map in the PUBLIC TEST cell from [-1, 0] to [-1, 0.0001] I get an “All tests passed”.

Also the next cell “START SKIP FOR GRADING“ works well and after submitting the test I get the full score.

I understand the theory, so I leave it for now. Thank you for your reaction.

Steven.

Note that one of these is a float, and the other is an integer.

There must be something different in how you wrote the code, because you picked a different “best analogy” for one of the word combinations. You picked another vector that points in the same direction, but is shorter. So how could that happen? There must be some subtle difference in your code. E.g. maybe you used >= instead of > for the comparison of the new value to the existing max?

Or maybe you removed this code that was given to you in the template for cosine_similarity:

    # Special case. Consider the case u = [0, 0], v=[0, 0]
    if np.all(u == v):
        return 1

I instrumented my code for complete_analogy and here’s my output from the test:

input c best analog c_nw similarity 1
input c best analog c_s similarity 1
input c best analog a similarity 0
input a best analog c similarity 1
All tests passed

Go back and examine the code for cosine_similarity and ask yourself: how can the return value end up as an integer value? There’s no way that will happen if you go through the actual computations involving norms, right?

Hi Paul and TMosh, I understand now what I did wrong:

I compared the combination of the three words “a“, “a_s“ and “c“ (resulting in [-2 0]) with both “c_se“ and “c_s“.

Thank you for your persistence in helping me getting it right!

Steven.

2 Likes