C3_W2_Collaborative_RecSys_Assignment: Wrong data for Regularization

Hi,
I have an issue for the C3_W2_Collaborative_RecSys_Assignment
Exercise 1
In the steps it is asked to compute cofi_cost_func with and without regularization
In the function towards the end, where J is computed,
As it asks to compute J without regularization first followed by with regularization, and since it is the same function call,
I added like this
if(lambda_ == 0):
J +=
else :
J +=

Is this approach correct?

I dont get any syntax error, but the value of J that is returned WITH regularization is wrong.

I am getting 144.43, whereas expected is 28.09

Can you please help me where I am going wrong?

Regards
Ravikiran

That’s not a necessary approach.

I recommend you implement only the code for the regularized cost, and if lambda = 0, then that contribution will be zero because it is a multiplier.

That indicates your calculation is incorrect.

But then I am getting error for the previous one where we calculate WITHOUT regularization where the expected output is 13.67 which I got with the different formula?
I guess there are two different formulas for with and without.
Is that not correct?

There is only one equation for the cost.

This image is from Section 4.1 of the notebook:

The part labeled A is the unregularized cost.

The part labeled B is the regularized portion of the cost.

If the cost function is called with the lambda parameter set to 0, then the B portion will compute to 0, so the function returns only the unregularized cost.

If you don’t get the correct results for the A portion, there is an error in your code.

I am kind of little lost.
From the “hints” portion of the exercise, it showed two different equations for w/o regularization and with regularization.
As per your tip, I should still use the equation for “regularization” and since lambda will be 0, it should work for w/o regularization also?

Let me try

Ravikiran

I am not sure if I am getting the correct value after following the equation.
I am stuck at this place.
I am aware that I am not supposed to paste the code here, but please delete it after I get the fix for this.
Please let me know what I did below is correct?

{mentor edit: code removed}

With regularization, I am getting a value of 302.13, while expected is 28.09

Please help

Ravikiran

Please do not share your code on the forum. That’s not allowed by the Code of Conduct.

You can send me your code via a private message, I’ll send you instructions in a moment.

Also note that the “preformatted text” tag should be used so that your code indentation is preserved.

Thank you, yes I am kind of aware not to do. Probably I should have asked how to do private message.
Sorry and thank you

Report back for students who find this thread later:

  • Be sure that if you compute the regularized cost for all values of X and W, that you do so only once (outside of any nested for-loops).