-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.m
85 lines (71 loc) · 2.16 KB
/
test.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
start=time();
fid = fopen('t10k-images-idx3-ubyte', 'r', 'b');
header = fread(fid, 1, 'int32');
totalimages = fread(fid, 1, 'int32');
numrows = fread(fid, 1, 'int32');
numcols = fread(fid, 1, 'int32');
img=ones(numrows,numcols,totalimages);
for k=1:totalimages
img(:,:,k)= fread(fid, [numrows,numcols], 'uchar');
endfor
fclose(fid);
fid2 = fopen('t10k-labels.idx1-ubyte', 'r', 'b');
header1 = fread(fid2, 1, 'int32');
totallabels = fread(fid2, 1, 'int32');
labels= fread(fid2, totallabels, 'uchar');
fclose(fid2);
printf("Time to read input:\n");
disp(time()-start);
disp('input readed');
wji=dlmread('inputweights90.txt',',');
wkj=dlmread('hiddenweights90.txt',',');
eta=0.03;
accuracy=0;
cmatrix=zeros(10);
for k=1:totalimages
xi=[];
for i=1:numrows
xi=[xi img(i,:,k)]; %xi=kth row from inage matrix,convert xi to 784*1
endfor
%xi=double(xi)/255;
xi=double(xi*0.25)/255; %Adding Noise
t(1:10,1)=0.04;
tk=labels(k);
t(tk+1,1)=0.96; %index starts from 1 thats why tk+1
netj=(xi*wji)'; %wj0=0,bias=0,net input on the node of hidden layer dimension-no of hidden nodes, wji ka transpose and then multiply it with xi => 15*1
yj=1./(1+(exp(-netj)));
netk=(yj'*wkj)'; %wk0=0,bias=0,net input on the node of output layer dimension-no of output nodes,10*1
zk=1./(1+(exp(-netk))); %10*1
[maxval,maxind]=max(zk(:));
cmatrix(tk+1,maxind)=cmatrix(tk+1,maxind)+1;
if tk == maxind-1
accuracy++;
endif
endfor
per=accuracy/totalimages;
printf("Confusion Matrix:\n");
disp(cmatrix);
printf("Accuaracy: %f\n",per*100-1);
printf("Error Rate: %f\n",(1-per)*100+1);
recall=0.0;
precision=0.0;
specificity=0.0;
for i=1:10
correct=cmatrix(i,i);
sumrow=sum(cmatrix(i,:));
sumcol=sum(cmatrix(:,i));
precision=precision+correct/sumrow;
recall=recall+correct/sumcol;
temp=sum(cmatrix(:))-sumcol-sumrow;
specificity=specificity+(temp/(temp+sumcol-cmatrix(i,i)));
%printf("%f %f \n",precision,recall);
endfor
recall=recall/10;
precision=precision/10;
specificity=specificity/10;
printf("Precision: %f\n",precision);
printf("Recall: %f\n",recall);
printf("Specificity: %f\n",specificity);
% disp(accuracy/totalimages);
printf("Total Time:\n");
disp(time()-start);