Kernel Perceptron for Handwritten Digit Recognition HW5 (GI03)

Note this code is DESIGNED to be INEFFICIENT.

$PrePrint=Short[#,20]&;

Off[General::spell1,General::spell2,General::spell3] (* Turn off spell warnings *)

dtrain123=Import["dtrain123.dat","Table"] ;

Dimensions[dtrain123]

dtest123=Import["dtest123.dat","Table"] ;

plotChar[char_]:= Module[{data,rep},
rep = Take[char,{2,257}];
data = Table[rep[[16*i+j+1]],{i,15,0,-1},{j,0,15}] ;
ListDensityPlot[data, Mesh -> False,PlotLabel->"The number "<>ToString[char[[1]]]];
] ;

plotChar[dtrain123[[1]]]

kerval[a_,b_] := (a.b)^3

mysign[x_] := If[x<=0.0,-1.0,1.0]

clearGLBcls[data_]  := 
GLBcls=Table[0.0,{3},{Length[data]}];

classpredk[dat_,pat_,cl_]  := Module[{lcd =Length[cl],i},
Sum[cl[[i]]kerval[pat,Take[dat[[i]],{2,257}]], {i,lcd}]]

traingen[dat_] := Module[{i,val,y,preds,mistakes=0},
For[i=1,i≤Length[dat],i++,
val = dat[[i]][[1]] ;

(* get predictions of the 3 classifiers *);
preds = Table[classpredk[dat,Take[dat[[i]],{2,257}],GLBcls[[j]]],{j,1,3}]  ;


maxc= - 10000000000000000.0;
For[j=1,j≤3,j++,
y = If[val==j,1.0,-1.0 ];

(* update *)
If[y preds[[j]]≤ 0,
GLBcls[[j,i]]= GLBcls[[j,i]] -mysign[preds[[j]]]] ;
If[preds[[j]]>maxc,(maxc=preds[[j]];maxi=j)];
];
If[maxi ≠ val,mistakes++] ;
];mistakes] ;

testclassifiers[dat_,testdat_] := Module[{i,val,y,preds,mistakes=0},
For[i=1,i≤Length[testdat],i++,
val = testdat[[i]][[1]];
preds = Table[classpredk[dat,Take[testdat[[i]],{2,257}],GLBcls[[j]]],{j,1,3}]  ;
maxc= - 1000000000.0;
For[j=1,j≤3,j++,
y = If[val==j,1.0,-1.0 ];
If[preds[[j]]>maxc,(maxc=preds[[j]];maxi=j)];
];
If[maxi ≠ val,mistakes++] ;
]; N[mistakes/Length[testdat]]] ;

demo[train_,test_] := Module[{i,rtn},
clearGLBcls[train];
Do[ (* 3 iterations chosen arbitrarily *)
rtn = traingen[train] //Timing ;
Print["Training - epoch ",i," required ",rtn[[1]],
 " with ",rtn[[2]]," mistakes out of ",Length[train]," items."] ;
rtn = testclassifiers[train,test] //Timing ;
Print["Testing - epoch ",i," required ",rtn[[1]],
" with a test error of ",NumberForm[rtn[[2]]*100,3],"%."] ;,{i,3}]]

demo[dtrain123,dtest123];