Unit test that I think is not logically consistent in homework

Homework 1 week 2

print("cosine_similarity(france - paris, rome - italy) = ",cosine_similarity(france - paris, rome - italy))
# END SKIP FOR GRADING

# PUBLIC TESTS
def cosine_similarity_test(target):
    a = np.random.uniform(-10, 10, 10)
    b = np.random.uniform(-10, 10, 10)
    c = np.random.uniform(-1, 1, 23)
        
    assert np.isclose(cosine_similarity(a, a), 1), "cosine_similarity(a, a) must be 1"
    assert np.isclose(cosine_similarity((c >= 0) * 1, (c < 0) * 1), 0), "cosine_similarity(a, not(a)) must be 0"
    assert np.isclose(cosine_similarity(a, -a), -1), "cosine_similarity(a, -a) must be -1"
    assert np.isclose(cosine_similarity(a, b), cosine_similarity(a * 2, b * 4)), "cosine_similarity must be scale-independent. You must divide by the product of the norms of each input"

    print("\033[92mAll test passed!")
    
cosine_similarity_test(cosine_similarity)
cosine_similarity(father, mother) =  None
cosine_similarity(ball, crocodile) =  None
cosine_similarity(france - paris, rome - italy) =  None
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-4b2d02f84f9c> in <module>
     27     print("\033[92mAll test passed!")
     28 
---> 29 cosine_similarity_test(cosine_similarity)

<ipython-input-31-4b2d02f84f9c> in cosine_similarity_test(target)
     21 
     22     assert np.isclose(cosine_similarity(a, a), 1), "cosine_similarity(a, a) must be 1"
---> 23     assert np.isclose(cosine_similarity((c >= 0) * 1, (c < 0) * 1), 0), "cosine_similarity(a, not(a)) must be 0"
     24     assert np.isclose(cosine_similarity(a, -a), -1), "cosine_similarity(a, -a) must be -1"
     25     assert np.isclose(cosine_similarity(a, b), cosine_similarity(a * 2, b * 4)), "cosine_similarity must be scale-independent. You must divide by the product of the norms of each input"

<__array_function__ internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2255     y = array(y, dtype=dt, copy=False, subok=True)
   2256 
-> 2257     xfin = isfinite(x)
   2258     yfin = isfinite(y)
   2259     if all(xfin) and all(yfin):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In particular I would like to draw your attention to this line

23     assert np.isclose(cosine_similarity((c >= 0) * 1, (c < 0) * 1), 0), "cosine_similarity(a, not(a)) must be 0"

There is no particular reason that the cosine error needs to be non-zero. Two vectors can be perpendicular, in other words, have a dot product of zero. That is completely possible. It’s unfortunate for optimization purposes because it provides poor feedback. A direction in which to progress probably needs to be chosen at random or something. But it is possible.

Letting you know because it is not letting me pass this unit test, and because this unit test lacks in a logical basis.

Steven

That assertion is not checking that the cosine error is non-zero. It’s checking that if you compute the cosine similarity of these two vectors:

(c >= 0) * 1
(c < 0) * 1

Then the result is close to zero, meaning that those two vectors are close to being perpendicular. Note that they will both be vectors of 0 and 1, but they are the complement of each other. So the dot product should be pretty close to zero, no?

I did not have any trouble passing this test. But note that the error that is being thrown is about the type of your answer in that case. It hasn’t gotten as far as looking at the actual value.

Notice from the visible test in the notebook that you seem to just be returning None for the return value. That will not end well. :nerd_face:

Although on second thought, it’s a bit puzzling in that case that the previous test would not also have thrown a similar error. <Update: they gave you some code in the template that handles the case of equal inputs separately.>

Here’s my output for that test cell:

cosine_similarity(father, mother) =  0.8909038442893615
cosine_similarity(ball, crocodile) =  0.2743924626137942
cosine_similarity(france - paris, rome - italy) =  -0.6751479308174202
All test passed!

I passed it. There was a line I didn’t see that I thought was already implemented for me that I just had to fill in. I passed both homework’s idk 8 hours ago or something quite a bit before the deadline in time to do some teaching Sunday evening. I got 100% on each. Thank you, all it was was that I thought the last step of the cosine similarity was already defined, but I had to enter it. I knew the formula, and it was no big deal. Thank you.

1 Like