C3_W2_Collaborative_RecSys_Assignment: Assertion error in cofi_cost_func


In the first test to calculate the cost function, I followed the steps and get the same output as expected for code cells with and without the regularization loss. So I assume my function is behaving correctly, but it still fails the test with an assertion error.

I checked the forum for a solution and found that this could be an issue due to an old version of the notebook. I updated my notebook to the latest version as instructed. However, the error still persists.

Attaching the screenshot of the error. I ran the cells in order (with and without regularization) to get the output. I also checked my solution with the vectorized implementation solution in the subsequent cell. The second screenshot shows the similar output from my solution and the vectorized implementation.

Perhaps I am missing something really obvious here? Please help me in resolving this.

Hello @Tushar_Anand,

From the error message, your function is returning a 0.0 for the first case of the public tests. You may actually checkout the public tests by click “File” > “Open”, then click to open the “public_tests.py” file to see that the first test case is the one that expects for 27 as the answer.

Then you can see that 0.0 is obviously not very reasonable, so you will need to check your code more carefully, because the bug you are facing can pass 2 tests.


Hi @rmwkwok ,

Thank you for your response.

The test passes a np.zeroes array as the value for R_r. I have added a condition to calculate the loss only if R_r[i,j] equals 1 because the loss is only to be computed if the movie is rated by the user. In this case, since all R_r values are 0, the function passes through without calculating the loss. The exercise instructions also say that loss is only to be computed if R_r[i,j] =1.

I’m guessing I’m misinterpreting the concept here a bit. If R_r is an array of zeroes, does it not mean that none of the movies were rated by the users? And the loss in this case should technically be 0?

Perhaps you have not taken the regularization term into account?

This solved my problem. I was misinterpreting the regularization term. It was only being added when the loss was computed, and was 0 otherwise.

Thank you for your help. I was going crazy trying to figure it out!


1 Like