]> ruin.nu Git - germs.git/blob - fann/src/include/fann_activation.h
Make it possible to build statically against the included fann library.
[germs.git] / fann / src / include / fann_activation.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_activation_h__
21 #define __fann_activation_h__
22 /* internal include file, not to be included directly
23  */
24
25 /* Implementation of the activation functions
26  */
27
28 /* stepwise linear functions used for some of the activation functions */
29
30 /* defines used for the stepwise linear functions */
31
32 #define fann_linear_func(v1, r1, v2, r2, sum) (((((r2)-(r1)) * ((sum)-(v1)))/((v2)-(v1))) + (r1))
33 #define fann_stepwise(v1, v2, v3, v4, v5, v6, r1, r2, r3, r4, r5, r6, min, max, sum) (sum < v5 ? (sum < v3 ? (sum < v2 ? (sum < v1 ? min : fann_linear_func(v1, r1, v2, r2, sum)) : fann_linear_func(v2, r2, v3, r3, sum)) : (sum < v4 ? fann_linear_func(v3, r3, v4, r4, sum) : fann_linear_func(v4, r4, v5, r5, sum))) : (sum < v6 ? fann_linear_func(v5, r5, v6, r6, sum) : max))
34
35 /* FANN_LINEAR */
36 #define fann_linear(steepness, sum) fann_mult(steepness, sum)
37 #define fann_linear_derive(steepness, value) (steepness)
38
39 /* FANN_SIGMOID */
40 #define fann_sigmoid(steepness, sum) (1.0f/(1.0f + exp(-2.0f * steepness * sum)))
41 #define fann_sigmoid_real(sum) (1.0f/(1.0f + exp(-2.0f * sum)))
42 #define fann_sigmoid_derive(steepness, value) (2.0f * steepness * value * (1.0f - value))
43
44 /* FANN_SIGMOID_SYMMETRIC */
45 #define fann_sigmoid_symmetric(steepness, sum) (2.0f/(1.0f + exp(-2.0f * steepness * sum)) - 1.0f)
46 #define fann_sigmoid_symmetric_real(sum) (2.0f/(1.0f + exp(-2.0f * sum)) - 1.0f)
47 #define fann_sigmoid_symmetric_derive(steepness, value) steepness * (1.0f - (value*value))
48
49 /* FANN_GAUSSIAN */
50 #define fann_gaussian(steepness, sum) (exp(-sum * steepness * sum * steepness))
51 #define fann_gaussian_real(sum) (exp(-sum * sum))
52 #define fann_gaussian_derive(steepness, value, sum) (-2.0f * sum * value * steepness)
53
54 /* FANN_GAUSSIAN_SYMMETRIC */
55 #define fann_gaussian_symmetric(steepness, sum) ((exp(-sum * steepness * sum * steepness)*2.0)-1.0)
56 #define fann_gaussian_symmetric_real(sum) ((exp(-sum * sum)*2.0)-1.0)
57 #define fann_gaussian_symmetric_derive(steepness, value, sum) (-2.0f * sum * (value+1.0f) * steepness)
58
59 /* FANN_ELLIOT */
60 #define fann_elliot(steepness, sum) (((sum * steepness) / 2.0f) / (1.0f + fann_abs(sum * steepness)) + 0.5f)
61 #define fann_elliot_real(sum) (((sum) / 2.0f) / (1.0f + fann_abs(sum)) + 0.5f)
62 #define fann_elliot_derive(steepness, value, sum) (steepness * 1.0f / (2.0f * (1.0f + fann_abs(sum)) * (1.0f + fann_abs(sum))))
63
64 /* FANN_ELLIOT_SYMMETRIC */
65 #define fann_elliot_symmetric(steepness, sum) ((sum * steepness) / (1.0f + fann_abs(sum * steepness)))
66 #define fann_elliot_symmetric_real(sum) ((sum) / (1.0f + fann_abs(sum)))
67 #define fann_elliot_symmetric_derive(steepness, value, sum) (steepness * 1.0f / ((1.0f + fann_abs(sum)) * (1.0f + fann_abs(sum))))
68
69 #define fann_activation_switch(ann, activation_function, value, result) \
70 switch(activation_function) \
71 { \
72         case FANN_LINEAR: \
73                 result = (fann_type)value; \
74         break; \
75         case FANN_LINEAR_PIECE: \
76                 result = (fann_type)((value < 0) ? 0 : (value > 1) ? 1 : value); \
77         break; \
78         case FANN_LINEAR_PIECE_SYMMETRIC: \
79                 result = (fann_type)((value < -1) ? -1 : (value > 1) ? 1 : value); \
80         break; \
81         case FANN_SIGMOID: \
82                 result = (fann_type)fann_sigmoid_real(value); \
83         break; \
84         case FANN_SIGMOID_SYMMETRIC: \
85                 result = (fann_type)fann_sigmoid_symmetric_real(value); \
86         break; \
87         case FANN_SIGMOID_SYMMETRIC_STEPWISE: \
88                 result = (fann_type)fann_stepwise(-2.64665293693542480469e+00, -1.47221934795379638672e+00, -5.49306154251098632812e-01, 5.49306154251098632812e-01, 1.47221934795379638672e+00, 2.64665293693542480469e+00, -9.90000009536743164062e-01, -8.99999976158142089844e-01, -5.00000000000000000000e-01, 5.00000000000000000000e-01, 8.99999976158142089844e-01, 9.90000009536743164062e-01, -1, 1, value); \
89         break; \
90         case FANN_SIGMOID_STEPWISE: \
91                 result = (fann_type)fann_stepwise(-2.64665246009826660156e+00, -1.47221946716308593750e+00, -5.49306154251098632812e-01, 5.49306154251098632812e-01, 1.47221934795379638672e+00, 2.64665293693542480469e+00, 4.99999988824129104614e-03, 5.00000007450580596924e-02, 2.50000000000000000000e-01, 7.50000000000000000000e-01, 9.49999988079071044922e-01, 9.95000004768371582031e-01, 0, 1, value); \
92         break; \
93         case FANN_THRESHOLD: \
94                 result = (fann_type)((value < 0) ? 0 : 1); \
95         break; \
96         case FANN_THRESHOLD_SYMMETRIC: \
97                 result = (fann_type)((value < 0) ? -1 : 1); \
98         break; \
99         case FANN_GAUSSIAN: \
100                 result = (fann_type)fann_gaussian_real(value); \
101         break; \
102         case FANN_GAUSSIAN_SYMMETRIC: \
103                 result = (fann_type)fann_gaussian_symmetric_real(value); \
104         break; \
105         case FANN_ELLIOT: \
106                 result = (fann_type)fann_elliot_real(value); \
107             break; \
108         case FANN_ELLIOT_SYMMETRIC: \
109                 result = (fann_type)fann_elliot_symmetric_real(value); \
110         break; \
111 }
112
113 #endif