forked from libfann/fann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fann.c
1843 lines (1627 loc) · 58.3 KB
/
fann.c
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2012 Steffen Nissen ([email protected])
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "config.h"
#include "fann.h"
/* #define FANN_NO_SEED */
FANN_EXTERNAL struct fann *FANN_API fann_create_standard(unsigned int num_layers, ...)
{
struct fann *ann;
va_list layer_sizes;
int i;
int status;
int arg;
unsigned int *layers = (unsigned int *) calloc(num_layers, sizeof(unsigned int));
if(layers == NULL)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
return NULL;
}
va_start(layer_sizes, num_layers);
status = 1;
for(i = 0; i < (int) num_layers; i++)
{
arg = va_arg(layer_sizes, unsigned int);
if(arg < 0 || arg > 1000000)
status = 0;
layers[i] = arg;
}
va_end(layer_sizes);
if(!status)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
free(layers);
return NULL;
}
ann = fann_create_standard_array(num_layers, layers);
free(layers);
return ann;
}
FANN_EXTERNAL struct fann *FANN_API fann_create_standard_array(unsigned int num_layers,
const unsigned int *layers)
{
return fann_create_sparse_array(1, num_layers, layers);
}
FANN_EXTERNAL struct fann *FANN_API fann_create_sparse(float connection_rate,
unsigned int num_layers, ...)
{
struct fann *ann;
va_list layer_sizes;
int i;
int status;
int arg;
unsigned int *layers = (unsigned int *) calloc(num_layers, sizeof(unsigned int));
if(layers == NULL)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
return NULL;
}
va_start(layer_sizes, num_layers);
status = 1;
for(i = 0; i < (int) num_layers; i++)
{
arg = va_arg(layer_sizes, unsigned int);
if(arg < 0 || arg > 1000000)
status = 0;
layers[i] = arg;
}
va_end(layer_sizes);
if(!status)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
free(layers);
return NULL;
}
ann = fann_create_sparse_array(connection_rate, num_layers, layers);
free(layers);
return ann;
}
FANN_EXTERNAL struct fann *FANN_API fann_create_sparse_array(float connection_rate,
unsigned int num_layers,
const unsigned int *layers)
{
struct fann_layer *layer_it, *last_layer, *prev_layer;
struct fann *ann;
struct fann_neuron *neuron_it, *last_neuron, *random_neuron, *bias_neuron;
#ifdef DEBUG
unsigned int prev_layer_size;
#endif
unsigned int num_neurons_in, num_neurons_out, i, j;
unsigned int min_connections, max_connections, num_connections;
unsigned int connections_per_neuron, allocated_connections;
unsigned int random_number, found_connection, tmp_con;
#ifdef FIXEDFANN
unsigned int multiplier;
#endif
if(connection_rate > 1)
{
connection_rate = 1;
}
/* seed random */
#ifndef FANN_NO_SEED
fann_seed_rand();
#endif
/* allocate the general structure */
ann = fann_allocate_structure(num_layers);
if(ann == NULL)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
return NULL;
}
ann->connection_rate = connection_rate;
#ifdef FIXEDFANN
multiplier = ann->multiplier;
fann_update_stepwise(ann);
#endif
/* determine how many neurons there should be in each layer */
i = 0;
for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++)
{
/* we do not allocate room here, but we make sure that
* last_neuron - first_neuron is the number of neurons */
layer_it->first_neuron = NULL;
layer_it->last_neuron = layer_it->first_neuron + layers[i++] + 1; /* +1 for bias */
ann->total_neurons += (unsigned int)(layer_it->last_neuron - layer_it->first_neuron);
}
ann->num_output = (unsigned int)((ann->last_layer - 1)->last_neuron - (ann->last_layer - 1)->first_neuron - 1);
ann->num_input = (unsigned int)(ann->first_layer->last_neuron - ann->first_layer->first_neuron - 1);
/* allocate room for the actual neurons */
fann_allocate_neurons(ann);
if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM)
{
fann_destroy(ann);
return NULL;
}
#ifdef DEBUG
printf("creating network with connection rate %f\n", connection_rate);
printf("input\n");
printf(" layer : %d neurons, 1 bias\n",
(int)(ann->first_layer->last_neuron - ann->first_layer->first_neuron - 1));
#endif
num_neurons_in = ann->num_input;
for(layer_it = ann->first_layer + 1; layer_it != ann->last_layer; layer_it++)
{
num_neurons_out = (unsigned int)(layer_it->last_neuron - layer_it->first_neuron - 1);
/*�if all neurons in each layer should be connected to at least one neuron
* in the previous layer, and one neuron in the next layer.
* and the bias node should be connected to the all neurons in the next layer.
* Then this is the minimum amount of neurons */
min_connections = fann_max(num_neurons_in, num_neurons_out); /* not calculating bias */
max_connections = num_neurons_in * num_neurons_out; /* not calculating bias */
num_connections = fann_max(min_connections,
(unsigned int) (0.5 + (connection_rate * max_connections))) +
num_neurons_out;
connections_per_neuron = num_connections / num_neurons_out;
allocated_connections = 0;
/* Now split out the connections on the different neurons */
for(i = 0; i != num_neurons_out; i++)
{
layer_it->first_neuron[i].first_con = ann->total_connections + allocated_connections;
allocated_connections += connections_per_neuron;
layer_it->first_neuron[i].last_con = ann->total_connections + allocated_connections;
layer_it->first_neuron[i].activation_function = FANN_SIGMOID_STEPWISE;
#ifdef FIXEDFANN
layer_it->first_neuron[i].activation_steepness = ann->multiplier / 2;
#else
layer_it->first_neuron[i].activation_steepness = 0.5;
#endif
if(allocated_connections < (num_connections * (i + 1)) / num_neurons_out)
{
layer_it->first_neuron[i].last_con++;
allocated_connections++;
}
}
/* bias neuron also gets stuff */
layer_it->first_neuron[i].first_con = ann->total_connections + allocated_connections;
layer_it->first_neuron[i].last_con = ann->total_connections + allocated_connections;
ann->total_connections += num_connections;
/* used in the next run of the loop */
num_neurons_in = num_neurons_out;
}
fann_allocate_connections(ann);
if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM)
{
fann_destroy(ann);
return NULL;
}
if(connection_rate >= 1)
{
#ifdef DEBUG
prev_layer_size = ann->num_input + 1;
#endif
prev_layer = ann->first_layer;
last_layer = ann->last_layer;
for(layer_it = ann->first_layer + 1; layer_it != last_layer; layer_it++)
{
last_neuron = layer_it->last_neuron - 1;
for(neuron_it = layer_it->first_neuron; neuron_it != last_neuron; neuron_it++)
{
tmp_con = neuron_it->last_con - 1;
for(i = neuron_it->first_con; i != tmp_con; i++)
{
ann->weights[i] = (fann_type) fann_random_weight();
/* these connections are still initialized for fully connected networks, to allow
* operations to work, that are not optimized for fully connected networks.
*/
ann->connections[i] = prev_layer->first_neuron + (i - neuron_it->first_con);
}
/* bias weight */
ann->weights[tmp_con] = (fann_type) fann_random_bias_weight();
ann->connections[tmp_con] = prev_layer->first_neuron + (tmp_con - neuron_it->first_con);
}
#ifdef DEBUG
prev_layer_size = layer_it->last_neuron - layer_it->first_neuron;
#endif
prev_layer = layer_it;
#ifdef DEBUG
printf(" layer : %d neurons, 1 bias\n", prev_layer_size - 1);
#endif
}
}
else
{
/* make connections for a network, that are not fully connected */
/* generally, what we do is first to connect all the input
* neurons to a output neuron, respecting the number of
* available input neurons for each output neuron. Then
* we go through all the output neurons, and connect the
* rest of the connections to input neurons, that they are
* not allready connected to.
*/
/* All the connections are cleared by calloc, because we want to
* be able to see which connections are allready connected */
for(layer_it = ann->first_layer + 1; layer_it != ann->last_layer; layer_it++)
{
num_neurons_out = (unsigned int)(layer_it->last_neuron - layer_it->first_neuron - 1);
num_neurons_in = (unsigned int)((layer_it - 1)->last_neuron - (layer_it - 1)->first_neuron - 1);
/* first connect the bias neuron */
bias_neuron = (layer_it - 1)->last_neuron - 1;
last_neuron = layer_it->last_neuron - 1;
for(neuron_it = layer_it->first_neuron; neuron_it != last_neuron; neuron_it++)
{
ann->connections[neuron_it->first_con] = bias_neuron;
ann->weights[neuron_it->first_con] = (fann_type) fann_random_bias_weight();
}
/* then connect all neurons in the input layer */
last_neuron = (layer_it - 1)->last_neuron - 1;
for(neuron_it = (layer_it - 1)->first_neuron; neuron_it != last_neuron; neuron_it++)
{
/* random neuron in the output layer that has space
* for more connections */
do
{
random_number = (int) (0.5 + fann_rand(0, num_neurons_out - 1));
random_neuron = layer_it->first_neuron + random_number;
/* checks the last space in the connections array for room */
}
while(ann->connections[random_neuron->last_con - 1]);
/* find an empty space in the connection array and connect */
for(i = random_neuron->first_con; i < random_neuron->last_con; i++)
{
if(ann->connections[i] == NULL)
{
ann->connections[i] = neuron_it;
ann->weights[i] = (fann_type) fann_random_weight();
break;
}
}
}
/* then connect the rest of the unconnected neurons */
last_neuron = layer_it->last_neuron - 1;
for(neuron_it = layer_it->first_neuron; neuron_it != last_neuron; neuron_it++)
{
/* find empty space in the connection array and connect */
for(i = neuron_it->first_con; i < neuron_it->last_con; i++)
{
/* continue if allready connected */
if(ann->connections[i] != NULL)
continue;
do
{
found_connection = 0;
random_number = (int) (0.5 + fann_rand(0, num_neurons_in - 1));
random_neuron = (layer_it - 1)->first_neuron + random_number;
/* check to see if this connection is allready there */
for(j = neuron_it->first_con; j < i; j++)
{
if(random_neuron == ann->connections[j])
{
found_connection = 1;
break;
}
}
}
while(found_connection);
/* we have found a neuron that is not allready
* connected to us, connect it */
ann->connections[i] = random_neuron;
ann->weights[i] = (fann_type) fann_random_weight();
}
}
#ifdef DEBUG
printf(" layer : %d neurons, 1 bias\n", num_neurons_out);
#endif
}
/* TODO it would be nice to have the randomly created
* connections sorted for smoother memory access.
*/
}
#ifdef DEBUG
printf("output\n");
#endif
return ann;
}
FANN_EXTERNAL struct fann *FANN_API fann_create_shortcut(unsigned int num_layers, ...)
{
struct fann *ann;
int i;
int status;
int arg;
va_list layer_sizes;
unsigned int *layers = (unsigned int *) calloc(num_layers, sizeof(unsigned int));
if(layers == NULL)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
return NULL;
}
va_start(layer_sizes, num_layers);
status = 1;
for(i = 0; i < (int) num_layers; i++)
{
arg = va_arg(layer_sizes, unsigned int);
if(arg < 0 || arg > 1000000)
status = 0;
layers[i] = arg;
}
va_end(layer_sizes);
if(!status)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
free(layers);
return NULL;
}
ann = fann_create_shortcut_array(num_layers, layers);
free(layers);
return ann;
}
FANN_EXTERNAL struct fann *FANN_API fann_create_shortcut_array(unsigned int num_layers,
const unsigned int *layers)
{
struct fann_layer *layer_it, *layer_it2, *last_layer;
struct fann *ann;
struct fann_neuron *neuron_it, *neuron_it2 = 0;
unsigned int i;
unsigned int num_neurons_in, num_neurons_out;
#ifdef FIXEDFANN
unsigned int multiplier;
#endif
/* seed random */
#ifndef FANN_NO_SEED
fann_seed_rand();
#endif
/* allocate the general structure */
ann = fann_allocate_structure(num_layers);
if(ann == NULL)
{
fann_error(NULL, FANN_E_CANT_ALLOCATE_MEM);
return NULL;
}
ann->connection_rate = 1;
ann->network_type = FANN_NETTYPE_SHORTCUT;
#ifdef FIXEDFANN
multiplier = ann->multiplier;
fann_update_stepwise(ann);
#endif
/* determine how many neurons there should be in each layer */
i = 0;
for(layer_it = ann->first_layer; layer_it != ann->last_layer; layer_it++)
{
/* we do not allocate room here, but we make sure that
* last_neuron - first_neuron is the number of neurons */
layer_it->first_neuron = NULL;
layer_it->last_neuron = layer_it->first_neuron + layers[i++];
if(layer_it == ann->first_layer)
{
/* there is a bias neuron in the first layer */
layer_it->last_neuron++;
}
ann->total_neurons += (unsigned int)(layer_it->last_neuron - layer_it->first_neuron);
}
ann->num_output = (unsigned int)((ann->last_layer - 1)->last_neuron - (ann->last_layer - 1)->first_neuron);
ann->num_input = (unsigned int)(ann->first_layer->last_neuron - ann->first_layer->first_neuron - 1);
/* allocate room for the actual neurons */
fann_allocate_neurons(ann);
if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM)
{
fann_destroy(ann);
return NULL;
}
#ifdef DEBUG
printf("creating fully shortcut connected network.\n");
printf("input\n");
printf(" layer : %d neurons, 1 bias\n",
(int)(ann->first_layer->last_neuron - ann->first_layer->first_neuron - 1));
#endif
num_neurons_in = ann->num_input;
last_layer = ann->last_layer;
for(layer_it = ann->first_layer + 1; layer_it != last_layer; layer_it++)
{
num_neurons_out = (unsigned int)(layer_it->last_neuron - layer_it->first_neuron);
/* Now split out the connections on the different neurons */
for(i = 0; i != num_neurons_out; i++)
{
layer_it->first_neuron[i].first_con = ann->total_connections;
ann->total_connections += num_neurons_in + 1;
layer_it->first_neuron[i].last_con = ann->total_connections;
layer_it->first_neuron[i].activation_function = FANN_SIGMOID_STEPWISE;
#ifdef FIXEDFANN
layer_it->first_neuron[i].activation_steepness = ann->multiplier / 2;
#else
layer_it->first_neuron[i].activation_steepness = 0.5;
#endif
}
#ifdef DEBUG
printf(" layer : %d neurons, 0 bias\n", num_neurons_out);
#endif
/* used in the next run of the loop */
num_neurons_in += num_neurons_out;
}
fann_allocate_connections(ann);
if(ann->errno_f == FANN_E_CANT_ALLOCATE_MEM)
{
fann_destroy(ann);
return NULL;
}
/* Connections are created from all neurons to all neurons in later layers
*/
num_neurons_in = ann->num_input + 1;
for(layer_it = ann->first_layer + 1; layer_it != last_layer; layer_it++)
{
for(neuron_it = layer_it->first_neuron; neuron_it != layer_it->last_neuron; neuron_it++)
{
i = neuron_it->first_con;
for(layer_it2 = ann->first_layer; layer_it2 != layer_it; layer_it2++)
{
for(neuron_it2 = layer_it2->first_neuron; neuron_it2 != layer_it2->last_neuron;
neuron_it2++)
{
ann->weights[i] = (fann_type) fann_random_weight();
ann->connections[i] = neuron_it2;
i++;
}
}
}
num_neurons_in += (unsigned int)(layer_it->last_neuron - layer_it->first_neuron);
}
#ifdef DEBUG
printf("output\n");
#endif
return ann;
}
FANN_EXTERNAL fann_type *FANN_API fann_run(struct fann * ann, fann_type * input)
{
struct fann_neuron *neuron_it, *last_neuron, *neurons, **neuron_pointers;
unsigned int i, num_connections, num_input, num_output;
fann_type neuron_sum, *output;
fann_type *weights;
struct fann_layer *layer_it, *last_layer;
unsigned int activation_function;
fann_type steepness;
/* store some variabels local for fast access */
struct fann_neuron *first_neuron = ann->first_layer->first_neuron;
#ifdef FIXEDFANN
int multiplier = ann->multiplier;
unsigned int decimal_point = ann->decimal_point;
/* values used for the stepwise linear sigmoid function */
fann_type r1 = 0, r2 = 0, r3 = 0, r4 = 0, r5 = 0, r6 = 0;
fann_type v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0;
fann_type last_steepness = 0;
unsigned int last_activation_function = 0;
#else
fann_type max_sum = 0;
#endif
/* first set the input */
num_input = ann->num_input;
for(i = 0; i != num_input; i++)
{
#ifdef FIXEDFANN
if(fann_abs(input[i]) > multiplier)
{
printf
("Warning input number %d is out of range -%d - %d with value %d, integer overflow may occur.\n",
i, multiplier, multiplier, input[i]);
}
#endif
first_neuron[i].value = input[i];
}
/* Set the bias neuron in the input layer */
#ifdef FIXEDFANN
(ann->first_layer->last_neuron - 1)->value = multiplier;
#else
(ann->first_layer->last_neuron - 1)->value = 1;
#endif
last_layer = ann->last_layer;
for(layer_it = ann->first_layer + 1; layer_it != last_layer; layer_it++)
{
last_neuron = layer_it->last_neuron;
for(neuron_it = layer_it->first_neuron; neuron_it != last_neuron; neuron_it++)
{
if(neuron_it->first_con == neuron_it->last_con)
{
/* bias neurons */
#ifdef FIXEDFANN
neuron_it->value = multiplier;
#else
neuron_it->value = 1;
#endif
continue;
}
activation_function = neuron_it->activation_function;
steepness = neuron_it->activation_steepness;
neuron_sum = 0;
num_connections = neuron_it->last_con - neuron_it->first_con;
weights = ann->weights + neuron_it->first_con;
if(ann->connection_rate >= 1)
{
if(ann->network_type == FANN_NETTYPE_SHORTCUT)
{
neurons = ann->first_layer->first_neuron;
}
else
{
neurons = (layer_it - 1)->first_neuron;
}
/* unrolled loop start */
i = num_connections & 3; /* same as modulo 4 */
switch (i)
{
case 3:
neuron_sum += fann_mult(weights[2], neurons[2].value);
case 2:
neuron_sum += fann_mult(weights[1], neurons[1].value);
case 1:
neuron_sum += fann_mult(weights[0], neurons[0].value);
case 0:
break;
}
for(; i != num_connections; i += 4)
{
neuron_sum +=
fann_mult(weights[i], neurons[i].value) +
fann_mult(weights[i + 1], neurons[i + 1].value) +
fann_mult(weights[i + 2], neurons[i + 2].value) +
fann_mult(weights[i + 3], neurons[i + 3].value);
}
/* unrolled loop end */
/*
* for(i = 0;i != num_connections; i++){
* printf("%f += %f*%f, ", neuron_sum, weights[i], neurons[i].value);
* neuron_sum += fann_mult(weights[i], neurons[i].value);
* }
*/
}
else
{
neuron_pointers = ann->connections + neuron_it->first_con;
i = num_connections & 3; /* same as modulo 4 */
switch (i)
{
case 3:
neuron_sum += fann_mult(weights[2], neuron_pointers[2]->value);
case 2:
neuron_sum += fann_mult(weights[1], neuron_pointers[1]->value);
case 1:
neuron_sum += fann_mult(weights[0], neuron_pointers[0]->value);
case 0:
break;
}
for(; i != num_connections; i += 4)
{
neuron_sum +=
fann_mult(weights[i], neuron_pointers[i]->value) +
fann_mult(weights[i + 1], neuron_pointers[i + 1]->value) +
fann_mult(weights[i + 2], neuron_pointers[i + 2]->value) +
fann_mult(weights[i + 3], neuron_pointers[i + 3]->value);
}
}
#ifdef FIXEDFANN
neuron_it->sum = fann_mult(steepness, neuron_sum);
if(activation_function != last_activation_function || steepness != last_steepness)
{
switch (activation_function)
{
case FANN_SIGMOID:
case FANN_SIGMOID_STEPWISE:
r1 = ann->sigmoid_results[0];
r2 = ann->sigmoid_results[1];
r3 = ann->sigmoid_results[2];
r4 = ann->sigmoid_results[3];
r5 = ann->sigmoid_results[4];
r6 = ann->sigmoid_results[5];
v1 = ann->sigmoid_values[0] / steepness;
v2 = ann->sigmoid_values[1] / steepness;
v3 = ann->sigmoid_values[2] / steepness;
v4 = ann->sigmoid_values[3] / steepness;
v5 = ann->sigmoid_values[4] / steepness;
v6 = ann->sigmoid_values[5] / steepness;
break;
case FANN_SIGMOID_SYMMETRIC:
case FANN_SIGMOID_SYMMETRIC_STEPWISE:
r1 = ann->sigmoid_symmetric_results[0];
r2 = ann->sigmoid_symmetric_results[1];
r3 = ann->sigmoid_symmetric_results[2];
r4 = ann->sigmoid_symmetric_results[3];
r5 = ann->sigmoid_symmetric_results[4];
r6 = ann->sigmoid_symmetric_results[5];
v1 = ann->sigmoid_symmetric_values[0] / steepness;
v2 = ann->sigmoid_symmetric_values[1] / steepness;
v3 = ann->sigmoid_symmetric_values[2] / steepness;
v4 = ann->sigmoid_symmetric_values[3] / steepness;
v5 = ann->sigmoid_symmetric_values[4] / steepness;
v6 = ann->sigmoid_symmetric_values[5] / steepness;
break;
case FANN_THRESHOLD:
break;
}
}
switch (activation_function)
{
case FANN_SIGMOID:
case FANN_SIGMOID_STEPWISE:
neuron_it->value =
(fann_type) fann_stepwise(v1, v2, v3, v4, v5, v6, r1, r2, r3, r4, r5, r6, 0,
multiplier, neuron_sum);
break;
case FANN_SIGMOID_SYMMETRIC:
case FANN_SIGMOID_SYMMETRIC_STEPWISE:
neuron_it->value =
(fann_type) fann_stepwise(v1, v2, v3, v4, v5, v6, r1, r2, r3, r4, r5, r6,
-multiplier, multiplier, neuron_sum);
break;
case FANN_THRESHOLD:
neuron_it->value = (fann_type) ((neuron_sum < 0) ? 0 : multiplier);
break;
case FANN_THRESHOLD_SYMMETRIC:
neuron_it->value = (fann_type) ((neuron_sum < 0) ? -multiplier : multiplier);
break;
case FANN_LINEAR:
neuron_it->value = neuron_sum;
break;
case FANN_LINEAR_PIECE:
neuron_it->value = (fann_type)((neuron_sum < 0) ? 0 : (neuron_sum > multiplier) ? multiplier : neuron_sum);
break;
case FANN_LINEAR_PIECE_SYMMETRIC:
neuron_it->value = (fann_type)((neuron_sum < -multiplier) ? -multiplier : (neuron_sum > multiplier) ? multiplier : neuron_sum);
break;
case FANN_ELLIOT:
case FANN_ELLIOT_SYMMETRIC:
case FANN_GAUSSIAN:
case FANN_GAUSSIAN_SYMMETRIC:
case FANN_GAUSSIAN_STEPWISE:
case FANN_SIN_SYMMETRIC:
case FANN_COS_SYMMETRIC:
fann_error((struct fann_error *) ann, FANN_E_CANT_USE_ACTIVATION);
break;
}
last_steepness = steepness;
last_activation_function = activation_function;
#else
neuron_sum = fann_mult(steepness, neuron_sum);
max_sum = 150/steepness;
if(neuron_sum > max_sum)
neuron_sum = max_sum;
else if(neuron_sum < -max_sum)
neuron_sum = -max_sum;
neuron_it->sum = neuron_sum;
fann_activation_switch(activation_function, neuron_sum, neuron_it->value);
#endif
}
}
/* set the output */
output = ann->output;
num_output = ann->num_output;
neurons = (ann->last_layer - 1)->first_neuron;
for(i = 0; i != num_output; i++)
{
output[i] = neurons[i].value;
}
return ann->output;
}
FANN_EXTERNAL void FANN_API fann_destroy(struct fann *ann)
{
if(ann == NULL)
return;
fann_safe_free(ann->weights);
fann_safe_free(ann->connections);
fann_safe_free(ann->first_layer->first_neuron);
fann_safe_free(ann->first_layer);
fann_safe_free(ann->output);
fann_safe_free(ann->train_errors);
fann_safe_free(ann->train_slopes);
fann_safe_free(ann->prev_train_slopes);
fann_safe_free(ann->prev_steps);
fann_safe_free(ann->prev_weights_deltas);
fann_safe_free(ann->errstr);
fann_safe_free(ann->cascade_activation_functions);
fann_safe_free(ann->cascade_activation_steepnesses);
fann_safe_free(ann->cascade_candidate_scores);
#ifndef FIXEDFANN
fann_safe_free( ann->scale_mean_in );
fann_safe_free( ann->scale_deviation_in );
fann_safe_free( ann->scale_new_min_in );
fann_safe_free( ann->scale_factor_in );
fann_safe_free( ann->scale_mean_out );
fann_safe_free( ann->scale_deviation_out );
fann_safe_free( ann->scale_new_min_out );
fann_safe_free( ann->scale_factor_out );
#endif
fann_safe_free(ann);
}
FANN_EXTERNAL void FANN_API fann_randomize_weights(struct fann *ann, fann_type min_weight,
fann_type max_weight)
{
fann_type *last_weight;
fann_type *weights = ann->weights;
last_weight = weights + ann->total_connections;
for(; weights != last_weight; weights++)
{
*weights = (fann_type) (fann_rand(min_weight, max_weight));
}
#ifndef FIXEDFANN
if(ann->prev_train_slopes != NULL)
{
fann_clear_train_arrays(ann);
}
#endif
}
/* deep copy of the fann structure */
FANN_EXTERNAL struct fann* FANN_API fann_copy(struct fann* orig)
{
struct fann* copy;
unsigned int num_layers = (unsigned int)(orig->last_layer - orig->first_layer);
struct fann_layer *orig_layer_it, *copy_layer_it;
unsigned int layer_size;
struct fann_neuron *last_neuron,*orig_neuron_it,*copy_neuron_it;
unsigned int i;
struct fann_neuron *orig_first_neuron,*copy_first_neuron;
unsigned int input_neuron;
copy = fann_allocate_structure(num_layers);
if (copy==NULL) {
fann_error((struct fann_error*)orig, FANN_E_CANT_ALLOCATE_MEM);
return NULL;
}
copy->errno_f = orig->errno_f;
if (orig->errstr)
{
copy->errstr = (char *) malloc(FANN_ERRSTR_MAX);
if (copy->errstr == NULL)
{
fann_destroy(copy);
return NULL;
}
strcpy(copy->errstr,orig->errstr);
}
copy->error_log = orig->error_log;
copy->learning_rate = orig->learning_rate;
copy->learning_momentum = orig->learning_momentum;
copy->connection_rate = orig->connection_rate;
copy->network_type = orig->network_type;
copy->num_MSE = orig->num_MSE;
copy->MSE_value = orig->MSE_value;
copy->num_bit_fail = orig->num_bit_fail;
copy->bit_fail_limit = orig->bit_fail_limit;
copy->train_error_function = orig->train_error_function;
copy->train_stop_function = orig->train_stop_function;
copy->training_algorithm = orig->training_algorithm;
copy->callback = orig->callback;
copy->cascade_output_change_fraction = orig->cascade_output_change_fraction;
copy->cascade_output_stagnation_epochs = orig->cascade_output_stagnation_epochs;
copy->cascade_candidate_change_fraction = orig->cascade_candidate_change_fraction;
copy->cascade_candidate_stagnation_epochs = orig->cascade_candidate_stagnation_epochs;
copy->cascade_best_candidate = orig->cascade_best_candidate;
copy->cascade_candidate_limit = orig->cascade_candidate_limit;
copy->cascade_weight_multiplier = orig->cascade_weight_multiplier;
copy->cascade_max_out_epochs = orig->cascade_max_out_epochs;
copy->cascade_max_cand_epochs = orig->cascade_max_cand_epochs;
copy->user_data = orig->user_data;
/* copy cascade activation functions */
copy->cascade_activation_functions_count = orig->cascade_activation_functions_count;
copy->cascade_activation_functions = (enum fann_activationfunc_enum *)realloc(copy->cascade_activation_functions,
copy->cascade_activation_functions_count * sizeof(enum fann_activationfunc_enum));
if(copy->cascade_activation_functions == NULL)
{
fann_error((struct fann_error*)orig, FANN_E_CANT_ALLOCATE_MEM);
fann_destroy(copy);
return NULL;
}
memcpy(copy->cascade_activation_functions,orig->cascade_activation_functions,
copy->cascade_activation_functions_count * sizeof(enum fann_activationfunc_enum));
/* copy cascade activation steepnesses */
copy->cascade_activation_steepnesses_count = orig->cascade_activation_steepnesses_count;
copy->cascade_activation_steepnesses = (fann_type *)realloc(copy->cascade_activation_steepnesses, copy->cascade_activation_steepnesses_count * sizeof(fann_type));
if(copy->cascade_activation_steepnesses == NULL)
{
fann_error((struct fann_error*)orig, FANN_E_CANT_ALLOCATE_MEM);
fann_destroy(copy);
return NULL;
}
memcpy(copy->cascade_activation_steepnesses,orig->cascade_activation_steepnesses,copy->cascade_activation_steepnesses_count * sizeof(fann_type));
copy->cascade_num_candidate_groups = orig->cascade_num_candidate_groups;
/* copy candidate scores, if used */
if (orig->cascade_candidate_scores == NULL)
{
copy->cascade_candidate_scores = NULL;
}
else
{
copy->cascade_candidate_scores =
(fann_type *) malloc(fann_get_cascade_num_candidates(copy) * sizeof(fann_type));
if(copy->cascade_candidate_scores == NULL)
{
fann_error((struct fann_error *) orig, FANN_E_CANT_ALLOCATE_MEM);
fann_destroy(copy);
return NULL;
}
memcpy(copy->cascade_candidate_scores,orig->cascade_candidate_scores,fann_get_cascade_num_candidates(copy) * sizeof(fann_type));
}
copy->quickprop_decay = orig->quickprop_decay;
copy->quickprop_mu = orig->quickprop_mu;
copy->rprop_increase_factor = orig->rprop_increase_factor;
copy->rprop_decrease_factor = orig->rprop_decrease_factor;
copy->rprop_delta_min = orig->rprop_delta_min;
copy->rprop_delta_max = orig->rprop_delta_max;
copy->rprop_delta_zero = orig->rprop_delta_zero;
/* user_data is not deep copied. user should use fann_copy_with_user_data() for that */
copy->user_data = orig->user_data;
#ifdef FIXEDFANN
copy->decimal_point = orig->decimal_point;
copy->multiplier = orig->multiplier;
memcpy(copy->sigmoid_results,orig->sigmoid_results,6*sizeof(fann_type));
memcpy(copy->sigmoid_values,orig->sigmoid_values,6*sizeof(fann_type));
memcpy(copy->sigmoid_symmetric_results,orig->sigmoid_symmetric_results,6*sizeof(fann_type));
memcpy(copy->sigmoid_symmetric_values,orig->sigmoid_symmetric_values,6*sizeof(fann_type));
#endif
/* copy layer sizes, prepare for fann_allocate_neurons */
for (orig_layer_it = orig->first_layer, copy_layer_it = copy->first_layer;
orig_layer_it != orig->last_layer; orig_layer_it++, copy_layer_it++)
{
layer_size = (unsigned int)(orig_layer_it->last_neuron - orig_layer_it->first_neuron);
copy_layer_it->first_neuron = NULL;
copy_layer_it->last_neuron = copy_layer_it->first_neuron + layer_size;
copy->total_neurons += layer_size;
}
copy->num_input = orig->num_input;
copy->num_output = orig->num_output;