Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 4/Programming Assignment - 3/machine-learning-ex3/ex3/predict.m
864 views
1
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(size(X, 1), 1);
12
13
% ====================== YOUR CODE HERE ======================
14
% Instructions: Complete the following code to make predictions using
15
% your learned neural network. You should set p to a
16
% vector containing labels between 1 to num_labels.
17
%
18
% Hint: The max function might come in useful. In particular, the max
19
% function can also return the index of the max element, for more
20
% information see 'help max'. If your examples are in rows, then, you
21
% can use max(A, [], 2) to obtain the max for each row.
22
%
23
a1 = [ones(m,1) X]; %Adding bias-unit to a1
24
25
% Step-1: Computing the 2nd activation layer
26
z2 = a1 * Theta1';
27
a2 = sigmoid(z2);
28
29
% Step-2: Computing the 3rd activation layer
30
a2 = [ones(m,1) a2]; %Adding bias-unit to a2
31
z3 = a2 * Theta2';
32
a3 = sigmoid(z3); %This computes the final hypothesis
33
34
% Step-4: Finding the max prediction and determining its index, to compute...
35
% the probability at class k, where k =[0:9], represents the digits
36
[predict index] = max(a3'); % size(a3) = 1 x 5000
37
p = index';
38
% =========================================================================
39
end
40
41