]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/__init.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / __init.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} = __init (@var{net})
20 ## @code{__init} initializes a neural network. This will be done
21 ## with the function @code{rand} from octave.
22 ##
23 ## @example
24 ## net = __init(net);
25 ## @end example
26 ##
27 ## This function takes the octave function "rand" to init the 
28 ## neural network weights.
29 ##
30 ## @noindent
31 ## @end deftypefn
32
33
34 ## Author: Michel D. Schmid
35
36 function net=__init(net)
37
38   ## check number of inputs
39   error(nargchk(1,1,nargin));
40
41   ## check input
42   if ( !__checknetstruct(net) )
43     error("__init: wrong argument type, must be a structure!");
44   endif
45
46
47   if (strcmp(net.networkType,"newff"))
48
49     ## init with random numbers between +-1
50     ## input weight layer
51     mRand = rand(net.layers{1}.size,net.inputs{1}.size);
52     net.IW{1} = mRand*2-1;
53
54     ## hidden layers
55     nLayers = net.numLayers;
56     for i=2:nLayers
57       mRand = rand(net.layers{i}.size,net.layers{i-1}.size);
58       net.LW{i,i-1} = mRand*2-1;
59     endfor
60     for i=1:nLayers
61       mRand = rand(net.biases{i}.size,1);
62       net.b{i} = mRand*2-1;
63     endfor
64   elseif (strcmp(net.networkType,"newp"))
65
66     ## init with zeros
67     inputRows = size(net.inputs{1,1}.range,1);
68     net.IW{1} = zeros(inputRows,1);
69     net.b{1} = zeros(1,1);
70   endif
71
72   ## warn user of constant inputs
73   for i=1:net.numInputs
74     prange = net.inputs{i}.range;
75     if (any(prange(:,1) == prange(:,2)))
76       fprintf("\n")
77       fprintf("** Warning in INIT\n")
78       fprintf("** Network net.inputs{%g}.range has a row with equal min and max values.\n",i)
79       fprintf("** Constant inputs do not provide useful information.\n")
80       fprintf("\n")
81     end
82   end
83
84 endfunction