IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clssr_ann.m
Go to the documentation of this file.
1 %> @brief Neural Network Classifier. This is a wrapper to MATLAB's Neural Networks Toolbox
2 %>
3 %> Check MATLAB's NN toolbox documentation on net.trainParam
4 %>
5 %> This is by default tuned to finished trained mostly when the net overfits:
6 %> @arg High number of epochs
7 %> @arg goal is zero (will only stop according to this criterion if reaches 100% classification)
8 %> @arg min_grad is very small as well (1e-5)
9 %>
10 %> Input weights are initialized to 'midpoint', and layer weights are initialized to zero. This way, the classifier will be deterministic
11 %> (the default MATLAB "initnw" sounds nice but has randomness in it)
12 %>
13 %> @sa uip_clssr_ann.m
14 classdef clssr_ann < clssr
15  properties
16  %> whether classes must be converter into multiple-output boolean targets.
17  flag_class2mo = 1;
18  %> = [1]. Number of neurons in each hidden layer.
19  hiddens = [1];
20 
21  % Learning parameters. Names match the ones in net.trainParam
22 
23  %> =0. Stands for the goal MSE (Mean Squared Error). Training stops when achieving this error. We don't know what to expect, so it is fair to
24  %> expect 0 error (100 classification).
25  goal = 0;
26  %> = 1000
27  epochs = 1000;
28  %> = 10.
29  show = 10;
30  %> = 0.05.
31  lr = 0.05;
32  %> = 15 Maximum number of validation increases
33  max_fail = 10;
34  %> =0.
35  flag_show_window = 0;
36  %> Whether to weight the observations (for unbalanced classes)
37  flag_weighted = 0;
38  %> =1e-5. Minimum gradient.
39  min_grad = 1e-5;
40  end;
41 
42  properties(SetAccess=protected)
43  net;
44  end;
45 
46  methods
47  function o = clssr_ann(o)
48  o.classtitle = 'Artificial Neural Network';
49  o.short = 'ANN';
50  end;
51  end;
52 
53  methods(Access=protected)
54 
55  function o = do_boot(o)
56  end;
57 
58 
59 
60  % TODO actually this could even be multiple-trainable
61  function o = do_train(o, data)
62  o.classlabels = data.classlabels;
63 
64  tic;
65 
66  % TODO I pasted from the boot procedure an dI don't know it this is working, long time not using NN
67 
68  targets = classes2boolean(o.get_classes(data), o.no_outputs)';
69 
70  o.net = newpr(data.X', targets, o.hiddens);
71 
72  o.net.trainFcn = 'trainlm';
73 
74  o.net.trainParam.goal = o.goal;
75  o.net.trainParam.epochs = o.epochs;
76  o.net.trainParam.show = o.show;
77  o.net.trainParam.lr = o.lr;
78  o.net.trainParam.max_fail = o.max_fail;
79  o.net.trainParam.showWindow = o.flag_show_window; % gets rid of this annoying GUI, TODO however not quite working
80 
81 
82 
83 
84 
85 
86  o.net.initFcn = 'initlay'; % (net.initParam automatically becomes initlay's default parameters.)
87  for i = 1:numel(o.net.layers)
88  o.net.layers{i}.initFcn = 'initwb';
89  end;
90  for i = 1:size(o.net.inputWeights, 1)
91  for j = 1:size(o.net.inputWeights, 2)
92  if ~isempty(o.net.inputWeights{i, j})
93  o.net.inputWeights{i, j}.initFcn = 'midpoint';
94  end;
95  end;
96  end;
97 
98 
99  % These instructions of how to use midpoint are from MATLAB's reference to the midpoint function.
100  % However, this gave errors and I opted for initializing the layer weights to zero
101 
102 % for i = 1:size(o.net.layerWeights, 1)
103 % for j = 1:size(o.net.layerWeights, 2)
104 % if ~isempty(o.net.layerWeights{i, j})
105 % o.net.layerWeights{i, j}.initFcn = 'midpoint';
106 % end;
107 % end;
108 % end;
109 
110 
111 
112 
113  o.net = init(o.net);
114 
115  for i = 1:size(o.net.LW, 1)
116  for j = 1:size(o.net.LW, 1)
117  z = o.net.LW{i, j};
118  if ~isempty(z)
119  o.net.LW{i, j} = zeros(size(z));
120  end;
121  end;
122  end;
123 
124 
125 
126 
127 
128 
129 
130  % % Changes the way the data is split
131  % o.net.divideFcn = 'divideind';
132  % o.net.divideParam.trainInd = 1:no_obs_train;
133  % o.net.divideParam.valInd = (no_obs_train+1):no_obs_total;
134  % o.net.divideParam.testInd = (no_obs_train+1):no_obs_total;
135 
136 
137  if ~o.flag_weighted
138  [o.net, tr, output, error] = train(o.net, data.X', targets);
139  else
140  ww = data.get_weights(); % weights per class
141  weights = zeros(1, data.no); % weights per observation
142 
143  for i = 1:data.nc
144  weights(data.classes == i-1) = ww(i);
145  end;
146 
147 
148 % train(ftdnn_net,p,t,Pi,Ai,ew1);
149  [o.net, tr, output, error] = train(o.net, data.X', targets, {}, {}, weights);
150 % [o.net, tr, output, error] = train(o.net, data.X', targets, [], []);
151  end;
152 
153  o.time_train = toc;
154  end;
155 
156 
157 
158  function est = do_use(o, data)
159  est = estimato();
160  est.classlabels = o.classlabels;
161  est = est.copy_from_data(data);
162 
163  tic();
164  Y = sim(o.net, data.X')';
165 
166  % TODO SoftMax!!!!!!!!11
167  est.X = Y;
168 
169 
170  o.time_use = toc();
171  end;
172  end;
173 end
function classes2boolean(in classes, in no_different)
Neural Network Classifier. This is a wrapper to MATLAB's Neural Networks Toolbox. ...
Definition: clssr_ann.m:14
Classifiers base class.
Definition: clssr.m:6
Analysis Session (AS) base class.
Definition: as.m:6