Collaborative_filtering_C3_W2

Hi Team,

I’m trying this code below for with and with out regularization and the result for with out regularization is not right. Please advise on my mistake:


if lambda_ == 0:
    for j in range(nu):
        w = W[j,:]
        b_j = b[0,j]
        for i in range(nm):
            x = X[i,:]
            y = Y[i,j]
            r = R[i,j]
            J += np.square(r * (np.dot(w,x) + b_j - y ) )
    J = J/2
else:
    J += (lambda_/2) * (np.sum(np.square(W)) + np.sum(np.square(X)))

### END CODE HERE ### 

return J

Thank you,
Sri

Hi @Sri_Alekhya,

It is important to pay attention to two things when refactoring code.

Firstly, the regularization term does not require an if-else condition. If the reg_term is equal to zero, then the corresponding term will be 0 (multiplied by 0). You can view the cost function in greater detail here:

Secondly, to ensure the correctness of the previous equation, you must add the regularization term in the following line of code.

J = J/2 + (lambda_/2) * (np.sum(np.square(W)) + np.sum(np.square(X)))

I hope you find this answer useful.

Cheers,

2 Likes

Thank you so much!
Yes, my bad, I forgot to add the J with out regularization to with regularization J. Solved it.

Thanks again,
Sri

1 Like