]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/__newnetwork.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / __newnetwork.m
1 ## Copyright (C) 2005 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{net}} = __newnetwork(@var{numInputs},@var{numLayers},@var{numOutputs},@var{networkType})
20 ## @code{__newnetwork} create a custom 'zero'-network
21 ##
22 ##
23 ## @example
24 ## net = __newnetwork(numInputs,numLayers,numOutputs,networkType)
25 ##
26 ## numInputs : number of input vectors, actually only 1 allowed
27 ## numLayers : number of layers
28 ## numOutputs: number of output vectors, actually only 1 allowed
29 ## networkType: e.g. feed-forward-network "newff"
30 ## @end example
31 ##
32 ## @example
33 ## net = __newnetwork(1,2,1,"newff")
34 ##       1 input layer, two hidden layers, one output layer
35 ##       and the network type
36 ## @end example
37 ##
38 ## @noindent
39 ## @end deftypefn
40
41 ## Author: Michel D. Schmid
42
43 function net = __newnetwork(numInputs,numLayers,numOutputs,networkType)
44
45   ## check range of input arguments
46   error(nargchk(4,4,nargin))
47
48   ## check input args
49   if ( !isposint(numInputs) )
50     error("network: at least 1 input must be defined! ")
51     # this can't happen actually, only one is allowed and this
52     # one is hard coded
53   elseif ( !isposint(numLayers) )
54     error("network: at least 1 hidden- and one output layer must be defined! ")
55   endif
56   ## second check for numLayers... must be at least "2" for the
57   ## newff, this means at least 1 hidden and 1 output layer
58   if (strcmp(networkType,"newff")  && (numLayers<2))
59     error("network: not enough layers are defined! ")
60   endif
61
62   ## define network type
63   net.networkType = networkType;
64
65   ## ZERO NETWORK
66   net.numInputs = 0;
67   net.numLayers = 0;
68   net.numInputDelays = 0;
69   net.numLayerDelays = 0;
70   # the next five parameters aren't used till now, they are used
71   # only for matlab nnet type compatibility ==> saveMLPStruct
72   net.biasConnect = [];   # not used parameter till now
73   net.inputConnect = [];  # not used parameter till now
74   net.layerConnect = [];  # not used parameter till now
75   net.outputConnect = []; # not used parameter till now
76   net.targetConnect = []; # not used parameter till now
77   net.numOutputs = 0;
78   net.numTargets = 0;
79   net.inputs = cell(0,1);
80   net.layers = cell(0,1);
81   net.biases = cell(0,1);
82   net.inputWeights = cell(0,0);
83   net.layerWeights = cell(0,0);
84   net.outputs = cell(1,0);
85   net.targets = cell(1,0);
86   net.performFcn = "";
87   net.performParam = [];
88   net.trainFcn = "";
89   net.trainParam = [];
90   net.IW = {};
91   net.LW = {};
92   net.b = cell(0,1);
93   net.userdata.note = "Put your custom network information here.";
94
95
96   ## ARCHITECTURE
97   
98   ## define everything with "inputs"
99   net.numInputs = numInputs;
100   ## actually, it's only possible to have "one" input vector
101   net.inputs{1,1}.range = [0 0];
102   net.inputs{1,1}.size = 0;
103   net.inputs{1,1}.userdata = "Put your custom informations here!";
104   
105   ## define everything with "layers"
106   net.numLayers = numLayers;
107   net = newLayers(net,numLayers);
108
109   ## define unused variables, must be defined for saveMLPStruct
110   net.biasConnect = [0; 0];
111   net.inputConnect = [0; 0];
112   net.layerConnect = [0 0; 0 0];
113   net.outputConnect = [0 0];
114   net.targetConnect = [0 0];
115   net.numInputDelays = 0;
116   net.numLayerDelays = 0;
117
118   ## define everything with "outputs"
119   net.numOutputs = numOutputs;
120   net.outputs = cell(1,numLayers);
121   for i=1:numLayers
122     if (i==numLayers)
123       net.outputs{i}.size = 1; # nothing else allowed till now
124       net.outputs{i}.userdata = "Put your custom informations here!";
125     else
126       net.outputs{i} = [];
127     endif
128   endfor
129
130   ## define everything with "biases"
131   net = newBiases(net,numLayers);
132
133
134
135 #=====================================================
136 #
137 # Additional ARCHITECTURE Functions
138 #
139 #=====================================================
140   function net = newLayers(net,numLayers)
141
142     ## check range of input arguments
143     error(nargchk(2,2,nargin))
144
145     ## check type of arguments
146     if ( !isscalar(numLayers) || !isposint(numLayers) )
147       error("second argument must be a positive integer scalar value!")
148     endif
149     if ( !__checknetstruct(net) )
150       error("first argument must be a network structure!")
151     endif
152
153     for iRuns=1:numLayers
154       net.layers{iRuns,1}.dimension = 0;
155       net.layers{iRuns,1}.netInputFcn = "";
156       net.layers{iRuns,1}.size = 0;
157 ### TODO: test with newff      net.layers{iRuns,1}.transferFcn = "tansig";
158       net.layers{iRuns,1}.transferFcn = "";
159       net.layers{iRuns,1}.userdata = "Put your custom informations here!";
160     endfor
161
162   endfunction
163
164 #-----------------------------------------------------
165
166   function net = newBiases(net,numLayers)
167
168     ## check range of input arguments
169     error(nargchk(2,2,nargin))
170
171     ## check type of arguments
172     if ( !isscalar(numLayers) || !isposint(numLayers) )
173       error("second argument must be a positive integer scalar value!")
174     endif
175     if ( !isstruct(net) )
176       error("first argument must be a network structure!")
177     endif
178
179     for iRuns=1:numLayers
180       net.biases{iRuns,1}.learn = 1;
181       net.biases{iRuns,1}.learnFcn = "";
182       net.biases{iRuns,1}.learnParam = "undefined...";
183       net.biases{iRuns,1}.size = 0;
184       net.biases{iRuns,1}.userdata = "Put your custom informations here!";
185     endfor
186
187   endfunction
188
189 # ================================================================
190 #
191 #             END Additional ARCHITECTURE Functions
192 #
193 # ================================================================
194
195 endfunction