Lab 1 - Collaborative Filtering

I have written the same code as yours and it is giving me an attribute error. I also wrote the regularization code to verify and that is also giving wrong output. Please guide

{ code removed by mentor }

The error message indicates that it has a problem with the variable b which happened even before it starts to test your function. Therefore, the error isn’t about your work.

The message said that the variable b couldn’t be reshaped and that’s likely before you had somehow unintentionally changed b to something that the assignment wasn’t designed to. I suggest you to click, on the menu bar of the notebook “Kernel” > “Restart”, then you run your notebook from the first cell again, and see if the reported problem is gone. If it remains, then you would need to refresh your notebook, and copy your answers back to the new notebook.

Cheers,
Raymond

My code is working without regularization now, but with regularization, it’s giving the wrong output. My code is same as your code
{ code removed by mentor }

yes. I got a 144… when I moved to next step, code is not executing

I am getting following error when I run the following…

Public tests
from public_tests import *
test_cofi_cost_func(cofi_cost_func)

AssertionError Traceback (most recent call last)
in
1 # Public tests
2 from public_tests import *
----> 3 test_cofi_cost_func(cofi_cost_func)

~/work/public_tests.py in test_cofi_cost_func(target)
14 J = target(X_r, W_r, b_r, Y_r, R_r, 2);
15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiply the regularization term by lambda_?"
—> 16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"
17
18

AssertionError: Wrong value. Expected 27, got 270.0. Check the regularization term

Hello @Syed_Hamza_Tehseen, for the “with regularization” version, you need to implement the full cost function.

image

Raymond

PS: I have removed your assignment code since it’s not allowed to share it here.

Hello @Sudhir_Nandamuru,

You need to make sure the formula is implemented correctly. Note that the regularization term is outside of the error term (pay attention to the square brackets that I have used to separate the two).

image

Therefore, you have loops for the error terms only, and after you complete calculating the error terms, you add the regularization term.

Raymond

PS: Next time, please just open a new thread for your own question, instead of writing replies in multiple threads.

1 Like

Thank you. As guided, I have made changes to the code and am still getting the same error.

Next time, I will open a different thread.

Sudhir

Raymond,

I found the problem with code… it is working now… thanks for your guidance. Sudhir

Well done, Sudhir!

Raymond

Can you elaborate please??

Sure, @Sudhir_Nandamuru.

If we read the formula:

image

we see the total cost consists of two parts:

the error part:
image

and the regularization part:
image

Now, you have passed the part for “without regularization”, and you are doing the “with regularization” part, right? Here are the two things I wanted to point out:

  1. For the “without regularization”, you only need the error part, but for the “with regularization”, you need BOTH the error part and the regularization part

  2. The error part consists of some loops, right? because from the error part’s formula, it has summation. Your work should complete the error part first, which means to complete those loops first, and come up with a value, AFTER THAT you add some code to compute the regularization part, and you come up with another value, right? And finally, you add the two values up to get the final value. Do these steps make sense given the formula that consists of two parts?

Cheers,
Raymond

I understand your point but I am having difficulty in writing the code

Hello @Syed_Hamza_Tehseen,

Sometimes, exericses are meant to be challenging, and what we can do for you is to give you some suggestions, but you will need to get through it yourself.

Let’s do a catch-up on what you have so far.

  1. You said your code can work without regularization.
  2. You have access to some hints under the exercise’s code cell
  3. You have the formula:
    image
  4. I have given you my explanation on how to approach it

So to move on, first, please go back to the version of your code that passed the “without regularization” tests (point number 1). We should start from there. Then, look at the hints (point number 2) on how the regularization term should look like, and read my explanation (point number 4) on where to put it. Note that it’s not going to be inside any loop that computes the error term, because as I said, the error term and the regularization term are separated, and you can also see that they ARE separated from the formula (point number 3).

That’s all I can share with you. The rest is on you now @Syed_Hamza_Tehseen :wink:

Cheers,
Raymond

Yes, I got that now. Thank you so much for sticking with me and helping out. Much Appreciated

It’s good to hear that you have passed it. Keep it up, @Syed_Hamza_Tehseen!

Cheers,
Raymond

1 Like