moo_fann 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,613 @@
1
+ /*
2
+ Fast Artificial Neural Network Library (fann)
3
+ Copyright (C) 2003-2012 Steffen Nissen (sn@leenissen.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
+ /* This file defines the user interface to the fann library.
21
+ It is included from fixedfann.h, floatfann.h and doublefann.h and should
22
+ NOT be included directly. If included directly it will react as if
23
+ floatfann.h was included.
24
+ */
25
+
26
+ /* Section: FANN Creation/Execution
27
+
28
+ The FANN library is designed to be very easy to use.
29
+ A feedforward ann can be created by a simple <fann_create_standard> function, while
30
+ other ANNs can be created just as easily. The ANNs can be trained by <fann_train_on_file>
31
+ and executed by <fann_run>.
32
+
33
+ All of this can be done without much knowledge of the internals of ANNs, although the ANNs created will
34
+ still be powerfull and effective. If you have more knowledge about ANNs, and desire more control, almost
35
+ every part of the ANNs can be parametized to create specialized and highly optimal ANNs.
36
+ */
37
+ /* Group: Creation, Destruction & Execution */
38
+
39
+ #ifndef FANN_INCLUDE
40
+ /* just to allow for inclusion of fann.h in normal stuations where only floats are needed */
41
+ #ifdef FIXEDFANN
42
+ #include "fixedfann.h"
43
+ #else
44
+ #include "floatfann.h"
45
+ #endif /* FIXEDFANN */
46
+
47
+ #else
48
+
49
+ /* COMPAT_TIME REPLACEMENT */
50
+ #ifndef _WIN32
51
+ #include <sys/time.h>
52
+ #else /* _WIN32 */
53
+ #if !defined(_MSC_EXTENSIONS) && !defined(_INC_WINDOWS)
54
+ extern unsigned long __stdcall GetTickCount(void);
55
+
56
+ #else /* _MSC_EXTENSIONS */
57
+ #define WIN32_LEAN_AND_MEAN
58
+ #include <windows.h>
59
+ #endif /* _MSC_EXTENSIONS */
60
+ #endif /* _WIN32 */
61
+
62
+ #ifndef __fann_h__
63
+ #define __fann_h__
64
+
65
+ #ifdef __cplusplus
66
+ extern "C"
67
+ {
68
+
69
+ #ifndef __cplusplus
70
+ } /* to fool automatic indention engines */
71
+ #endif
72
+ #endif /* __cplusplus */
73
+
74
+ #ifndef NULL
75
+ #define NULL 0
76
+ #endif /* NULL */
77
+
78
+ /* ----- Macros used to define DLL external entrypoints ----- */
79
+ /*
80
+ DLL Export, import and calling convention for Windows.
81
+ Only defined for Microsoft VC++ FANN_EXTERNAL indicates
82
+ that a function will be exported/imported from a dll
83
+ FANN_API ensures that the DLL calling convention
84
+ will be used for a function regardless of the calling convention
85
+ used when compiling.
86
+
87
+ For a function to be exported from a DLL its prototype and
88
+ declaration must be like this:
89
+ FANN_EXTERNAL void FANN_API function(char *argument)
90
+
91
+ The following ifdef block is a way of creating macros which
92
+ make exporting from a DLL simple. All files within a DLL are
93
+ compiled with the FANN_DLL_EXPORTS symbol defined on the
94
+ command line. This symbol should not be defined on any project
95
+ that uses this DLL. This way any other project whose source
96
+ files include this file see FANN_EXTERNAL functions as being imported
97
+ from a DLL, whereas a DLL sees symbols defined with this
98
+ macro as being exported which makes calls more efficient.
99
+ The __stdcall calling convention is used for functions in a
100
+ windows DLL.
101
+
102
+ The callback functions for fann_set_callback must be declared as FANN_API
103
+ so the DLL and the application program both use the same
104
+ calling convention.
105
+ */
106
+
107
+ /*
108
+ The following sets the default for MSVC++ 2003 or later to use
109
+ the fann dll's. To use a lib or fixedfann.c, floatfann.c or doublefann.c
110
+ with those compilers FANN_NO_DLL has to be defined before
111
+ including the fann headers.
112
+ The default for previous MSVC compilers such as VC++ 6 is not
113
+ to use dll's. To use dll's FANN_USE_DLL has to be defined before
114
+ including the fann headers.
115
+ */
116
+ #if (_MSC_VER > 1300)
117
+ #ifndef FANN_NO_DLL
118
+ #define FANN_USE_DLL
119
+ #endif /* FANN_USE_LIB */
120
+ #endif /* _MSC_VER */
121
+ #if defined(_MSC_VER) && (defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS))
122
+ #ifdef FANN_DLL_EXPORTS
123
+ #define FANN_EXTERNAL __declspec(dllexport)
124
+ #else /* */
125
+ #define FANN_EXTERNAL __declspec(dllimport)
126
+ #endif /* FANN_DLL_EXPORTS*/
127
+ #define FANN_API __stdcall
128
+ #else /* */
129
+ #define FANN_EXTERNAL
130
+ #define FANN_API
131
+ #endif /* _MSC_VER */
132
+ /* ----- End of macros used to define DLL external entrypoints ----- */
133
+
134
+ #include "fann_error.h"
135
+ #include "fann_activation.h"
136
+ #include "fann_data.h"
137
+ #include "fann_internal.h"
138
+ #include "fann_train.h"
139
+ #include "fann_cascade.h"
140
+ #include "fann_io.h"
141
+
142
+ /* Function: fann_create_standard
143
+
144
+ Creates a standard fully connected backpropagation neural network.
145
+
146
+ There will be a bias neuron in each layer (except the output layer),
147
+ and this bias neuron will be connected to all neurons in the next layer.
148
+ When running the network, the bias nodes always emits 1.
149
+
150
+ To destroy a <struct fann> use the <fann_destroy> function.
151
+
152
+ Parameters:
153
+ num_layers - The total number of layers including the input and the output layer.
154
+ ... - Integer values determining the number of neurons in each layer starting with the
155
+ input layer and ending with the output layer.
156
+
157
+ Returns:
158
+ A pointer to the newly created <struct fann>.
159
+
160
+ Example:
161
+ > // Creating an ANN with 2 input neurons, 1 output neuron,
162
+ > // and two hidden neurons with 8 and 9 neurons
163
+ > struct fann *ann = fann_create_standard(4, 2, 8, 9, 1);
164
+
165
+ See also:
166
+ <fann_create_standard_array>, <fann_create_sparse>, <fann_create_shortcut>
167
+
168
+ This function appears in FANN >= 2.0.0.
169
+ */
170
+ FANN_EXTERNAL struct fann *FANN_API fann_create_standard(unsigned int num_layers, ...);
171
+
172
+ /* Function: fann_create_standard_array
173
+ Just like <fann_create_standard>, but with an array of layer sizes
174
+ instead of individual parameters.
175
+
176
+ Example:
177
+ > // Creating an ANN with 2 input neurons, 1 output neuron,
178
+ > // and two hidden neurons with 8 and 9 neurons
179
+ > unsigned int layers[4] = {2, 8, 9, 1};
180
+ > struct fann *ann = fann_create_standard_array(4, layers);
181
+
182
+ See also:
183
+ <fann_create_standard>, <fann_create_sparse>, <fann_create_shortcut>
184
+
185
+ This function appears in FANN >= 2.0.0.
186
+ */
187
+ FANN_EXTERNAL struct fann *FANN_API fann_create_standard_array(unsigned int num_layers,
188
+ const unsigned int *layers);
189
+
190
+ /* Function: fann_create_sparse
191
+
192
+ Creates a standard backpropagation neural network, which is not fully connected.
193
+
194
+ Parameters:
195
+ connection_rate - The connection rate controls how many connections there will be in the
196
+ network. If the connection rate is set to 1, the network will be fully
197
+ connected, but if it is set to 0.5 only half of the connections will be set.
198
+ A connection rate of 1 will yield the same result as <fann_create_standard>
199
+ num_layers - The total number of layers including the input and the output layer.
200
+ ... - Integer values determining the number of neurons in each layer starting with the
201
+ input layer and ending with the output layer.
202
+
203
+ Returns:
204
+ A pointer to the newly created <struct fann>.
205
+
206
+ See also:
207
+ <fann_create_sparse_array>, <fann_create_standard>, <fann_create_shortcut>
208
+
209
+ This function appears in FANN >= 2.0.0.
210
+ */
211
+ FANN_EXTERNAL struct fann *FANN_API fann_create_sparse(float connection_rate,
212
+ unsigned int num_layers, ...);
213
+
214
+
215
+ /* Function: fann_create_sparse_array
216
+ Just like <fann_create_sparse>, but with an array of layer sizes
217
+ instead of individual parameters.
218
+
219
+ See <fann_create_standard_array> for a description of the parameters.
220
+
221
+ See also:
222
+ <fann_create_sparse>, <fann_create_standard>, <fann_create_shortcut>
223
+
224
+ This function appears in FANN >= 2.0.0.
225
+ */
226
+ FANN_EXTERNAL struct fann *FANN_API fann_create_sparse_array(float connection_rate,
227
+ unsigned int num_layers,
228
+ const unsigned int *layers);
229
+
230
+ /* Function: fann_create_shortcut
231
+
232
+ Creates a standard backpropagation neural network, which is not fully connected and which
233
+ also has shortcut connections.
234
+
235
+ Shortcut connections are connections that skip layers. A fully connected network with shortcut
236
+ connections, is a network where all neurons are connected to all neurons in later layers.
237
+ Including direct connections from the input layer to the output layer.
238
+
239
+ See <fann_create_standard> for a description of the parameters.
240
+
241
+ See also:
242
+ <fann_create_shortcut_array>, <fann_create_standard>, <fann_create_sparse>,
243
+
244
+ This function appears in FANN >= 2.0.0.
245
+ */
246
+ FANN_EXTERNAL struct fann *FANN_API fann_create_shortcut(unsigned int num_layers, ...);
247
+
248
+ /* Function: fann_create_shortcut_array
249
+ Just like <fann_create_shortcut>, but with an array of layer sizes
250
+ instead of individual parameters.
251
+
252
+ See <fann_create_standard_array> for a description of the parameters.
253
+
254
+ See also:
255
+ <fann_create_shortcut>, <fann_create_standard>, <fann_create_sparse>
256
+
257
+ This function appears in FANN >= 2.0.0.
258
+ */
259
+ FANN_EXTERNAL struct fann *FANN_API fann_create_shortcut_array(unsigned int num_layers,
260
+ const unsigned int *layers);
261
+ /* Function: fann_destroy
262
+ Destroys the entire network and properly freeing all the associated memmory.
263
+
264
+ This function appears in FANN >= 1.0.0.
265
+ */
266
+ FANN_EXTERNAL void FANN_API fann_destroy(struct fann *ann);
267
+
268
+
269
+ /* Function: fann_copy
270
+ Creates a copy of a fann structure.
271
+
272
+ Data in the user data <fann_set_user_data> is not copied, but the user data pointer is copied.
273
+
274
+ This function appears in FANN >= 2.2.0.
275
+ */
276
+ FANN_EXTERNAL struct fann * FANN_API fann_copy(struct fann *ann);
277
+
278
+
279
+ /* Function: fann_run
280
+ Will run input through the neural network, returning an array of outputs, the number of which being
281
+ equal to the number of neurons in the output layer.
282
+
283
+ See also:
284
+ <fann_test>
285
+
286
+ This function appears in FANN >= 1.0.0.
287
+ */
288
+ FANN_EXTERNAL fann_type * FANN_API fann_run(struct fann *ann, fann_type * input);
289
+
290
+ /* Function: fann_randomize_weights
291
+ Give each connection a random weight between *min_weight* and *max_weight*
292
+
293
+ From the beginning the weights are random between -0.1 and 0.1.
294
+
295
+ See also:
296
+ <fann_init_weights>
297
+
298
+ This function appears in FANN >= 1.0.0.
299
+ */
300
+ FANN_EXTERNAL void FANN_API fann_randomize_weights(struct fann *ann, fann_type min_weight,
301
+ fann_type max_weight);
302
+
303
+ /* Function: fann_init_weights
304
+ Initialize the weights using Widrow + Nguyen's algorithm.
305
+
306
+ This function behaves similarly to fann_randomize_weights. It will use the algorithm developed
307
+ by Derrick Nguyen and Bernard Widrow to set the weights in such a way
308
+ as to speed up training. This technique is not always successful, and in some cases can be less
309
+ efficient than a purely random initialization.
310
+
311
+ The algorithm requires access to the range of the input data (ie, largest and smallest input),
312
+ and therefore accepts a second argument, data, which is the training data that will be used to
313
+ train the network.
314
+
315
+ See also:
316
+ <fann_randomize_weights>, <fann_read_train_from_file>
317
+
318
+ This function appears in FANN >= 1.1.0.
319
+ */
320
+ FANN_EXTERNAL void FANN_API fann_init_weights(struct fann *ann, struct fann_train_data *train_data);
321
+
322
+ /* Function: fann_print_connections
323
+ Will print the connections of the ann in a compact matrix, for easy viewing of the internals
324
+ of the ann.
325
+
326
+ The output from fann_print_connections on a small (2 2 1) network trained on the xor problem
327
+ >Layer / Neuron 012345
328
+ >L 1 / N 3 BBa...
329
+ >L 1 / N 4 BBA...
330
+ >L 1 / N 5 ......
331
+ >L 2 / N 6 ...BBA
332
+ >L 2 / N 7 ......
333
+
334
+ This network have five real neurons and two bias neurons. This gives a total of seven neurons
335
+ named from 0 to 6. The connections between these neurons can be seen in the matrix. "." is a
336
+ place where there is no connection, while a character tells how strong the connection is on a
337
+ scale from a-z. The two real neurons in the hidden layer (neuron 3 and 4 in layer 1) has
338
+ connection from the three neurons in the previous layer as is visible in the first two lines.
339
+ The output neuron (6) has connections form the three neurons in the hidden layer 3 - 5 as is
340
+ visible in the fourth line.
341
+
342
+ To simplify the matrix output neurons is not visible as neurons that connections can come from,
343
+ and input and bias neurons are not visible as neurons that connections can go to.
344
+
345
+ This function appears in FANN >= 1.2.0.
346
+ */
347
+ FANN_EXTERNAL void FANN_API fann_print_connections(struct fann *ann);
348
+
349
+ /* Group: Parameters */
350
+ /* Function: fann_print_parameters
351
+
352
+ Prints all of the parameters and options of the ANN
353
+
354
+ This function appears in FANN >= 1.2.0.
355
+ */
356
+ FANN_EXTERNAL void FANN_API fann_print_parameters(struct fann *ann);
357
+
358
+
359
+ /* Function: fann_get_num_input
360
+
361
+ Get the number of input neurons.
362
+
363
+ This function appears in FANN >= 1.0.0.
364
+ */
365
+ FANN_EXTERNAL unsigned int FANN_API fann_get_num_input(struct fann *ann);
366
+
367
+
368
+ /* Function: fann_get_num_output
369
+
370
+ Get the number of output neurons.
371
+
372
+ This function appears in FANN >= 1.0.0.
373
+ */
374
+ FANN_EXTERNAL unsigned int FANN_API fann_get_num_output(struct fann *ann);
375
+
376
+
377
+ /* Function: fann_get_total_neurons
378
+
379
+ Get the total number of neurons in the entire network. This number does also include the
380
+ bias neurons, so a 2-4-2 network has 2+4+2 +2(bias) = 10 neurons.
381
+
382
+ This function appears in FANN >= 1.0.0.
383
+ */
384
+ FANN_EXTERNAL unsigned int FANN_API fann_get_total_neurons(struct fann *ann);
385
+
386
+
387
+ /* Function: fann_get_total_connections
388
+
389
+ Get the total number of connections in the entire network.
390
+
391
+ This function appears in FANN >= 1.0.0.
392
+ */
393
+ FANN_EXTERNAL unsigned int FANN_API fann_get_total_connections(struct fann *ann);
394
+
395
+ /* Function: fann_get_network_type
396
+
397
+ Get the type of neural network it was created as.
398
+
399
+ Parameters:
400
+ ann - A previously created neural network structure of
401
+ type <struct fann> pointer.
402
+
403
+ Returns:
404
+ The neural network type from enum <fann_network_type_enum>
405
+
406
+ See Also:
407
+ <fann_network_type_enum>
408
+
409
+ This function appears in FANN >= 2.1.0
410
+ */
411
+ FANN_EXTERNAL enum fann_nettype_enum FANN_API fann_get_network_type(struct fann *ann);
412
+
413
+ /* Function: fann_get_connection_rate
414
+
415
+ Get the connection rate used when the network was created
416
+
417
+ Parameters:
418
+ ann - A previously created neural network structure of
419
+ type <struct fann> pointer.
420
+
421
+ Returns:
422
+ The connection rate
423
+
424
+ This function appears in FANN >= 2.1.0
425
+ */
426
+ FANN_EXTERNAL float FANN_API fann_get_connection_rate(struct fann *ann);
427
+
428
+ /* Function: fann_get_num_layers
429
+
430
+ Get the number of layers in the network
431
+
432
+ Parameters:
433
+ ann - A previously created neural network structure of
434
+ type <struct fann> pointer.
435
+
436
+ Returns:
437
+ The number of layers in the neural network
438
+
439
+ Example:
440
+ > // Obtain the number of layers in a neural network
441
+ > struct fann *ann = fann_create_standard(4, 2, 8, 9, 1);
442
+ > unsigned int num_layers = fann_get_num_layers(ann);
443
+
444
+ This function appears in FANN >= 2.1.0
445
+ */
446
+ FANN_EXTERNAL unsigned int FANN_API fann_get_num_layers(struct fann *ann);
447
+
448
+ /*Function: fann_get_layer_array
449
+
450
+ Get the number of neurons in each layer in the network.
451
+
452
+ Bias is not included so the layers match the fann_create functions.
453
+
454
+ Parameters:
455
+ ann - A previously created neural network structure of
456
+ type <struct fann> pointer.
457
+
458
+ The layers array must be preallocated to at least
459
+ sizeof(unsigned int) * fann_num_layers() long.
460
+
461
+ This function appears in FANN >= 2.1.0
462
+ */
463
+ FANN_EXTERNAL void FANN_API fann_get_layer_array(struct fann *ann, unsigned int *layers);
464
+
465
+ /* Function: fann_get_bias_array
466
+
467
+ Get the number of bias in each layer in the network.
468
+
469
+ Parameters:
470
+ ann - A previously created neural network structure of
471
+ type <struct fann> pointer.
472
+
473
+ The bias array must be preallocated to at least
474
+ sizeof(unsigned int) * fann_num_layers() long.
475
+
476
+ This function appears in FANN >= 2.1.0
477
+ */
478
+ FANN_EXTERNAL void FANN_API fann_get_bias_array(struct fann *ann, unsigned int *bias);
479
+
480
+ /* Function: fann_get_connection_array
481
+
482
+ Get the connections in the network.
483
+
484
+ Parameters:
485
+ ann - A previously created neural network structure of
486
+ type <struct fann> pointer.
487
+
488
+ The connections array must be preallocated to at least
489
+ sizeof(struct fann_connection) * fann_get_total_connections() long.
490
+
491
+ This function appears in FANN >= 2.1.0
492
+ */
493
+ FANN_EXTERNAL void FANN_API fann_get_connection_array(struct fann *ann,
494
+ struct fann_connection *connections);
495
+
496
+ /* Function: fann_set_weight_array
497
+
498
+ Set connections in the network.
499
+
500
+ Parameters:
501
+ ann - A previously created neural network structure of
502
+ type <struct fann> pointer.
503
+
504
+ Only the weights can be changed, connections and weights are ignored
505
+ if they do not already exist in the network.
506
+
507
+ The array must have sizeof(struct fann_connection) * num_connections size.
508
+
509
+ This function appears in FANN >= 2.1.0
510
+ */
511
+ FANN_EXTERNAL void FANN_API fann_set_weight_array(struct fann *ann,
512
+ struct fann_connection *connections, unsigned int num_connections);
513
+
514
+ /* Function: fann_set_weight
515
+
516
+ Set a connection in the network.
517
+
518
+ Parameters:
519
+ ann - A previously created neural network structure of
520
+ type <struct fann> pointer.
521
+
522
+ Only the weights can be changed. The connection/weight is
523
+ ignored if it does not already exist in the network.
524
+
525
+ This function appears in FANN >= 2.1.0
526
+ */
527
+ FANN_EXTERNAL void FANN_API fann_set_weight(struct fann *ann,
528
+ unsigned int from_neuron, unsigned int to_neuron, fann_type weight);
529
+
530
+ /* Function: fann_set_user_data
531
+
532
+ Store a pointer to user defined data. The pointer can be
533
+ retrieved with <fann_get_user_data> for example in a
534
+ callback. It is the user's responsibility to allocate and
535
+ deallocate any data that the pointer might point to.
536
+
537
+ Parameters:
538
+ ann - A previously created neural network structure of
539
+ type <struct fann> pointer.
540
+ user_data - A void pointer to user defined data.
541
+
542
+ This function appears in FANN >= 2.1.0
543
+ */
544
+ FANN_EXTERNAL void FANN_API fann_set_user_data(struct fann *ann, void *user_data);
545
+
546
+ /* Function: fann_get_user_data
547
+
548
+ Get a pointer to user defined data that was previously set
549
+ with <fann_set_user_data>. It is the user's responsibility to
550
+ allocate and deallocate any data that the pointer might point to.
551
+
552
+ Parameters:
553
+ ann - A previously created neural network structure of
554
+ type <struct fann> pointer.
555
+
556
+ Returns:
557
+ A void pointer to user defined data.
558
+
559
+ This function appears in FANN >= 2.1.0
560
+ */
561
+ FANN_EXTERNAL void * FANN_API fann_get_user_data(struct fann *ann);
562
+
563
+ #ifdef FIXEDFANN
564
+
565
+ /* Function: fann_get_decimal_point
566
+
567
+ Returns the position of the decimal point in the ann.
568
+
569
+ This function is only available when the ANN is in fixed point mode.
570
+
571
+ The decimal point is described in greater detail in the tutorial <Fixed Point Usage>.
572
+
573
+ See also:
574
+ <Fixed Point Usage>, <fann_get_multiplier>, <fann_save_to_fixed>, <fann_save_train_to_fixed>
575
+
576
+ This function appears in FANN >= 1.0.0.
577
+ */
578
+ FANN_EXTERNAL unsigned int FANN_API fann_get_decimal_point(struct fann *ann);
579
+
580
+
581
+ /* Function: fann_get_multiplier
582
+
583
+ returns the multiplier that fix point data is multiplied with.
584
+
585
+ This function is only available when the ANN is in fixed point mode.
586
+
587
+ The multiplier is the used to convert between floating point and fixed point notation.
588
+ A floating point number is multiplied with the multiplier in order to get the fixed point
589
+ number and visa versa.
590
+
591
+ The multiplier is described in greater detail in the tutorial <Fixed Point Usage>.
592
+
593
+ See also:
594
+ <Fixed Point Usage>, <fann_get_decimal_point>, <fann_save_to_fixed>, <fann_save_train_to_fixed>
595
+
596
+ This function appears in FANN >= 1.0.0.
597
+ */
598
+ FANN_EXTERNAL unsigned int FANN_API fann_get_multiplier(struct fann *ann);
599
+
600
+ #endif /* FIXEDFANN */
601
+
602
+ #ifdef __cplusplus
603
+ #ifndef __cplusplus
604
+ /* to fool automatic indention engines */
605
+ {
606
+
607
+ #endif
608
+ }
609
+ #endif /* __cplusplus */
610
+
611
+ #endif /* __fann_h__ */
612
+
613
+ #endif /* NOT FANN_INCLUDE */