]> ruin.nu Git - germs.git/blob - fann/src/include/fann_data.h
Make it possible to build statically against the included fann library.
[germs.git] / fann / src / include / fann_data.h
1 /*
2 Fast Artificial Neural Network Library (fann)
3 Copyright (C) 2003 Steffen Nissen (lukesky@diku.dk)
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #ifndef __fann_data_h__
21 #define __fann_data_h__
22
23 #include <stdio.h>
24
25 /* Section: FANN Datatypes
26
27    The two main datatypes used in the fann library is <struct fann>, 
28    which represents an artificial neural network, and <struct fann_train_data>,
29    which represent training data.
30  */
31
32
33 /* Type: fann_type
34    fann_type is the type used for the weights, inputs and outputs of the neural network.
35    
36         fann_type is defined as a:
37         float - if you include fann.h or floatfann.h
38         double - if you include doublefann.h
39         int - if you include fixedfann.h (please be aware that fixed point usage is 
40                         only to be used during execution, and not during training).
41 */
42
43 /* Enum: fann_train_enum
44         The Training algorithms used when training on <struct fann_train_data> with functions like
45         <fann_train_on_data> or <fann_train_on_file>. The incremental training looks alters the weights
46         after each time it is presented an input pattern, while batch only alters the weights once after
47         it has been presented to all the patterns.
48
49         FANN_TRAIN_INCREMENTAL -  Standard backpropagation algorithm, where the weights are 
50                 updated after each training pattern. This means that the weights are updated many 
51                 times during a single epoch. For this reason some problems, will train very fast with 
52                 this algorithm, while other more advanced problems will not train very well.
53         FANN_TRAIN_BATCH -  Standard backpropagation algorithm, where the weights are updated after 
54                 calculating the mean square error for the whole training set. This means that the weights 
55                 are only updated once during a epoch. For this reason some problems, will train slower with 
56                 this algorithm. But since the mean square error is calculated more correctly than in 
57                 incremental training, some problems will reach a better solutions with this algorithm.
58         FANN_TRAIN_RPROP - A more advanced batch training algorithm which achieves good results 
59                 for many problems. The RPROP training algorithm is adaptive, and does therefore not 
60                 use the learning_rate. Some other parameters can however be set to change the way the 
61                 RPROP algorithm works, but it is only recommended for users with insight in how the RPROP 
62                 training algorithm works. The RPROP training algorithm is described by 
63                 [Riedmiller and Braun, 1993], but the actual learning algorithm used here is the 
64                 iRPROP- training algorithm which is described by [Igel and Husken, 2000] which 
65         is an variety of the standard RPROP training algorithm.
66         FANN_TRAIN_QUICKPROP - A more advanced batch training algorithm which achieves good results 
67                 for many problems. The quickprop training algorithm uses the learning_rate parameter 
68                 along with other more advanced parameters, but it is only recommended to change these 
69                 advanced parameters, for users with insight in how the quickprop training algorithm works.
70                 The quickprop training algorithm is described by [Fahlman, 1988].
71         
72         See also:
73                 <fann_set_training_algorithm>, <fann_get_training_algorithm>
74 */
75 enum fann_train_enum
76 {
77         FANN_TRAIN_INCREMENTAL = 0,
78         FANN_TRAIN_BATCH,
79         FANN_TRAIN_RPROP,
80         FANN_TRAIN_QUICKPROP
81 };
82
83 /* Constant: FANN_TRAIN_NAMES
84    
85    Constant array consisting of the names for the training algorithms, so that the name of an
86    training function can be received by:
87    (code)
88    char *name = FANN_TRAIN_NAMES[train_function];
89    (end)
90
91    See Also:
92       <fann_train_enum>
93 */
94 static char const *const FANN_TRAIN_NAMES[] = {
95         "FANN_TRAIN_INCREMENTAL",
96         "FANN_TRAIN_BATCH",
97         "FANN_TRAIN_RPROP",
98         "FANN_TRAIN_QUICKPROP"
99 };
100
101 /* Enums: fann_activationfunc_enum
102    
103         The activation functions used for the neurons during training. The activation functions
104         can either be defined for a group of neurons by <fann_set_activation_function_hidden> and
105         <fann_set_activation_function_output> or it can be defined for a single neuron by <fann_set_activation_function>.
106
107         The steepness of an activation function is defined in the same way by 
108         <fann_set_activation_steepness_hidden>, <fann_set_activation_steepness_output> and <fann_set_activation_steepness>.
109    
110    The functions are described with functions where:
111    * x is the input to the activation function,
112    * y is the output,
113    * s is the steepness and
114    * d is the derivation.
115
116    FANN_LINEAR - Linear activation function. 
117      * span: -inf < y < inf
118          * y = x*s, d = 1*s
119          * Can NOT be used in fixed point.
120
121    FANN_THRESHOLD - Threshold activation function.
122          * x < 0 -> y = 0, x >= 0 -> y = 1
123          * Can NOT be used during training.
124
125    FANN_THRESHOLD_SYMMETRIC - Threshold activation function.
126          * x < 0 -> y = 0, x >= 0 -> y = 1
127          * Can NOT be used during training.
128
129    FANN_SIGMOID - Sigmoid activation function.
130          * One of the most used activation functions.
131          * span: 0 < y < 1
132          * y = 1/(1 + exp(-2*s*x))
133          * d = 2*s*y*(1 - y)
134
135    FANN_SIGMOID_STEPWISE - Stepwise linear approximation to sigmoid.
136          * Faster than sigmoid but a bit less precise.
137
138    FANN_SIGMOID_SYMMETRIC - Symmetric sigmoid activation function, aka. tanh.
139          * One of the most used activation functions.
140          * span: -1 < y < 1
141          * y = tanh(s*x) = 2/(1 + exp(-2*s*x)) - 1
142          * d = s*(1-(y*y))
143
144    FANN_SIGMOID_SYMMETRIC - Stepwise linear approximation to symmetric sigmoid.
145          * Faster than symmetric sigmoid but a bit less precise.
146
147    FANN_GAUSSIAN - Gaussian activation function.
148          * 0 when x = -inf, 1 when x = 0 and 0 when x = inf
149          * span: 0 < y < 1
150          * y = exp(-x*s*x*s)
151          * d = -2*x*s*y*s
152
153    FANN_GAUSSIAN_SYMMETRIC - Symmetric gaussian activation function.
154          * -1 when x = -inf, 1 when x = 0 and 0 when x = inf
155          * span: -1 < y < 1
156          * y = exp(-x*s*x*s)*2-1
157          * d = -2*x*s*(y+1)*s
158          
159    FANN_ELLIOT - Fast (sigmoid like) activation function defined by David Elliott
160          * span: 0 < y < 1
161          * y = ((x*s) / 2) / (1 + |x*s|) + 0.5
162          * d = s*1/(2*(1+|x*s|)*(1+|x*s|))
163          
164    FANN_ELLIOT_SYMMETRIC - Fast (symmetric sigmoid like) activation function defined by David Elliott
165          * span: -1 < y < 1   
166          * y = (x*s) / (1 + |x*s|)
167          * d = s*1/((1+|x*s|)*(1+|x*s|))
168
169         FANN_LINEAR_PIECE - Bounded linear activation function.
170          * span: 0 < y < 1
171          * y = x*s, d = 1*s
172          
173         FANN_LINEAR_PIECE_SYMMETRIC - Bounded Linear activation function.
174          * span: -1 < y < 1
175          * y = x*s, d = 1*s
176          
177         See also:
178                 <fann_set_activation_function_hidden>,
179                 <fann_set_activation_function_output>
180 */
181 enum fann_activationfunc_enum
182 {
183         FANN_LINEAR = 0,
184         FANN_THRESHOLD,
185         FANN_THRESHOLD_SYMMETRIC,
186         FANN_SIGMOID,
187         FANN_SIGMOID_STEPWISE,
188         FANN_SIGMOID_SYMMETRIC,
189         FANN_SIGMOID_SYMMETRIC_STEPWISE,
190         FANN_GAUSSIAN,
191         FANN_GAUSSIAN_SYMMETRIC,
192         /* Stepwise linear approximation to gaussian.
193          * Faster than gaussian but a bit less precise.
194          * NOT implemented yet.
195          */
196         FANN_GAUSSIAN_STEPWISE,
197         FANN_ELLIOT,
198         FANN_ELLIOT_SYMMETRIC,
199         FANN_LINEAR_PIECE,
200         FANN_LINEAR_PIECE_SYMMETRIC
201 };
202
203 /* Constant: FANN_ACTIVATIONFUNC_NAMES
204    
205    Constant array consisting of the names for the activation function, so that the name of an
206    activation function can be received by:
207    (code)
208    char *name = FANN_ACTIVATIONFUNC_NAMES[activation_function];
209    (end)
210
211    See Also:
212       <fann_activationfunc_enum>
213 */
214 static char const *const FANN_ACTIVATIONFUNC_NAMES[] = {
215         "FANN_LINEAR",
216         "FANN_THRESHOLD",
217         "FANN_THRESHOLD_SYMMETRIC",
218         "FANN_SIGMOID",
219         "FANN_SIGMOID_STEPWISE",
220         "FANN_SIGMOID_SYMMETRIC",
221         "FANN_SIGMOID_SYMMETRIC_STEPWISE",
222         "FANN_GAUSSIAN",
223         "FANN_GAUSSIAN_SYMMETRIC",
224         "FANN_GAUSSIAN_STEPWISE",
225         "FANN_ELLIOT",
226         "FANN_ELLIOT_SYMMETRIC",
227         "FANN_LINEAR_PIECE",
228         "FANN_LINEAR_PIECE_SYMMETRIC"
229 };
230
231 /* Enum: fann_errorfunc_enum
232         Error function used during training.
233         
234         FANN_ERRORFUNC_LINEAR - Standard linear error function.
235         FANN_ERRORFUNC_TANH - Tanh error function, usually better 
236                 but can require a lower learning rate. This error function agressively targets outputs that
237                 differ much from the desired, while not targetting outputs that only differ a little that much.
238                 This activation function is not recommended for cascade training and incremental training.
239
240         See also:
241                 <fann_set_train_error_function>, <fann_get_train_error_function>
242 */
243 enum fann_errorfunc_enum
244 {
245         FANN_ERRORFUNC_LINEAR = 0,
246         FANN_ERRORFUNC_TANH
247 };
248
249 /* Constant: FANN_ERRORFUNC_NAMES
250    
251    Constant array consisting of the names for the training error functions, so that the name of an
252    error function can be received by:
253    (code)
254    char *name = FANN_ERRORFUNC_NAMES[error_function];
255    (end)
256
257    See Also:
258       <fann_errorfunc_enum>
259 */
260 static char const *const FANN_ERRORFUNC_NAMES[] = {
261         "FANN_ERRORFUNC_LINEAR",
262         "FANN_ERRORFUNC_TANH"
263 };
264
265 /* Enum: fann_stopfunc_enum
266         Stop criteria used during training.
267
268         FANN_STOPFUNC_MSE - Stop criteria is Mean Square Error (MSE) value.
269         FANN_STOPFUNC_BIT - Stop criteria is number of bits that fail. The number of bits; means the
270                 number of output neurons which differ more than the bit fail limit 
271                 (see <fann_get_bit_fail_limit>, <fann_set_bit_fail_limit>). 
272                 The bits are counted in all of the training data, so this number can be higher than
273                 the number of training data.
274
275         See also:
276                 <fann_set_train_stop_function>, <fann_get_train_stop_function>
277 */
278 enum fann_stopfunc_enum
279 {
280         FANN_STOPFUNC_MSE = 0,
281         FANN_STOPFUNC_BIT
282 };
283
284 /* Constant: FANN_STOPFUNC_NAMES
285    
286    Constant array consisting of the names for the training stop functions, so that the name of a
287    stop function can be received by:
288    (code)
289    char *name = FANN_STOPFUNC_NAMES[stop_function];
290    (end)
291
292    See Also:
293       <fann_stopfunc_enum>
294 */
295 static char const *const FANN_STOPFUNC_NAMES[] = {
296         "FANN_STOPFUNC_MSE",
297         "FANN_STOPFUNC_BIT"
298 };
299
300 /* forward declarations for use with the callback */
301 struct fann;
302 struct fann_train_data;
303 /* Type: fann_callback_type
304    This callback function can be called during training when using <fann_train_on_data>, 
305    <fann_train_on_file> or <fann_cascade_train>.
306         
307         >typedef int (FANN_API * fann_callback_type) (struct fann *ann, struct fann_train_data *train, 
308         >                                                                                         unsigned int max_epochs, 
309         >                                             unsigned int epochs_between_reports, 
310         >                                             float desired_error, unsigned int epochs);
311         
312         The callback can be set by using <fann_set_callback> and is very usefull for doing custom 
313         things during training. It is recommended to use this function when implementing custom 
314         training procedures, or when visualizing the training in a GUI etc. The parameters which the
315         callback function takes is the parameters given to the <fann_train_on_data>, plus an epochs
316         parameter which tells how many epochs the training have taken so far.
317         
318         The callback function should return an integer, if the callback function returns -1, the training
319         will terminate.
320         
321         Example of a callback function:
322                 >int FANN_API test_callback(struct fann *ann, struct fann_train_data *train,
323                 >                                           unsigned int max_epochs, unsigned int epochs_between_reports, 
324                 >                                           float desired_error, unsigned int epochs)
325                 >{
326                 >       printf("Epochs     %8d. MSE: %.5f. Desired-MSE: %.5f\n", epochs, fann_get_MSE(ann), desired_error);
327                 >       return 0;
328                 >}
329         
330         See also:
331                 <fann_set_callback>, <fann_train_on_data>
332  */ 
333 FANN_EXTERNAL typedef int (FANN_API * fann_callback_type) (struct fann *ann, struct fann_train_data *train, 
334                                                                                                                    unsigned int max_epochs, 
335                                                                                                                    unsigned int epochs_between_reports, 
336                                                                                                                    float desired_error, unsigned int epochs);
337
338
339 /* ----- Data structures -----
340  * No data within these structures should be altered directly by the user.
341  */
342
343 struct fann_neuron
344 {
345         /* Index to the first and last connection
346          * (actually the last is a past end index)
347          */
348         unsigned int first_con;
349         unsigned int last_con;
350         /* The sum of the inputs multiplied with the weights */
351         fann_type sum;
352         /* The value of the activation function applied to the sum */
353         fann_type value;
354         /* The steepness of the activation function */
355         fann_type activation_steepness;
356         /* Used to choose which activation function to use */
357         enum fann_activationfunc_enum activation_function;
358 #ifdef __GNUC__
359 } __attribute__ ((packed));
360 #else
361 };
362 #endif
363
364 /* A single layer in the neural network.
365  */
366 struct fann_layer
367 {
368         /* A pointer to the first neuron in the layer 
369          * When allocated, all the neurons in all the layers are actually
370          * in one long array, this is because we wan't to easily clear all
371          * the neurons at once.
372          */
373         struct fann_neuron *first_neuron;
374
375         /* A pointer to the neuron past the last neuron in the layer */
376         /* the number of neurons is last_neuron - first_neuron */
377         struct fann_neuron *last_neuron;
378 };
379
380 /* Struct: struct fann_error
381    
382         Structure used to store error-related information, both
383         <struct fann> and <struct fann_train_data> can be casted to this type.
384         
385         See also:
386                 <fann_set_error_log>, <fann_get_errno>
387 */
388 struct fann_error
389 {
390         enum fann_errno_enum errno_f;
391         FILE *error_log;
392         char *errstr;
393 };
394
395
396 /*      Struct: struct fann
397         The fast artificial neural network(fann) structure.
398
399         Data within this structure should never be accessed directly, but only by using the
400         *fann_get_...* and *fann_set_...* functions.
401
402         The fann structure is created using one of the *fann_create_...* functions and each of
403         the functions which operates on the structure takes *struct fann * ann* as the first parameter.
404
405         See also:
406                 <fann_create_standard>, <fann_destroy>
407  */
408 struct fann
409 {
410         /* The type of error that last occured. */
411         enum fann_errno_enum errno_f;
412
413         /* Where to log error messages. */
414         FILE *error_log;
415
416         /* A string representation of the last error. */
417         char *errstr;
418
419         /* the learning rate of the network */
420         float learning_rate;
421
422         /* The learning momentum used for backpropagation algorithm. */
423         float learning_momentum;
424
425         /* the connection rate of the network
426          * between 0 and 1, 1 meaning fully connected
427          */
428         float connection_rate;
429
430         /* is 1 if shortcut connections are used in the ann otherwise 0
431          * Shortcut connections are connections that skip layers.
432          * A fully connected ann with shortcut connections are a ann where
433          * neurons have connections to all neurons in all later layers.
434          */
435         unsigned int shortcut_connections;
436
437         /* pointer to the first layer (input layer) in an array af all the layers,
438          * including the input and outputlayers 
439          */
440         struct fann_layer *first_layer;
441
442         /* pointer to the layer past the last layer in an array af all the layers,
443          * including the input and outputlayers 
444          */
445         struct fann_layer *last_layer;
446
447         /* Total number of neurons.
448          * very usefull, because the actual neurons are allocated in one long array
449          */
450         unsigned int total_neurons;
451
452         /* Number of input neurons (not calculating bias) */
453         unsigned int num_input;
454
455         /* Number of output neurons (not calculating bias) */
456         unsigned int num_output;
457
458         /* The weight array */
459         fann_type *weights;
460
461         /* The connection array */
462         struct fann_neuron **connections;
463
464         /* Used to contain the errors used during training
465          * Is allocated during first training session,
466          * which means that if we do not train, it is never allocated.
467          */
468         fann_type *train_errors;
469
470         /* Training algorithm used when calling fann_train_on_..
471          */
472         enum fann_train_enum training_algorithm;
473
474 #ifdef FIXEDFANN
475         /* the decimal_point, used for shifting the fix point
476          * in fixed point integer operatons.
477          */
478         unsigned int decimal_point;
479
480         /* the multiplier, used for multiplying the fix point
481          * in fixed point integer operatons.
482          * Only used in special cases, since the decimal_point is much faster.
483          */
484         unsigned int multiplier;
485
486         /* When in choosen (or in fixed point), the sigmoid function is
487          * calculated as a stepwise linear function. In the
488          * activation_results array, the result is saved, and in the
489          * two values arrays, the values that gives the results are saved.
490          */
491         fann_type sigmoid_results[6];
492         fann_type sigmoid_values[6];
493         fann_type sigmoid_symmetric_results[6];
494         fann_type sigmoid_symmetric_values[6];
495 #endif
496
497         /* Total number of connections.
498          * very usefull, because the actual connections
499          * are allocated in one long array
500          */
501         unsigned int total_connections;
502
503         /* used to store outputs in */
504         fann_type *output;
505
506         /* the number of data used to calculate the mean square error.
507          */
508         unsigned int num_MSE;
509
510         /* the total error value.
511          * the real mean square error is MSE_value/num_MSE
512          */
513         float MSE_value;
514
515         /* The number of outputs which would fail (only valid for classification problems)
516          */
517         unsigned int num_bit_fail;
518
519         /* The maximum difference between the actual output and the expected output 
520          * which is accepted when counting the bit fails.
521          * This difference is multiplied by two when dealing with symmetric activation functions,
522          * so that symmetric and not symmetric activation functions can use the same limit.
523          */
524         fann_type bit_fail_limit;
525
526         /* The error function used during training. (default FANN_ERRORFUNC_TANH)
527          */
528         enum fann_errorfunc_enum train_error_function;
529         
530         /* The stop function used during training. (default FANN_STOPFUNC_MSE)
531         */
532         enum fann_stopfunc_enum train_stop_function;
533
534         /* The callback function used during training. (default NULL)
535         */
536         fann_callback_type callback;
537
538         /* Variables for use with Cascade Correlation */
539
540         /* The error must change by at least this
541          * fraction of its old value to count as a
542          * significant change.
543          */
544         float cascade_output_change_fraction;
545
546         /* No change in this number of epochs will cause
547          * stagnation.
548          */
549         unsigned int cascade_output_stagnation_epochs;
550
551         /* The error must change by at least this
552          * fraction of its old value to count as a
553          * significant change.
554          */
555         float cascade_candidate_change_fraction;
556
557         /* No change in this number of epochs will cause
558          * stagnation.
559          */
560         unsigned int cascade_candidate_stagnation_epochs;
561
562         /* The current best candidate, which will be installed.
563          */
564         unsigned int cascade_best_candidate;
565
566         /* The upper limit for a candidate score
567          */
568         fann_type cascade_candidate_limit;
569
570         /* Scale of copied candidate output weights
571          */
572         fann_type cascade_weight_multiplier;
573         
574         /* Maximum epochs to train the output neurons during cascade training
575          */
576         unsigned int cascade_max_out_epochs;
577         
578         /* Maximum epochs to train the candidate neurons during cascade training
579          */
580         unsigned int cascade_max_cand_epochs;   
581
582         /* An array consisting of the activation functions used when doing
583          * cascade training.
584          */
585         enum fann_activationfunc_enum *cascade_activation_functions;
586         
587         /* The number of elements in the cascade_activation_functions array.
588         */
589         unsigned int cascade_activation_functions_count;
590         
591         /* An array consisting of the steepnesses used during cascade training.
592         */
593         fann_type *cascade_activation_steepnesses;
594
595         /* The number of elements in the cascade_activation_steepnesses array.
596         */
597         unsigned int cascade_activation_steepnesses_count;
598         
599         /* The number of candidates of each type that will be present.
600          * The actual number of candidates is then 
601          * cascade_activation_functions_count * 
602          * cascade_activation_steepnesses_count *
603          * cascade_num_candidate_groups
604         */
605         unsigned int cascade_num_candidate_groups;
606         
607         /* An array consisting of the score of the individual candidates,
608          * which is used to decide which candidate is the best
609          */
610         fann_type *cascade_candidate_scores;
611         
612         /* The number of allocated neurons during cascade correlation algorithms.
613          * This number might be higher than the actual number of neurons to avoid
614          * allocating new space too often.
615          */
616         unsigned int total_neurons_allocated;
617
618         /* The number of allocated connections during cascade correlation algorithms.
619          * This number might be higher than the actual number of neurons to avoid
620          * allocating new space too often.
621          */
622         unsigned int total_connections_allocated;
623
624         /* Variables for use with Quickprop training */
625
626         /* Decay is used to make the weights not go so high */
627         float quickprop_decay;
628
629         /* Mu is a factor used to increase and decrease the stepsize */
630         float quickprop_mu;
631
632         /* Variables for use with with RPROP training */
633
634         /* Tells how much the stepsize should increase during learning */
635         float rprop_increase_factor;
636
637         /* Tells how much the stepsize should decrease during learning */
638         float rprop_decrease_factor;
639
640         /* The minimum stepsize */
641         float rprop_delta_min;
642
643         /* The maximum stepsize */
644         float rprop_delta_max;
645
646         /* The initial stepsize */
647         float rprop_delta_zero;
648         
649         /* Used to contain the slope errors used during batch training
650          * Is allocated during first training session,
651          * which means that if we do not train, it is never allocated.
652          */
653         fann_type *train_slopes;
654
655         /* The previous step taken by the quickprop/rprop procedures.
656          * Not allocated if not used.
657          */
658         fann_type *prev_steps;
659
660         /* The slope values used by the quickprop/rprop procedures.
661          * Not allocated if not used.
662          */
663         fann_type *prev_train_slopes;
664         
665         /* The last delta applied to a connection weight.
666          * This is used for the momentum term in the backpropagation algorithm.
667          * Not allocated if not used.    
668          */
669         fann_type *prev_weights_deltas;
670         
671 };
672
673 #endif