-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcofiCostFunc.m
54 lines (33 loc) · 1.03 KB
/
cofiCostFunc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...
num_features, lambda)
X = reshape(params(1:num_movies*num_features), num_movies, num_features);
Theta = reshape(params(num_movies*num_features+1:end), ...
num_users, num_features);
% You need to return the following values correctly
J = 0;
X_grad = zeros(size(X));
Theta_grad = zeros(size(Theta));
s=0;
[a b] = size(R);
s = sum(sum(((X*Theta').* R - (Y .* R) ).^ 2));
a = sum(sum(Theta .^ 2));
b = sum(sum(X .^ 2));
J = 0.5 * ( s + lambda * (a+b));
[a b] = size(X);
for i = 1:a
idx = find(R(i,:)==1);
Thetatemp = Theta(idx,:);
Ytemp = Y(i, idx);
X_grad(i, :) = ((X(i, :) * Thetatemp') - Ytemp ) * Thetatemp;
end
X_grad = X_grad + lambda * X;
[a b] = size(Theta);
for i = 1:a
idx = find(R(:,i)==1);
Xtemp = X(idx,:);
Ytemp = Y(idx, i);
Theta_grad(i, :) = ((Xtemp * Theta(i,:)') - Ytemp )' * Xtemp;
end
Theta_grad = Theta_grad + lambda * Theta;
grad = [X_grad(:); Theta_grad(:)];
end