Help with Lab1 Week2 Course 3

Hi, I have been having trouble with the Lab1 Week2 Course 3. I code it and after several failed attempts looked at the solution. However, I observe they are the same.

This is my code:

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 ###
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 += (lambda_/2) * (np.sum(np.square(W)) + np.sum(np.square(X)))

J = J/2

return J

Hi Juanes!, The following expression gives the collaborative filtering cost function:

That said, the exercise asks to do the non-regularized cost function implementation. Therefore it is not necessary to include the regularization term. On the other hand, your work seems not to include the norm term.

Cheers,

Ahhh ok, I thought I had to regularize J. I have made the necessary changes, which works on the examples but not on the grading bot:

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 ###  

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

return J

I correct the previous comment, the exercise asks you to start with the non-regularized cost function implementation and then add the regularization term. So the correct solution is to include both terms in the implementation.

Having said this, it will only be necessary to add the two terms of J that you’ve made.

Apologies for the misunderstanding.
Cheers,

Hello Enzo,
Thank you for the correction, that is what I thought initially so I conditioned if lambda is zero apply one formula, else apply the other. I dont understand quite well what you mean with adding the two terms of J, maybe could you expand on that please?

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 ###  

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]
        if(lambda_ == 0):
            J += np.square(r * (np.dot(w,x) + b_j - y ) )
        if(lambda_):
            J += (lambda_/2) * (np.sum(np.square(w)) + np.sum(np.square(x)))
J = J/2

return J

Hi Juanes! Sorry for the delay. My email did not notify me of your response. As my first post says, the cost function is composed of the sum of two terms the norm term (euclidian distance) and the regularization term.

So J must be the sum of those two terms and not depend on the lambda value. Finally, the cost function is given by:

J = (1/2)*(norm_term) + (lambda_/2)*(reg_term)

Regularization Term:

Norm Term:

Hey Enzo! Don’t worry, sometimes the email notifications do not work correctly.

On the other hand, thank you very much for your help. I was able to code the solution. :smile:

1 Like