C3_W2_Collaborative_RecSys_Assignment: Exercise 1 errors

I’m unable to troubleshoot the errors thrown by the grader. In particular, I get:

—> 15 assert not np.isclose(J, 13.5), f"Wrong value. Got {J}. Did you multiplied the regulartization term by lambda_?"
16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"
17

<array_function internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
2255 y = array(y, dtype=dt, copy=False, subok=True)
2256
→ 2257 xfin = isfinite(x)
2258 yfin = isfinite(y)
2259 if all(xfin) and all(yfin):

TypeError: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’

The relevant parts of my code are as follows:

START CODE HERE

for j in range(nu):
      w = xxxxxx
      b_j = xxxxx
      for i in range(nm): 
           x = xxxxxx
           y = xxxxxx
           r = xxxxxx
           **J += np.square(r * (np.dot(w,x) + b_j - y ) )**

**J = J/2**
### END CODE HERE ### 

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

Grateful for any help

This error occurs when a test program tries to compare your J with 13.5 by np.isclose().
And, this types of error occurs when your “J” is non-numeric like “string”. So, it is better to check your return code “J” from “confi_cost_func”.

The potential problem should be the last one to update J with a regularization term. (You may want to double check the syntax, and also ensure to divide by 2, if you want to add it at the last. If this term is added before “J=J/2”, then, no need to change.)

Thank you very much. I’m still stuck.

Regardless of the indentation of the regularisation term, I get

TypeError 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)
13
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 multiplied the regulartization term by lambda_?"
16 assert np.isclose(J, 27), f"Wrong value. Expected {27}, got {J}. Check the regularization term"
17

<array_function internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
2255 y = array(y, dtype=dt, copy=False, subok=True)
2256
→ 2257 xfin = isfinite(x)
2258 yfin = isfinite(y)
2259 if all(xfin) and all(yfin):

TypeError: ufunc ‘isfinite’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’

Thanks again for your attention

Have you checked your return value J ? That’s the first thing to do, as I suggested.
The error says that it is not numeric. You can easily check with a print command just one line before “return J”.

Another possibility is,… you may accidentally erase “return J”…

Thank you very much.

Indeed, the error arose from the accidental deletion of the “return J” line

OK, now you can go ahead !

I am getting the same error in Exercise1 -

Here is the code:

# GRADED FUNCTION: cofi_cost_func
# UNQ_C1

def cofi_cost_func(X, W, b, Y, R, lambda_)

========
While running the below code getting error-

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

Can someone help, what is wrong with the code?

Got the error. It was an indentation of the regularization term.

Thanks.

3 Likes

Thank you so much you solved my bug that I was confused for an hour omg

hi @sumeet.dey33 if you see the formula for cost the r term is out of the squares i am confused about how you get the result but my version of cost and I think as per the formula it should be

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 ###  
    ### END CODE HERE ### 

    return J

Hello @anshbhatnagar007,

I think we are discussing the above formula. it shows that the r(i,j) is not squared, but since r(i,j) is either 0 or 1, squaring it or not won’t make any difference.

Lastly, thank you for pointing that out, but I am afraid I have to remove your code becaues we don’t share assignment code here.

Have a good day and cheers,
Raymond

P.S. I removed assignment codes in other posts in this thread as well.