완료
This commit is contained in:
@@ -20,8 +20,10 @@ grad = zeros(size(theta));
|
|||||||
% Note: grad should have the same dimensions as theta
|
% Note: grad should have the same dimensions as theta
|
||||||
%
|
%
|
||||||
|
|
||||||
|
hx = sigmoid(X*theta);
|
||||||
|
J = sum(-y.*log(hx) - (1.-y).*log(1.-hx))/m;
|
||||||
|
|
||||||
|
grad = sum((hx-y).*X)/m;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ grad = zeros(size(theta));
|
|||||||
% derivatives of the cost w.r.t. each parameter in theta
|
% derivatives of the cost w.r.t. each parameter in theta
|
||||||
|
|
||||||
|
|
||||||
|
hx = sigmoid(X*theta);
|
||||||
|
J = sum(-y.*log(hx)-(1.-y).*log(1.-hx))/m + lambda*sum([0; theta(2:end)].^2)/(2*m);
|
||||||
|
grad = (sum((hx-y).*X)./m)' + lambda*[0; theta(2:end)]/m;
|
||||||
|
|
||||||
% =============================================================
|
% =============================================================
|
||||||
|
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ fprintf('Expected gradients (approx) - first five values only:\n');
|
|||||||
fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n');
|
fprintf(' 0.0085\n 0.0188\n 0.0001\n 0.0503\n 0.0115\n');
|
||||||
|
|
||||||
fprintf('\nProgram paused. Press enter to continue.\n');
|
fprintf('\nProgram paused. Press enter to continue.\n');
|
||||||
pause;
|
%pause;
|
||||||
|
|
||||||
% Compute and display cost and gradient
|
% Compute and display cost and gradient
|
||||||
% with all-ones theta and lambda = 10
|
% with all-ones theta and lambda = 10
|
||||||
@@ -90,7 +90,7 @@ fprintf('Expected gradients (approx) - first five values only:\n');
|
|||||||
fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n');
|
fprintf(' 0.3460\n 0.1614\n 0.1948\n 0.2269\n 0.0922\n');
|
||||||
|
|
||||||
fprintf('\nProgram paused. Press enter to continue.\n');
|
fprintf('\nProgram paused. Press enter to continue.\n');
|
||||||
pause;
|
%pause;
|
||||||
|
|
||||||
%% ============= Part 2: Regularization and Accuracies =============
|
%% ============= Part 2: Regularization and Accuracies =============
|
||||||
% Optional Exercise:
|
% Optional Exercise:
|
||||||
@@ -107,7 +107,7 @@ pause;
|
|||||||
initial_theta = zeros(size(X, 2), 1);
|
initial_theta = zeros(size(X, 2), 1);
|
||||||
|
|
||||||
% Set regularization parameter lambda to 1 (you should vary this)
|
% Set regularization parameter lambda to 1 (you should vary this)
|
||||||
lambda = 1;
|
lambda = 100;
|
||||||
|
|
||||||
% Set Options
|
% Set Options
|
||||||
options = optimset('GradObj', 'on', 'MaxIter', 400);
|
options = optimset('GradObj', 'on', 'MaxIter', 400);
|
||||||
|
|||||||
@@ -12,10 +12,15 @@ figure; hold on;
|
|||||||
% examples and 'ko' for the negative examples.
|
% examples and 'ko' for the negative examples.
|
||||||
%
|
%
|
||||||
|
|
||||||
|
pos = find(y==1);
|
||||||
|
neg = find(y == 0);
|
||||||
|
|
||||||
|
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
|
||||||
|
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', 'MarkerSize', 7);
|
||||||
|
|
||||||
|
ylabel('Exam 2 score');
|
||||||
|
xlabel('Exam 1 score');
|
||||||
|
legend('Admitted', 'Not admitted');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ p = zeros(m, 1);
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
hx = X*theta;
|
||||||
|
p = hx >= 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ g = zeros(size(z));
|
|||||||
% vector or scalar).
|
% vector or scalar).
|
||||||
|
|
||||||
|
|
||||||
|
g = 1./(1+exp(-z));
|
||||||
|
|
||||||
|
|
||||||
% =============================================================
|
% =============================================================
|
||||||
|
|||||||
15
ex2/token.mat
Normal file
15
ex2/token.mat
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Created by Octave 4.2.1, Wed May 10 12:15:18 2017 GMT <unknown@unknown>
|
||||||
|
# name: email
|
||||||
|
# type: sq_string
|
||||||
|
# elements: 1
|
||||||
|
# length: 16
|
||||||
|
mjjo53@gmail.com
|
||||||
|
|
||||||
|
|
||||||
|
# name: token
|
||||||
|
# type: sq_string
|
||||||
|
# elements: 1
|
||||||
|
# length: 16
|
||||||
|
fZMMhxjQUsSZ1osy
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user