File: fann_cpp_subclass.h

package info (click to toggle)
python-fann2 1%3A1.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 496 kB
  • sloc: cpp: 1,615; ansic: 862; python: 116; makefile: 22; sh: 13
file content (592 lines) | stat: -rw-r--r-- 18,343 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#ifndef FANN_CPP_SUBCLASS_H_INCLUDED
#define FANN_CPP_SUBCLASS_H_INCLUDED

#include <stdarg.h>
#include <string>
#include <fann_cpp.h>

#include <iostream>
/* Namespace: FANN
    The FANN namespace groups the C++ wrapper definitions */
namespace FANN
{

    template <typename T>
    class helper_array
    {
    	public:
            helper_array()
            {
                array=0;
                array_len=0;
		can_delete=true;
            }
            void set (T * array, unsigned int len)
            {
                this->array=array;
                this->array_len=array_len;
            }
            T* array;
            unsigned int array_len;
	    bool can_delete;
    };

    template <typename T>
    class helper_array_array
    {
        public:
            helper_array_array()
            {
                arrays=0;
                array_len=0;
                array_num=0;
                can_delete=false;
            }
            void set (T ** arrays, unsigned int len, unsigned int nun)
            {
                this->arrays=arrays;
                this->array_len=array_len;
                this->array_num=array_num;
            }
            T** arrays;
            unsigned int array_len;
            unsigned int array_num;
            bool can_delete;
    };

    /* Forward declaration of class neural_net and training_data */
    class Neural_net;
    class Training_data;


    /*************************************************************************/

    /* Class: training_data

        Encapsulation of a training data set <struct fann_train_data> and
        associated C API functions.
    */
    class Training_data : public training_data
    {
    public:
        /* Constructor: training_data

            Default constructor creates an empty neural net.
            Use <read_train_from_file>, <set_train_data> or <create_train_from_callback> to initialize.
        */
        Training_data() : training_data()
        {
        }

        /* Constructor: training_data

            Copy constructor constructs a copy of the training data.
            Corresponds to the C API <fann_duplicate_train_data> function.
        */
        Training_data(const Training_data &data)
        {
            destroy_train();
            if (data.train_data != NULL)
            {
                train_data = fann_duplicate_train_data(data.train_data);
            }
        }

        /* Destructor: ~training_data

            Provides automatic cleanup of data.
            Define USE_VIRTUAL_DESTRUCTOR if you need the destructor to be virtual.

            See also:
                <destroy>
        */
#ifdef USE_VIRTUAL_DESTRUCTOR
        virtual
#endif
        ~Training_data()
        {
            destroy_train();
        }



        /* Grant access to the encapsulated data since many situations
            and applications creates the data from sources other than files
            or uses the training data for testing and related functions */

        /* Method: get_input

            Returns:
                A pointer to the array of input training data

            See also:
                <get_output>, <set_train_data>
        */
        helper_array_array<fann_type>* get_input()
        {
            if (train_data == NULL)
            {
                return NULL;
            }
            else
            {
                helper_array_array<fann_type>* ret = new helper_array_array<fann_type>;

                ret->arrays=train_data->input;
                ret->array_num=train_data->num_data;
                ret->array_len=train_data->num_input;
		ret->can_delete=false;
                return ret;
            }
        }

        /* Method: get_output

            Returns:
                A pointer to the array of output training data

            See also:
                <get_input>, <set_train_data>
        */

        helper_array_array<fann_type>* get_output()
        {
            if (train_data == NULL)
            {
                return NULL;
            }
            else
            {
                helper_array_array<fann_type>* ret = new helper_array_array<fann_type>;

                ret->arrays=train_data->output;
                ret->array_num=train_data->num_data;
                ret->array_len=train_data->num_output;
		ret->can_delete=false;
                return ret;
            }
        }


        /* Method: set_train_data

            Set the training data to the input and output data provided.

            A copy of the data is made so there are no restrictions on the
            allocation of the input/output data and the caller is responsible
            for the deallocation of the data pointed to by input and output.

            See also:
                <get_input>, <get_output>
        */

        void set_train_data(helper_array_array< fann_type >* input,
            helper_array_array< fann_type >* output)
        {
            if (input->array_num!=output->array_num)
            {
                std::cerr<<"Error: input and output must have the same dimension!"<<std::endl;
                return;
            }
            input->can_delete=true;
            output->can_delete=true;

	    training_data::set_train_data(input->array_num, input->array_len, input->arrays, output->array_len, output->arrays);
        }


    };

    /*************************************************************************/

    /* Class: Neural_net

        Encapsulation of a neural network <struct fann> and
        associated C API functions.
    */
    class Neural_net : public neural_net
    {
    public:
        /* Constructor: neural_net

            Default constructor creates an empty neural net.
            Use one of the create functions to create the neural network.

            See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <create_standard_array>, <create_sparse_array>, <create_shortcut_array>
        */
        Neural_net() : neural_net()
        {
        }

