Skip to content

Commit 21d3b04

Browse files
committed
Ex8 - Part 2
1 parent 43fa2f1 commit 21d3b04

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

mlclass-ex8/mlclass-ex8/cofiCostFunc.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
Theta = reshape(params(num_movies*num_features+1:end), ...
1212
num_users, num_features);
1313

14-
14+
1515
% You need to return the following values correctly
1616
J = 0;
1717
X_grad = zeros(size(X));
@@ -41,16 +41,18 @@
4141
%
4242

4343

44+
J = sum(sum( (( (X * Theta') - Y) .* R) .^ 2 ))/2;
4445

46+
X_grad = (( (X * Theta') - Y) .* R) * Theta;
47+
Theta_grad = (( (X * Theta') - Y) .* R)' * X;
4548

4649

50+
% Regularization
51+
J = J + lambda * (Theta(:)' * Theta(:)) / 2;
52+
J = J + lambda * (X(:)' * X(:)) / 2;
4753

48-
49-
50-
51-
52-
53-
54+
X_grad = X_grad + lambda * X;
55+
Theta_grad = Theta_grad + lambda * Theta;
5456

5557

5658

mlclass-ex8/mlclass-ex8/ex8_cofi.m

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,37 @@
126126
% Initialize my ratings
127127
my_ratings = zeros(1682, 1);
128128

129-
% Check the file movie_idx.txt for id of each movie in our dataset
130-
% For example, Toy Story (1995) has ID 1, so to rate it "4", you can set
131-
my_ratings(1) = 4;
132-
133-
% Or suppose did not enjoy Silence of the Lambs (1991), you can set
134-
my_ratings(98) = 2;
135-
136-
% We have selected a few movies we liked / did not like and the ratings we
137-
% gave are as follows:
138-
my_ratings(7) = 3;
129+
% % Check the file movie_idx.txt for id of each movie in our dataset
130+
% % For example, Toy Story (1995) has ID 1, so to rate it "4", you can set
131+
% my_ratings(1) = 4;
132+
%
133+
% % Or suppose did not enjoy Silence of the Lambs (1991), you can set
134+
% my_ratings(98) = 2;
135+
%
136+
% % We have selected a few movies we liked / did not like and the ratings we
137+
% % gave are as follows:
138+
% my_ratings(7) = 3;
139+
% my_ratings(12)= 5;
140+
% my_ratings(54) = 4;
141+
% my_ratings(64)= 5;
142+
% my_ratings(66)= 3;
143+
% my_ratings(69) = 5;
144+
% my_ratings(183) = 4;
145+
% my_ratings(226) = 5;
146+
% my_ratings(355)= 5;
147+
148+
149+
my_ratings(1) = 5;
150+
my_ratings(7) = 5;
139151
my_ratings(12)= 5;
140152
my_ratings(54) = 4;
141153
my_ratings(64)= 5;
142154
my_ratings(66)= 3;
143155
my_ratings(69) = 5;
144-
my_ratings(183) = 4;
145-
my_ratings(226) = 5;
146-
my_ratings(355)= 5;
156+
my_ratings(98) = 4;
157+
my_ratings(183) = 3;
158+
my_ratings(226) = 4;
159+
my_ratings(355)= 4;
147160

148161
fprintf('\n\nNew user ratings:\n');
149162
for i = 1:length(my_ratings)

0 commit comments

Comments
 (0)