mirror of
https://github.com/jlengrand/MachineLearningCoursera.git
synced 2026-03-10 08:31:20 +00:00
Finished second question of the assignment,
This commit is contained in:
@@ -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 ================
|
||||
|
||||
@@ -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.
Reference in New Issue
Block a user