From de9c69bae64a3c2bda34a47e81a7bce6236fb6ca Mon Sep 17 00:00:00 2001 From: mjjo53 Date: Sun, 28 May 2017 06:51:50 +0900 Subject: [PATCH] =?UTF-8?q?=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex3/lrCostFunction.m | 7 ++++--- ex3/oneVsAll.m | 10 +++++----- ex3/predict.m | 10 ++++++---- ex3/predictOneVsAll.m | 5 ++--- ex3/token.mat | 15 +++++++++++++++ 5 files changed, 32 insertions(+), 15 deletions(-) create mode 100644 ex3/token.mat diff --git a/ex3/lrCostFunction.m b/ex3/lrCostFunction.m index 3ddfbc5..64b5b03 100644 --- a/ex3/lrCostFunction.m +++ b/ex3/lrCostFunction.m @@ -36,10 +36,11 @@ grad = zeros(size(theta)); % grad = grad + YOUR_CODE_HERE (using the temp variable) % +hx = sigmoid(X*theta); +%J = sum(-y.*log(hx)-(1.-y).*log(1.-hx))/m + lambda*sum([0; theta(2:end)].^2)/(2*m); +J = (-y'*log(hx)-(1.-y)'*log(1.-hx))/m + lambda*sum([0; theta(2:end)].^2)/(2*m); - - - +grad = X'*(hx-y)/m + lambda*[0; theta(2:end)]/m; diff --git a/ex3/oneVsAll.m b/ex3/oneVsAll.m index a3e0bdb..0cfd4c9 100644 --- a/ex3/oneVsAll.m +++ b/ex3/oneVsAll.m @@ -49,13 +49,13 @@ X = [ones(m, 1) X]; % initial_theta, options); % +for c=1:num_labels + initial_theta = zeros(n + 1, 1); + options = optimset('GradObj', 'on', 'MaxIter', 50); + [theta] = fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options); - - - - - + all_theta(c,:) = theta'; diff --git a/ex3/predict.m b/ex3/predict.m index d8a8faf..c9bc3d7 100644 --- a/ex3/predict.m +++ b/ex3/predict.m @@ -21,11 +21,13 @@ p = zeros(size(X, 1), 1); % can use max(A, [], 2) to obtain the max for each row. % +X = [ones(m, 1), X]; +a2 = sigmoid(X*Theta1'); +a2 = [ones(m, 1), a2]; +a3 = sigmoid(a2*Theta2'); - - - - +[val, idx] = max(a3, [], 2); +p = idx; diff --git a/ex3/predictOneVsAll.m b/ex3/predictOneVsAll.m index 8d7595c..418a378 100644 --- a/ex3/predictOneVsAll.m +++ b/ex3/predictOneVsAll.m @@ -30,9 +30,8 @@ X = [ones(m, 1) X]; % for each row. % - - - +[val, idx] = max(X*all_theta', [], 2); +p = idx; diff --git a/ex3/token.mat b/ex3/token.mat new file mode 100644 index 0000000..8100f0a --- /dev/null +++ b/ex3/token.mat @@ -0,0 +1,15 @@ +# Created by Octave 4.2.1, Thu May 11 16:43:37 2017 GMT +# name: email +# type: sq_string +# elements: 1 +# length: 16 +mjjo53@gmail.com + + +# name: token +# type: sq_string +# elements: 1 +# length: 16 +77Tay6oLh6AIePHt + +