1 /** @file percstruct.h
2
3 @brief TPerceptronStructure header
4
5 @author Jakub Ad�mek
6 Last modified $Id: percstruct.h,v 1.5 2002/04/23 15:49:41 jakubadamek Exp $
7 */
8
9 #ifndef __BANG_PERCSTRUCT_H__
10 #define __BANG_PERCSTRUCT_H__
11
12 #include "math.h"
13 #include "time.h"
14 //#include "../../base/bing.h"
15
16 /// abstract neuron transfer function
17 typedef double TFloat2FloatFn (double,double);
18
19 #undef LABELEASYC
20 #define LABELEASYC(x,y) x y
21
22 /** @brief Represents the learning goal on which training process stops. */
STRUCT_BEGIN(TLearningGoal)23 STRUCT_BEGIN (TLearningGoal)
24 void Init () { error=0.01; evalError=0; time=600; epochs=1000; errorImprove=0.00001;
25 nErrorImprove=30; }
26 enum typeEnum { GT_NOTMET, GT_ERROR, GT_TIME, GT_EPOCHS, GT_ERROR_IMPROVEMENT, GT_EVAL_IMPROVEMENT };
27 /// which goal was met - typeEnum
28 LABELEASYC (CInt, goalMet);
29 /// desired result: error not greater than this
30 LABELEASYC (CFloat, error);
31 /// information only - error on evaluation set
32 LABELEASYC (CFloat, evalError);
33 /// max time in seconds
34 LABELEASYC (CInt, time);
35 /// max number of epochs
36 LABELEASYC (CInt, epochs);
37 /** if the error (or evalError, if there is eval set)
38 doesn't improve enough for a number of epochs, stop your job */
39 LABELEASYC (CFloat, errorImprove);
40 /// if the error doesn't improve enough for a number of epochs, stop your job
41 LABELEASYC (CInt, nErrorImprove);
42 // bool operator < (const TLearningGoal &y) const { return true; }
43 STRUCT_END (TLearningGoal)
44
45 static const TEnumString goalMetString = "Nothing;Hurrah!;Time limit crossed;Too many epochs;"
46 "Error doesn't improve for too long;Generalization optimum on evaluation set reached";
47
48 class CPerceptronNN;
49
50 /// forward connections restriction (no restrictions exist for recurrent connections)
51 enum conRestrictEnum {
52 /// connections are only between neurons layer i -> layer i+1
53 CR_LAYERED,
54 /// any connections between neurons i -> j, i < j
55 CR_NONE
56 };
57
58 static const TEnumString CRs = "layered;none";
59
60 enum TTrainProcedures {
61 // simply lowers weights by a part of negative gradient
62 TP_GRADIENT_DESCENT,
63 // a special powerful algorithm by Rueger
64 TP_STABLE_CONJUGATE,
65 // slightly improved quazi-Newton Levenberg-Marquardt method
66 TP_MLM,
67 TP_LM,
68 // no learning: only reads the weights and allows to run the network
69 TP_RUN_ONLY
70 };
71
72 static const TEnumString TrainPs = "gradientDescent;stableConjugateGradient;modifiedLM;LM;runOnly";
73
74 enum TWeightInit {
75 // random initialization in <-n^.5;n^.5>, n is count of connections coming to the end neuron
76 WI_RANDOM,
77 WI_NGUYEN_WIDROW
78 };
79
80 static const TEnumString WIs = "random;nguyen-widrow";
81
82 enum TTransferFn {
83 TF_LOGSIG,
84 TF_LINEAR,
85 TF_TANSIG
86 };
87
88 static const TEnumString TFns = "logsig;linear;tansig";
89
90 /** @brief Structure of a perceptron network
91
92 Contains all info determining network structure. Used both by
93 CTrainingProcess and CPerceptron
94 */
STRUCT_BEGIN(TPerceptronStructure)95 STRUCT_BEGIN (TPerceptronStructure)
96 void Init() { conRestrict=CR_LAYERED; weightInitProcedure=WI_RANDOM;
97 trainProcedure=TP_GRADIENT_DESCENT; debug=0; }
98
99 /// see conRestrictEnum
100 CInt conRestrict;
101 /// see TWeightInit
102 CInt weightInitProcedure;
103 /// see TTrainProcedures
104 CInt trainProcedure;
105
106 /** @brief Parametres used in various methods */
STRUCT_BEGIN(TParams)107 STRUCT_BEGIN (TParams)
108 void Init () { learningRate=0.01; momentum=0.2; epsilon=0.00001; zeta=1.5; c=0.5;
109 h=0.005; mi=0.001; mi_i=2; mi_d=0.6; }
110 CFloat learningRate;
111 CFloat momentum;
112 /// node perturbation: the mini shift for derivation calculation
113 CFloat epsilon;
114 /// stable conjugate gradient: learning rate multiplicator
115 CFloat zeta;
116 /// stable conjugate gradient:
117 CFloat c;
118 /// modified L-M:
119 CFloat h;
120 /// modified L-M: the main parameter
121 CFloat mi;
122 /// modified L-M: increase in mi
123 CFloat mi_i;
124 /// modified L-M: decrease in mi
125 CFloat mi_d;
126 /** Minimal change to be used when adapting weights.
127 I am not really sure it improves learning, but you can set it to 0 at any time
128 to avoid use of it.
129 Not yet implemented. */
130 CFloat minErrorChange;
131 STRUCT_END (TParams)
132
133 /// parametres used in various methods
134 TParams par;
135 /// learning goal
136 TLearningGoal goal;
137
138 /// layerSizes[0] = input count, layerSizes[layerSizes.size()-1] = output count
139 TVectorInt layerSizes;
140 /// layer transfer functions .. layerTrFuncs[0] ignored - doesn't have sence for input layer
141 TVectorInt layerTrFuncs;
142
STRUCT_BEGIN(TNeuronRange)143 STRUCT_BEGIN (TNeuronRange)
144 void Init() { layer=0; from=0; to=-1; }
145 CInt from, to, layer;
146 STRUCT_END (TNeuronRange);
147
148 /** specifies a range of connections - neurons in layer[0] with indexes from[0]..to[0]
149 have connections to neurons in layer[1] with indexes from[1]..to[1] */
150 STRUCT_BEGIN (TRecurrentConnections)
151 TNeuronRange start, end;
152 STRUCT_END (TRecurrentConnections)
153 VECTORC (TRecurrentConnections, TAllRecurrentConnections)
154
155 TAllRecurrentConnections allRecurrentConnections;
156
157 /// log of training and evaluation errors
158 CString logErrors;
159 /// file name for final weights dump
160 CString dumpWeights;
161 CString dumpWeightsDelimiter;
162 /// where to dump network (backup)
163 CString dumpAll;
164 /// interval between backups
165 CInt dumpAllInterval;
166
167 /// if true, runs the weights perturbation after each backpropagation to prove the results are OK
168 CInt runWeightsPerturbation;
169
170 /// debug level - the higher the more messages are displayed
171 CInt debug;
172
173 /// prints the structure
174 virtual CXml *print_all () const;
175 /** Reads the structure. Returns error description or empty string.
176 The filename of the config file is used to find the path to the weights stream */
177 virtual CString read_all (CRox *xml);
178 STRUCT_END (TPerceptronStructure)
179
180 #endif
181
182