forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doclassif.m
65 lines (52 loc) · 1.55 KB
/
doclassif.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
function [wh,wo,tstcol]=doclassif(trndat,trnlab,tstdat,tstlab,U,I,E,EL,R)
% [H,O,TO] = doclassif(D,L,TD,TL,U,I,E,EL,R) Try classification
% D is rows of training data, with labels given by L.
% TD is rows of test data, with oracle results given by TL.
% Train a neural net on D with E epochs of I iterations,
% starting with learning rate = R, then halving it each epoch after
% the first EL. Use U hidden units.
% Returns the neural net in H and O, and the test output classes in TO
% 2001-03-27 [email protected]
if nargin < 5
U = 5;
end
if nargin < 6
I = 29;
end
if nargin < 7
E = 4;
end
if nargin < 8
EL = 1;
end
if nargin < 9
R = 0.08;
end
% Make actual target matrix
[ntrn,ndim] = size(trndat);
trg = sparse([1:ntrn],trnlab,1);
% Calculate norms
nrm = [mean(trndat);std(trndat)];
% Do training
wh = U;
wo = [];
for ep = 1:E
if ep > EL
R = R / 2;
end
[wh,wo] = nntrain(trndat,nrm,trg,R,I,wh,wo,16,10);
% Do testing
% Measure accuracy on train data to start
trnop = nnfwd(trndat,nrm,wh,wo);
trncol = findmaxcol(trnop);
tfac = mean(trncol == trnlab);
% Now do real test on test data
% Calculate net output for test data (using norms from trn data)
tstop = nnfwd(tstdat,nrm,wh,wo);
plot(tstop);
% Find 'winning' output for each pattern
tstcol = findmaxcol(tstop);
% Calculate proportion of matches to oracle labels
efac = mean(tstcol == tstlab);
disp(['Ep ',num2str(ep), ' lr=', num2str(R), ' frame accuracy on trn = ', num2str(100*tfac),'% test = ', num2str(100*efac),'%']);
end