% hndWrtnSVld.m      
%            based on sftMx2LyrPt.m 23 Mar 2017
%             30 Mar 2017  
% Andrew P Paplinski, Monash University
% Hand-written digit recognition. MNIST dataset
% using aiRT to create features
% softmax 2-Layer nnet classifier with ReLU
% Pattern learning

%  Validation

clear
diary off
hdr = 'MNIST Validation ';
tic
% load MNIST/MNaniRT % X Y Xtst Ytst
load MNIST/mnistIL % Mimgs Mlbls MtstImgs MtstLbls
[r, c, N] = size(MtstImgs) ;
X = reshape(MtstImgs, r*c, N) ; % 784 by 10000
Y = MtstLbls ;
% [r, c, N] = size(Mimgs) ;
% X = reshape(Mimgs, r*c, N) ; % 784 by 60000
% Y = Mlbls ;
% % 
% % testing=========
% N = 5000 ;
% X = X(:, 1:N) ;
% Y = Y(1:N) ;
% % ================

[D, N] = size(X) ;     % dimensionality of X and the number of points
                       % D = 28, size of aniRt vectors                      
M = length(unique(Y)); % number of classes, dimensionality of the output Z
Xh = [X ; ones(1,N)] ; % adding bias input
% converting the expected class labels Y into outputs YY
YY = zeros(M,N) ;
for n = 1:N
    YY(Y(n)+1, n) = 1;  
end

%  setting up the time marker and the file_name to store the results 
tim = clock; dat = date ;
disp([hdr, date, sprintf('%4d:%02d',tim(4:5))])   
fnm = ['A',dat(4:6),sprintf('%02dh%02d%02d',tim(3:5))];
fid = fopen('CrntFnm.txt','wt');
fprintf(fid,'%s',fnm);  % writing a current base file name to CrntFnm.txt
fclose(fid);

dfnm = ['rslts/', fnm] ; % the base file name for runs  
diary([dfnm, '.txt']) 

fprintf(' Total number of testing examples: %d', N)

% Downloading the NNet 
%    Dh hidden neurons with ReLU, M output neurons with Softmax
a = dir( 'rslts/*.mat') ;  
dfld = ['rslts/', a(end).name] ;  % last *.mat file in rslts
load(dfld)   %  'Wh','Wz' , 'etah', 'etaz'
[Dh, ~] = size(Wh) ; % number of hidden neurons

% Validation = testing set accuracy
Uh = Wh*Xh ; % postsynaptic activities in the hidden layer
Zh = [max(0,Uh); ones(1,N)];   % ReLU nonlinearity and bias
Uz = Wz*Zh ; 
% evaluate testing set accuracy
[~, Umx] = max(Uz) ; % predicted classes
tsacc = sum((Umx'-1) == Y)/N ;
fprintf('\n Training accuracy: %0.4f\n', tsacc)
% ab = [Y (Umx'-1)] ;
% ab(1:20,:)
 
diary off

 