Finished second question of the assignment,

This commit is contained in:
julien Lengrand-Lambert
2015-12-06 12:04:27 +01:00
parent b2587bca3c
commit c5d4509e65
3 changed files with 14 additions and 3 deletions

View File

@@ -81,7 +81,7 @@ fprintf(['Cost at parameters (loaded from ex4weights): %f '...
'\n(this value should be about 0.287629)\n'], J);
fprintf('\nProgram paused. Press enter to continue.\n');
pause;
% pause;
%% =============== Part 4: Implement Regularization ===============
% Once your cost function implementation is correct, you should now
@@ -100,7 +100,7 @@ fprintf(['Cost at parameters (loaded from ex4weights): %f '...
'\n(this value should be about 0.383770)\n'], J);
fprintf('Program paused. Press enter to continue.\n');
pause;
% pause;
%% ================ Part 5: Sigmoid Gradient ================

View File

@@ -77,15 +77,26 @@ for ii = 1:size(y2, 1)
y2(ii, y(ii)) = 1;
end
% Cost without regularization
for i = 1:m
temp1 = - y2(i, :) * log(a3(i, :))';
temp2 = (1-y2(i, :))*(log(1- a3(i, :)))';
temp3 = temp1 - temp2;
J = J + temp3;
end
J = J / m;
% Adding regularization
Theta11 = Theta1;% Theta1 without bias;
Theta11(:, 1) = [];
Theta22 = Theta2;
Theta22(:, 1) = [];
Theta11s = Theta11.^2;
Theta22s = Theta22.^2;
reg = (lambda/(2*m))*(sum(Theta11s(:)) + sum(Theta22s(:)));
J = J + reg;
% -------------------------------------------------------------

Binary file not shown.