Week2 Practice Lab C3_W2_Collaborative_RecSys_Assignment Exercise 1

C3_W2_Collaborative_RecSys_Assignment.ipynb

When I run Public test, the following error generated but no clue.

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 0.0. Check the regularization term

The code for cofi_cost_func is as follows:

GRADED FUNCTION: cofi_cost_func

UNQ_C1

def cofi_cost_func(X, W, b, Y, R, lambda_):
“”"
Returns the cost for the content-based filtering
Args:
X (ndarray (num_movies,num_features)): matrix of item features
W (ndarray (num_users,num_features)) : matrix of user parameters
b (ndarray (1, num_users) : vector of user parameters
Y (ndarray (num_movies,num_users) : matrix of user ratings of movies
R (ndarray (num_movies,num_users) : matrix, where R(i, j) = 1 if the i-th movies was rated by the j-th user
lambda_ (float): regularization parameter
Returns:
J (float) : Cost
“”"
nm, nu = Y.shape
J = 0
### START CODE HERE ###
Posting codes related to grade cell can cause your membership at the discourse community, so kindly follow code of conduct and refrain from posting grade cell codes in future. Refer FAQ section, Code of Conduct section for details
### END CODE HERE ###

return J

The error you encountered indicates that the function isn’t calculating the cost J correctly, especially around the regularization term. The regularization term for collaborative filtering typically includes X and W, which must be added to J after summing all contributions to J. Once all costs are summed, don’t forget to add the regularization term to J outside of the loop. The absence of this term may be the cause of the discrepancy.

Thank you for your advice. I believe I understand the point so I receive right answer where lambda = 0, but receives wrong cost with lambda and received an assertion error on public test with “AssertionError: Wrong value. Got 13.5. Did you multiply the regularization term by lambda_?”. I put the lambda equation based on “Click for more hints”. I appreciate if I can get direction on the resolution.

You’re on the right track with incorporating the regularization term, but the error you’re seeing now suggests that the regularization term is not being handled correctly. The regularization term includes the sum of squares of all elements in W and X, multiplied by lambda and divided by 2.

Thank you for your comment. since the divided by 2 made at last in the original, I understand now.