Skip to content
Browse files

mark: answers for neural networks stuff@

  • Loading branch information...
1 parent 78c0ce8 commit d8e37079190099af9ed7b5b5e78e4a68012c65a4 @mneedham committed
Showing with 44 additions and 11 deletions.
  1. +1 −0 mlclass-ex3/ex3.m
  2. +12 −3 mlclass-ex3/lrCostFunction.m
  3. +7 −5 mlclass-ex3/oneVsAll.m
  4. +13 −0 mlclass-ex3/predict.m
  5. +11 −3 mlclass-ex3/predictOneVsAll.m
View
1 mlclass-ex3/ex3.m
@@ -64,6 +64,7 @@
%% ================ Part 3: Predict for One-Vs-All ================
% After ...
pred = predictOneVsAll(all_theta, X);
+size(pred)
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);
View
15 mlclass-ex3/lrCostFunction.m
@@ -36,9 +36,18 @@
% grad = grad + YOUR_CODE_HERE (using the temp variable)
%
-
-
-
+predictions = sigmoid(X * theta);
+penalty0 = -y' * log(predictions);
+penalty1 = (1-y)' * log(1 - predictions);
+costAdjustment = lambda/(2*m) * sum(theta(2:end) .* theta(2:end));
+
+J = 1/m * sum(penalty0 -penalty1) + costAdjustment;
+
+grad = 1/m * X' * (predictions -y);
+temp = theta;
+temp(1) = 0;
+temp(2:end) = (lambda/m * theta)(2:end);
+grad = grad + temp;
View
12 mlclass-ex3/oneVsAll.m
@@ -48,11 +48,13 @@
% fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
% initial_theta, options);
%
-
-
-
-
-
+initial_theta = zeros(n + 1, 1);
+all_theta = zeros(num_labels, n + 1);
+options = optimset('GradObj', 'on', 'MaxIter', 50);
+for c=1:num_labels
+ [grad,cost] = fmincg(@(t)(lrCostFunction(t, X, (y == c), lambda)), initial_theta, options);
+ all_theta(c, :) = grad';
+end
View
13 mlclass-ex3/predict.m
@@ -21,7 +21,20 @@
% can use max(A, [], 2) to obtain the max for each row.
%
+X = [ones(m, 1) X];
+for i=1:m
+ aOne = X(i, :)';
+
+ zTwo = Theta1 * aOne;
+ aTwo = [1; sigmoid(zTwo)];
+
+ zThree = Theta2 * aTwo;
+ aThree = sigmoid(zThree);
+
+ [match, ix] = max(aThree);
+ p(i) = ix;
+end
View
14 mlclass-ex3/predictOneVsAll.m
@@ -31,9 +31,17 @@
%
-
-
-
+for i=1:m
+ prediction = zeros(1, num_labels);
+ for j=1:num_labels
+ iX = X(i, :);
+ jtheta = all_theta(j, :);
+ sigme = iX * jtheta';
+ prediction(j) = sigmoid(sigme);
+ end
+ [match, ix] = max(prediction);
+ p(i) = ix;
+end
% =========================================================================

0 comments on commit d8e3707

Please sign in to comment.
Something went wrong with that request. Please try again.