]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/sim.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / sim.m
1 ## Copyright (C) 2006 Michel D. Schmid  <michaelschmid@users.sourceforge.net>
2 ##
3 ##
4 ## This program is free software; you can redistribute it and/or modify it
5 ## under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
7 ## any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but
10 ## WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ## General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; see the file COPYING.  If not, see
16 ## <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {@var{netoutput} =} sim (@var{net}, @var{mInput})
20 ## @code{sim} is usuable to simulate a before defined neural network.
21 ## @code{net} is created with newff(@dots{}) and @var{mInput} should be the
22 ## corresponding input data set!
23 ## @end deftypefn
24
25 ## Author: Michel D. Schmid
26
27
28 ## Comments: see in "A neural network toolbox for Octave User's Guide" [4]
29 ##  for variable naming... there have inputs or targets only one letter,
30 ## e.g. for inputs is written P. To write a program, this is stupid, you can't
31 ##  search for 1 letter variable... that's why it is written here like Pp, or Tt
32 ## instead only P or T.
33
34 function [netoutput] = sim(net,mInput)
35
36   ## check range of input arguments
37   error(nargchk(2,2,nargin))
38
39   ## check input args
40   ## check "net", must be a net structure
41   if !__checknetstruct(net)
42     error("Structure doesn't seem to be a neural network")
43   endif
44   ## check "mInput", must have defined size
45   [nRows, nColumns] = size(mInput);
46   if (nRows != net.inputs{1}.size)
47     error(["Simulation input data must have: " num2str(net.inputs{1}.size) " rows."])
48   endif
49
50   ## first get weights...
51   IW = net.IW{1};
52   b1 = net.b{1};
53   b1 = repmat(b1,1,size(mInput,2));
54   nLoops = net.numLayers;
55   for i=1:nLoops
56
57     trf = net.layers{i}.transferFcn;
58     ## calculate the outputs for each layer from input to output
59
60     if i==1
61       Nn{i,1} = IW*mInput + b1;
62     else
63       LWx = net.LW{i,i-1};
64       bx = net.b{i};
65       bx = repmat(bx,1,size(Aa{i-1,1},2));
66       Nn{i,1} = LWx*Aa{i-1,1} + bx;
67     endif
68
69     switch(trf)
70       case "tansig"
71         Aa{i,1} = tansig(Nn{i,1});
72       case "purelin"
73         Aa{i,1} = purelin(Nn{i,1});
74       case "logsig"
75         Aa{i,1} = logsig(Nn{i,1});
76       otherwise
77         error(["sim:Unknown transfer fucntion: " trf "!"]);
78     endswitch
79   endfor
80
81   netoutput = Aa{i,1};
82
83 endfunction
84