Wednesday, July 24, 2013

MATLAB series: Neural Network Toolbox - newff

1) Calling newff function using the command;

net=newff(p,t,2);

will automatically scale the inputs and outputs to;

net.inputs{1}.processFcns = 'fixunknowns'    'removeconstantrows'    'mapminmax'
net.outputs{2}.processFcns = 'removeconstantrows'    'mapminmax'

2) If you were to directly export the weight generated from

[net,tr]=train(net,p,t);

to your program (i.e. C++), you will end up getting different result as compared to results generated by MATLAB.

3) This behavior was resulted from the preprocessing function described in (1).

network generated from newff(p,t,2)

network with 2 nodes in hidden layer


Process Input 1 from the default value of processFcns 

Process Output 1 from the default value of processFcns
 4) Hence, to use the weight in C++, you have to scale your input before multiply with weights and rescale your output to get the real output values.
For instance, to scale the input using mapminmax, you can use

y = (ymax-ymin)*(x-xmin)/(xmax-xmin) + ymin;

to get the scaled input between ymin = -1 and ymax = 1.

Finally, to scale the output back to original unit of the target, you can use

x = (xmax-xmin)*(y-ymin)/(ymax-ymin) + xmin;

5) Alternatively, you can disable the scaling preprocessing function by execute either of these command

net=newff(p,t,2);
net.inputs{1}.processFcns = {'fixunknowns' 'removeconstantrows'};
net2.outputs{2}.processFcns = {'removeconstantrows'};
%net.inputs{1}.processFcns = {}; 
%net.outputs{2}.processFcns = {};

6) The weights value generated can be directly exported to your C++ program.

Enjoy!!

UPDATE

preprocessing: mapstd

To scale the input:
y = (x-xmean) * (ystd/xstd) + ymean

To scale the output:
x = (y-ymean) * (xstd/ystd) + xmean

[sources: 1, 2]

No comments: