2014-10-21 20:59:55 +02:00
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 ) ;
2014-10-23 23:36:15 +02:00
% You need to return the following variables correctly
2014-10-21 20:59:55 +02:00
p = zeros ( size ( X , 1 ) , 1 ) ;
% === === === === === === === = YOUR CODE HERE === === === === === === === =
% Instructions : Complete the following code to make predictions using
2014-10-23 23:36:15 +02:00
% your learned neural network . You should set p to a
2014-10-21 20:59:55 +02:00
% 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 .
%
2014-10-23 23:36:15 +02:00
% Theta1 has size 25 x401
% Theta2 has size 10 x26
2014-10-21 20:59:55 +02:00
2014-10-23 23:36:15 +02:00
a1 = [ ones ( m , 1 ) X ] ;
% a1 has size mx401
2014-10-21 20:59:55 +02:00
2014-10-23 23:36:15 +02:00
a2_tmp = sigmoid ( a1 * Theta1 ' ) ;
% a2_tmp has size m x 401 * 401 x 25 = m x 25
a2 = [ ones ( m , 1 ) a2_tmp ] ;
% a2 has size m x 26
2014-10-21 20:59:55 +02:00
2014-10-23 23:36:15 +02:00
a3 = sigmoid ( a2 * Theta2 ' ) ;
% a3 has size m x 26 * 26 x 10 = m x 10
% note : sigmoid not actually neded here ( arg max ( sigmoid ( z ) ) = arg max ( z ) )
2014-10-21 20:59:55 +02:00
2014-10-23 23:36:15 +02:00
[ ~ , p ] = max ( a3 , [ ] , 2 ) ;
2014-10-21 20:59:55 +02:00
% === === === === === === === === === === === === === === === === === === === === === === === === =
end