Skip to content

Commit 26c5fd8

Browse files
solves neural networks learning quiz
1 parent a4bd175 commit 26c5fd8

File tree

10 files changed

+49
-42
lines changed

10 files changed

+49
-42
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ __Instructor__: Andrew Ng.
5858
- [One vs. All Multi Class Classifier](week4/machine-learning-ex3/ex3/oneVsAll.m)
5959
- [Predict one vs. all Multi Class Classifier](week4/machine-learning-ex3/ex3/predictOneVsAll.m)
6060
- [Neural Network Prediction Function](week4/machine-learning-ex3/ex3/predict.m)
61+
62+
63+
## Week 5
64+
### Quizzes
65+
- [Neural Networks: Learning](week5/neural-networks-quiz.md)
+13-41
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,15 @@
11
function p = predict(Theta1, Theta2, X)
2-
%PREDICT Predict the label of an input given a trained neural network
3-
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
4-
% trained weights of a neural network (Theta1, Theta2)
5-
6-
% Useful values
7-
m = size(X, 1);
8-
num_labels = size(Theta2, 1);
9-
10-
% You need to return the following variables correctly
11-
% p = zeros(m, 1);
12-
13-
% add x0 in x
14-
a1 = [ones(m, 1) X];
15-
a2 = sigmoid(a1 * Theta1');
16-
a2 = [ones(m, 1) a2];
17-
a3 = sigmoid(a2 * Theta2');
18-
[maxProbability index] = max(a3, [], 2);
19-
p = index;
20-
21-
% ====================== YOUR CODE HERE ======================
22-
% Instructions: Complete the following code to make predictions using
23-
% your learned neural network. You should set p to a
24-
% vector containing labels between 1 to num_labels.
25-
%
26-
% Hint: The max function might come in useful. In particular, the max
27-
% function can also return the index of the max element, for more
28-
% information see 'help max'. If your examples are in rows, then, you
29-
% can use max(A, [], 2) to obtain the max for each row.
30-
%
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
% =========================================================================
41-
42-
2+
%PREDICT Predict the label of an input given a trained neural network
3+
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
4+
% trained weights of a neural network (Theta1, Theta2)
5+
6+
% training data size
7+
m = size(X, 1);
8+
9+
a1 = [ones(m, 1) X];
10+
a2 = sigmoid(a1 * Theta1');
11+
a2 = [ones(m, 1) a2];
12+
a3 = sigmoid(a2 * Theta2');
13+
[maxProbability index] = max(a3, [], 2);
14+
p = index;
4315
end

week4/machine-learning-ex3/ex3/token.mat

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Created by Octave 5.2.0, Mon Jun 15 03:58:24 2020 GMT <unknown@anishLearnsToCode>
1+
# Created by Octave 5.2.0, Mon Jun 15 04:01:39 2020 GMT <unknown@anishLearnsToCode>
22
# name: email
33
# type: sq_string
44
# elements: 1

week5/assets/quiz-1.PNG

29.2 KB
Loading

week5/assets/quiz-2.PNG

28.3 KB
Loading

week5/assets/quiz-3.PNG

17 KB
Loading

week5/assets/quiz-4.PNG

41 KB
Loading

week5/assets/quiz-5.PNG

59.5 KB
Loading

week5/neural-networks-quiz.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Neural Networks: Learning
2+
3+
![Question 1](assets/quiz-1.PNG)
4+
![Question 2](assets/quiz-2.PNG)
5+
![Question 3](assets/quiz-3.PNG)
6+
![Question 4](assets/quiz-4.PNG)
7+
![Question 5](assets/quiz-5.PNG)

week5/week5.m

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
clc;
2+
clear;
3+
4+
% Gradiant Approximater (Gradiant Checker)
5+
function gradent = approximateGradient(theta, cost)
6+
n = length(theta);
7+
gradient = zeros(n, 1);
8+
EPSILON = 1e-4;
9+
for i = 1:n
10+
thetaPlus = theta;
11+
thetaPlus(i) += EPSILON;
12+
thetaMinus = theta;
13+
thetaMinus(i) -= EPSILON;
14+
gradent(i) = (cost(thetaPlus) - cost(thetaMinus)) / (2 * EPSILON);
15+
endfor
16+
endfunction
17+
18+
function J = costFunction(theta)
19+
J = 100 * rand(1, 1);
20+
endfunction
21+
22+
hypothesis = [0 ; 1 ; 2];
23+
disp(approximateGradient(hypothesis, @costFunction));

0 commit comments

Comments
 (0)