]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/newp.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / newp.m
1 ## Copyright (C) 2007 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, write to the Free
16 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 ## 02110-1301, USA.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{net}} = newp (@var{Pr},@var{ss},@var{transFunc},@var{learnFunc})
21 ## @code{newp} create a perceptron
22 ##
23 ## @example
24 ## PLEASE DON'T USE THIS FUNCTIONS, IT'S STILL NOT FINISHED!
25 ## =========================================================
26 ## @end example
27 ## @example
28 ## Pr - R x 2 matrix of min and max values for R input elements
29 ## ss - a scalar value with the number of neurons
30 ## transFunc - a string with the transfer function
31 ##       default = "hardlim"
32 ## learnFunc - a string with the learning function
33 ##       default = "learnp"
34 ## @end example
35 ##
36 ##
37 ## @end deftypefn
38
39 ## @seealso{}
40
41 ## Author: Michel D. Schmid
42
43 function net = newp(Pr,ss,transFunc,learnFunc)
44
45   ## initial descriptipn
46   ##  newp(Pr,ss,transFunc,learnFunc)
47   ##  * Pr is a nx2 matrix with min and max values of standardized inputs
48   ##    Pr means: p-range
49   ##  * ss is a scalar value which describes the number of neurons
50   ##    of output neurons
51   ##  * transFunc is the transfer function, standard is "hardlim"
52   ##  * learnFunc is the learning function, standard is "learnp"
53
54   ## check range of input arguments
55   error(nargchk(1,4,nargin))
56
57   ## set defaults
58   if (nargin <2)
59     ss = 1; # means one neuron
60   endif
61   if (nargin <3)
62     transFunc = "hardlim";
63   endif
64   if (nargin <4)
65     learnFunc = "learnp";
66   endif
67
68   ## check input args
69   checkInputArgs(Pr,ss);
70
71 #   ## get number of layers (without input layer)
72 #   nLayers = length(ss);
73
74   ## Standard architecture of neural network
75   net = __newnetwork(1,1,1,"newp");
76   ## description:
77   ##    first argument: number of inputs, nothing else allowed till now
78   ## it's not the same like the number of neurons in this input
79   ## second argument: number of layers, including output layer
80   ## third argument: number of outputs, nothing else allowed till now
81   ## it's not the same like the number of neurons in this output
82   ## fourth argument: network type
83
84
85   ## set inputs with limit of only ONE input
86   net.inputs{1}.range = Pr;
87   [nRows, nColumns] = size(Pr);
88   net.inputs{1}.size = nRows;
89
90   ## set size of IW
91   net.IW{1,1} = zeros(1,nRows);
92   ## set number of bias, one per layer
93   net.b{iBiases,1} = 0;
94
95   ## define everything with "layers"
96   net.numLayers = ss(end);
97   net.layers = cell(1,1);
98   net.layers{1}.size = ss(end);
99   net.layers{1}.transFcn = transFunc;
100   ## next row of code is only for MATLAB(TM) compatibility
101   ## I never used this the last 4 years ...
102   net.targets{i}.userdata = "Put your custom informations here!";
103
104   ## performance function
105   net.performFnc = "mae";
106
107   ## learning
108   net.biases{1}.learnFcn = learnFunc;
109   net.inputWeights{1,1}.learnFcn = learnFunc;
110
111   ## adaption
112   net.adaptFcn = "trains";
113
114   ## Training
115   net.trainFcn = "trainc";
116
117   ## Initialization
118   net = __init(net);
119
120 # ======================================================
121 #
122 # additional check functions...
123 #
124 # ======================================================
125   function checkInputArgs(Pr,ss)
126     
127     ## check if Pr has correct format
128     if !isreal(Pr) | (size(Pr,2)!=2)
129       error("Input ranges must be a two column matrix!")
130     endif
131     if any(Pr(:,1) > Pr(:,2)) # check if numbers in the second column are larger as in the first one
132       error("Input ranges has values in the second column larger as in the same row of the first column.")
133     endif
134
135     ## check if ss has correct format, must be a scalar value
136     if ( (size(ss,1)!=1) || (size(ss,2)!=1))
137       error("Layer sizes is not a scalar value.")
138     endif
139
140   endfunction
141
142 # ========================================================  
143
144
145 endfunction