Skip to content

Commit

Permalink
solves neural networks learning quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
anishLearnsToCode committed Jun 18, 2020
1 parent a4bd175 commit 26c5fd8
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 42 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ __Instructor__: Andrew Ng.
- [One vs. All Multi Class Classifier](week4/machine-learning-ex3/ex3/oneVsAll.m)
- [Predict one vs. all Multi Class Classifier](week4/machine-learning-ex3/ex3/predictOneVsAll.m)
- [Neural Network Prediction Function](week4/machine-learning-ex3/ex3/predict.m)


## Week 5
### Quizzes
- [Neural Networks: Learning](week5/neural-networks-quiz.md)
54 changes: 13 additions & 41 deletions week4/machine-learning-ex3/ex3/predict.m
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
% trained weights of a neural network (Theta1, Theta2)

% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);

% You need to return the following variables correctly
% p = zeros(m, 1);

% add x0 in x
a1 = [ones(m, 1) X];
a2 = sigmoid(a1 * Theta1');
a2 = [ones(m, 1) a2];
a3 = sigmoid(a2 * Theta2');
[maxProbability index] = max(a3, [], 2);
p = index;

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned neural network. You should set p to a
% vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. In particular, the max
% function can also return the index of the max element, for more
% information see 'help max'. If your examples are in rows, then, you
% can use max(A, [], 2) to obtain the max for each row.
%









% =========================================================================


%PREDICT Predict the label of an input given a trained neural network
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
% trained weights of a neural network (Theta1, Theta2)

% training data size
m = size(X, 1);

a1 = [ones(m, 1) X];
a2 = sigmoid(a1 * Theta1');
a2 = [ones(m, 1) a2];
a3 = sigmoid(a2 * Theta2');
[maxProbability index] = max(a3, [], 2);
p = index;
end
2 changes: 1 addition & 1 deletion week4/machine-learning-ex3/ex3/token.mat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Created by Octave 5.2.0, Mon Jun 15 03:58:24 2020 GMT <unknown@anishLearnsToCode>
# Created by Octave 5.2.0, Mon Jun 15 04:01:39 2020 GMT <unknown@anishLearnsToCode>
# name: email
# type: sq_string
# elements: 1
Expand Down
Binary file added week5/assets/quiz-1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week5/assets/quiz-2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week5/assets/quiz-3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week5/assets/quiz-4.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added week5/assets/quiz-5.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions week5/neural-networks-quiz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Neural Networks: Learning

![Question 1](assets/quiz-1.PNG)
![Question 2](assets/quiz-2.PNG)
![Question 3](assets/quiz-3.PNG)
![Question 4](assets/quiz-4.PNG)
![Question 5](assets/quiz-5.PNG)
23 changes: 23 additions & 0 deletions week5/week5.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
clc;
clear;

% Gradiant Approximater (Gradiant Checker)
function gradent = approximateGradient(theta, cost)
n = length(theta);
gradient = zeros(n, 1);
EPSILON = 1e-4;
for i = 1:n
thetaPlus = theta;
thetaPlus(i) += EPSILON;
thetaMinus = theta;
thetaMinus(i) -= EPSILON;
gradent(i) = (cost(thetaPlus) - cost(thetaMinus)) / (2 * EPSILON);
endfor
endfunction

function J = costFunction(theta)
J = 100 * rand(1, 1);
endfunction

hypothesis = [0 ; 1 ; 2];
disp(approximateGradient(hypothesis, @costFunction));

0 comments on commit 26c5fd8

Please sign in to comment.