        /* Destructor: ~neural_net

            Provides automatic cleanup of data.
            Define USE_VIRTUAL_DESTRUCTOR if you need the destructor to be virtual.

            See also:
                <destroy>
        */
#ifdef USE_VIRTUAL_DESTRUCTOR
        virtual
#endif
        ~Neural_net()
        {
            destroy();
        }


        /* Method: create_standard_array

           Just like <create_standard>, but with an array of layer sizes
           instead of individual parameters.

	        See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <fann_create_standard>

	        This function appears in FANN >= 2.0.0.
        */

        bool create_standard_array( helper_array<unsigned int>* layers)
        {
            return neural_net::create_standard_array( layers->array_len, layers->array);
        }

        /* Method: create_sparse_array
           Just like <create_sparse>, but with an array of layer sizes
           instead of individual parameters.

           See <create_sparse> for a description of the parameters.

	        See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <fann_create_sparse_array>

	        This function appears in FANN >= 2.0.0.
        */

        bool create_sparse_array(float connection_rate,
            helper_array<unsigned int>* layers)
         {
            return neural_net::create_sparse_array( connection_rate, layers->array_len, layers->array);
         }

        /* Method: create_shortcut_array

           Just like <create_shortcut>, but with an array of layer sizes
           instead of individual parameters.

	        See <create_standard_array> for a description of the parameters.

	        See also:
		        <create_standard>, <create_sparse>, <create_shortcut>,
		        <fann_create_shortcut_array>

	        This function appears in FANN >= 2.0.0.
        */

        bool create_shortcut_array( helper_array<unsigned int>* layers)
        {
            return neural_net::create_shortcut_array( layers->array_len, layers->array);
        }

        /* Method: run

	        Will run input through the neural network, returning an array of outputs, the number of which being
	        equal to the number of neurons in the output layer.

	        See also:
		        <test>, <fann_run>

	        This function appears in FANN >= 1.0.0.
        */

         helper_array<fann_type>* run(helper_array<fann_type> *input)
         {
            if (ann == NULL && input->array_len!=ann->num_input)
            {
                return NULL;
            }
            helper_array<fann_type>* res= new helper_array<fann_type>;
            res->array=fann_run(ann, input->array);
            res->array_len=ann->num_output;
            res->can_delete=false;
            return res;
         }



#ifndef FIXEDFANN
        /* Method: train

           Train one iteration with a set of inputs, and a set of desired outputs.
           This training is always incremental training (see <FANN::training_algorithm_enum>),
           since only one pattern is presented.

           Parameters:
   	        ann - The neural network structure
   	        input - an array of inputs. This array must be exactly <fann_get_num_input> long.
   	        desired_output - an array of desired outputs. This array must be exactly <fann_get_num_output> long.

   	        See also:
   		        <train_on_data>, <train_epoch>, <fann_train>

   	        This function appears in FANN >= 1.0.0.
         */

        void train(helper_array<fann_type> *input, helper_array<fann_type> *desired_output)
        {
            if (ann != NULL && input->array_len==ann->num_input && desired_output->array_len==ann->num_output)
            {
                fann_train(ann, input->array, desired_output->array);
            }
        }

#endif /* NOT FIXEDFANN */

        /* Method: test

           Test with a set of inputs, and a set of desired outputs.
           This operation updates the mean square error, but does not
           change the network in any way.

           See also:
   		        <test_data>, <train>, <fann_test>

           This function appears in FANN >= 1.0.0.
        */

         helper_array<fann_type>* test(helper_array<fann_type> *input, helper_array<fann_type>* desired_output)
         {
            if (ann == NULL)
            {
                return NULL;
            }
            helper_array<fann_type>* res= new helper_array<fann_type>;
            res->array=fann_test(ann, input->array, desired_output->array);
            res->array_len=ann->num_output;
            res->can_delete=false;
            return res;
        }


        /*************************************************************************************************************/


        /* Method: get_layer_array

            Return the number of neurons in each layer in the network.

            Bias is not included so the layers match the create methods.

            See also:
                <fann_get_layer_array>

           This function appears in FANN >= 2.1.0
        */

        helper_array<unsigned int>* get_layer_array()
        {
            if (ann != NULL)
            {
                helper_array<unsigned int>* res= new helper_array<unsigned int>;
                res->array_len = fann_get_num_layers(ann);
                res->array = (unsigned int*) malloc(sizeof(unsigned int)*
                        res->array_len);
                res->can_delete = true;
                fann_get_layer_array(ann, res->array);
                return res;
            }
            else {
                return NULL;
            }
        }

