|
11 | 11 | Theta = reshape(params(num_movies*num_features+1:end), ... |
12 | 12 | num_users, num_features); |
13 | 13 |
|
14 | | - |
| 14 | + |
15 | 15 | % You need to return the following values correctly |
16 | 16 | J = 0; |
17 | 17 | X_grad = zeros(size(X)); |
|
21 | 21 | % Instructions: Compute the cost function and gradient for collaborative |
22 | 22 | % filtering. Concretely, you should first implement the cost |
23 | 23 | % function (without regularization) and make sure it is |
24 | | -% matches our costs. After that, you should implement the |
| 24 | +% matches our costs. After that, you should implement the |
25 | 25 | % gradient and use the checkCostFunction routine to check |
26 | 26 | % that the gradient is correct. Finally, you should implement |
27 | 27 | % regularization. |
28 | 28 | % |
29 | 29 | % Notes: X - num_movies x num_features matrix of movie features |
30 | 30 | % Theta - num_users x num_features matrix of user features |
31 | 31 | % Y - num_movies x num_users matrix of user ratings of movies |
32 | | -% R - num_movies x num_users matrix, where R(i, j) = 1 if the |
| 32 | +% R - num_movies x num_users matrix, where R(i, j) = 1 if the |
33 | 33 | % i-th movie was rated by the j-th user |
34 | 34 | % |
35 | 35 | % You should set the following variables correctly: |
36 | 36 | % |
37 | | -% X_grad - num_movies x num_features matrix, containing the |
| 37 | +% X_grad - num_movies x num_features matrix, containing the |
38 | 38 | % partial derivatives w.r.t. to each element of X |
39 | | -% Theta_grad - num_users x num_features matrix, containing the |
| 39 | +% Theta_grad - num_users x num_features matrix, containing the |
40 | 40 | % partial derivatives w.r.t. to each element of Theta |
41 | 41 | % |
42 | 42 |
|
| 43 | +% calculating cost function. |
| 44 | +diff = (X*Theta'-Y); |
| 45 | +J = sum((diff.^2)(R==1))/2; |
| 46 | +J = J + lambda*sum(sum(Theta.^2))/2; % regularized term of theta. |
| 47 | +J = J + lambda*sum(sum(X.^2))/2; % regularized term of x. |
| 48 | + |
| 49 | +% calculating gradient of x. |
| 50 | +for i=1:num_movies |
| 51 | + idx = find(R(i, :)==1); % users that have rated movie i. |
| 52 | + Theta_tmp = Theta(idx, :); % user features of movie i. |
| 53 | + Y_tmp = Y(i, idx); % user's ratings of movie i. |
| 54 | + X_grad(i, :) = (X(i, :)*Theta_tmp' - Y_tmp)*Theta_tmp; |
| 55 | + X_grad(i, :) = X_grad(i, :)+lambda*X(i, :); % regularized term of x. |
| 56 | +end |
43 | 57 |
|
44 | | - |
45 | | - |
46 | | - |
47 | | - |
48 | | - |
49 | | - |
50 | | - |
51 | | - |
52 | | - |
53 | | - |
54 | | - |
55 | | - |
56 | | - |
| 58 | +% calculating gradient of theta. |
| 59 | +for j=1:num_users |
| 60 | + idx = find(R(:, j)==1)'; % movies that have rated by user j. |
| 61 | + X_tmp = X(idx, :); % features of movies rated by user j. |
| 62 | + Y_tmp = Y(idx, j); % user ratings by user j. |
| 63 | + Theta_grad(j, :) = (X_tmp*Theta(j, :)'-Y_tmp)'*X_tmp; |
| 64 | + Theta_grad(j, :) = Theta_grad(j, :)+lambda*Theta(j, :); % regularized term of theta. |
| 65 | +end |
57 | 66 |
|
58 | 67 | % ============================================================= |
59 | 68 |
|
|
0 commit comments