        /* Method: get_bias_array

            Get the number of bias in each layer in the network.

            The bias array must be preallocated to at least
            sizeof(unsigned int) * get_num_layers() long.

            See also:
                <fann_get_bias_array>

            This function appears in FANN >= 2.1.0
        */
        helper_array<unsigned int>* get_bias_array()
        {
            if (ann != NULL)
            {
                helper_array<unsigned int>* res= new helper_array<unsigned int>;
                res->array_len = fann_get_num_layers(ann);
                res->array = (unsigned int*) malloc(sizeof(unsigned int)*
                        res->array_len);
                res->can_delete = true;
                fann_get_bias_array(ann, res->array);
                return res;
            }
            else {
                return NULL;
            }
        }

        /* Method: get_connection_array

            Return the connections in the network.

            See also:
                <fann_get_connection_array>

           This function appears in FANN >= 2.1.0
        */

        helper_array<connection>* get_connection_array()
        {
            if (ann != NULL)
            {
                helper_array<connection>* res= new helper_array<connection>;
                res->array_len = fann_get_total_connections(ann);
                res->array = (connection*) malloc(sizeof(connection)*
                        res->array_len);
                res->can_delete = true;
                fann_get_connection_array(ann, res->array);
                return res;
            }
            else {
                return NULL;
            }
        }
        /* Method: set_weight_array

            Set connections in the network.

            Only the weights can be changed, connections and weights are ignored
            if they do not already exist in the network.

            The array must have sizeof(struct fann_connection) * num_connections size.

            See also:
                <fann_set_weight_array>

           This function appears in FANN >= 2.1.0
        */
        void set_weight_array(helper_array<connection> *connections)
        {
            if (ann != NULL)
            {
                fann_set_weight_array(ann, connections->array, connections->array_len);
            }
        }

        /*********************************************************************/

#ifdef TODO
        /* Method: get_cascade_activation_functions

           The cascade activation functions array is an array of the different activation functions used by
           the candidates.

           See <get_cascade_num_candidates> for a description of which candidate neurons will be
           generated by this array.

           See also:
   		        <get_cascade_activation_functions_count>, <set_cascade_activation_functions>,
   		        <FANN::activation_function_enum>

	        This function appears in FANN >= 2.0.0.
         */
        activation_function_enum * get_cascade_activation_functions()
        {
            enum fann_activationfunc_enum *activation_functions = NULL;
            if (ann != NULL)
            {
                activation_functions = fann_get_cascade_activation_functions(ann);
            }
            return reinterpret_cast<activation_function_enum *>(activation_functions);
        }

        /* Method: set_cascade_activation_functions

           Sets the array of cascade candidate activation functions. The array must be just as long
           as defined by the count.

           See <get_cascade_num_candidates> for a description of which candidate neurons will be
           generated by this array.

           See also:
   		        <get_cascade_activation_steepnesses_count>, <get_cascade_activation_steepnesses>,
                <fann_set_cascade_activation_functions>

	        This function appears in FANN >= 2.0.0.
         */
        void set_cascade_activation_functions(activation_function_enum *cascade_activation_functions,
            unsigned int cascade_activation_functions_count)
        {
            if (ann != NULL)
            {
                fann_set_cascade_activation_functions(ann,
                    reinterpret_cast<enum fann_activationfunc_enum *>(cascade_activation_functions),
                    cascade_activation_functions_count);
            }
        }
#endif
        /* Method: get_cascade_activation_steepnesses

           The cascade activation steepnesses array is an array of the different activation functions used by
           the candidates.

           See <get_cascade_num_candidates> for a description of which candidate neurons will be
           generated by this array.

           The default activation steepnesses is {0.25, 0.50, 0.75, 1.00}

           See also:
   		        <set_cascade_activation_steepnesses>, <get_cascade_activation_steepnesses_count>,
                <fann_get_cascade_activation_steepnesses>

	        This function appears in FANN >= 2.0.0.
         */
        helper_array<fann_type> *get_cascade_activation_steepnesses()
        {
            helper_array<fann_type> *activation_steepnesses = NULL;
            if (ann != NULL)
            {
                activation_steepnesses->array_len = fann_get_cascade_activation_steepnesses_count(ann);
                activation_steepnesses->array = fann_get_cascade_activation_steepnesses(ann);
            }
            return activation_steepnesses;
        }

        /* Method: set_cascade_activation_steepnesses

           Sets the array of cascade candidate activation steepnesses. The array must be just as long
           as defined by the count.

           See <get_cascade_num_candidates> for a description of which candidate neurons will be
           generated by this array.

           See also:
   		        <get_cascade_activation_steepnesses>, <get_cascade_activation_steepnesses_count>,
                <fann_set_cascade_activation_steepnesses>

	        This function appears in FANN >= 2.0.0.
         */
        void set_cascade_activation_steepnesses(helper_array<fann_type> *cascade_activation_steepnesses)
        {
            if (ann != NULL)
            {
                fann_set_cascade_activation_steepnesses(ann,
                    cascade_activation_steepnesses->array, cascade_activation_steepnesses->array_len);
            }
        }


    };

    /*************************************************************************/
};

#endif /* FANN_CPP_SUBCLASS_H_INCLUDED */