Skip to content

Commit

Permalink
Cast double precision literals to fann_type and added compile time se…
Browse files Browse the repository at this point in the history
…lection of sin, cos and exp or thier single precision variants. This only makes 2% to 3% difference on x86 but is significant on processors that do not have HW double precision support.
  • Loading branch information
Shapedsundew9 committed Jan 17, 2016
1 parent 8cdf3eb commit d407291
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/include/fann_activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,28 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef __fann_activation_h__
#define __fann_activation_h__
/* internal include file, not to be included directly
*/
*/

/* Implementation of the activation functions
*/
*/

/* stepwise linear functions used for some of the activation functions */

/* defines used for the stepwise linear functions */

/* Uses the single precision version of math.h functions if
__doublefann_h__ is not defined
*/
#ifndef __doublefann_h__
#define FANN_EXP(x) expf(x)
#define FANN_SIN(x) sinf(x)
#define FANN_COS(x) cosf(x)
#else
#define FANN_EXP(x) exp(x)
#define FANN_SIN(x) sin(x)
#define FANN_COS(x) cos(x)
#endif

#define fann_linear_func(v1, r1, v2, r2, sum) (((((r2)-(r1)) * ((sum)-(v1)))/((v2)-(v1))) + (r1))
#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))

Expand All @@ -38,22 +51,22 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

/* FANN_SIGMOID */
/* #define fann_sigmoid(steepness, sum) (1.0f/(1.0f + exp(-2.0f * steepness * sum))) */
#define fann_sigmoid_real(sum) (1.0f/(1.0f + exp(-2.0f * sum)))
#define fann_sigmoid_real(sum) (1.0f/(1.0f + FANN_EXP(-2.0f * sum)))
#define fann_sigmoid_derive(steepness, value) (2.0f * steepness * value * (1.0f - value))

/* FANN_SIGMOID_SYMMETRIC */
/* #define fann_sigmoid_symmetric(steepness, sum) (2.0f/(1.0f + exp(-2.0f * steepness * sum)) - 1.0f) */
#define fann_sigmoid_symmetric_real(sum) (2.0f/(1.0f + exp(-2.0f * sum)) - 1.0f)
#define fann_sigmoid_symmetric_real(sum) (2.0f/(1.0f + FANN_EXP(-2.0f * sum)) - 1.0f)
#define fann_sigmoid_symmetric_derive(steepness, value) steepness * (1.0f - (value*value))

/* FANN_GAUSSIAN */
/* #define fann_gaussian(steepness, sum) (exp(-sum * steepness * sum * steepness)) */
#define fann_gaussian_real(sum) (exp(-sum * sum))
#define fann_gaussian_real(sum) (FANN_EXP(-sum * sum))
#define fann_gaussian_derive(steepness, value, sum) (-2.0f * sum * value * steepness * steepness)

/* FANN_GAUSSIAN_SYMMETRIC */
/* #define fann_gaussian_symmetric(steepness, sum) ((exp(-sum * steepness * sum * steepness)*2.0)-1.0) */
#define fann_gaussian_symmetric_real(sum) ((exp(-sum * sum)*2.0f)-1.0f)
#define fann_gaussian_symmetric_real(sum) ((FANN_EXP(-sum * sum)*2.0f)-1.0f)
#define fann_gaussian_symmetric_derive(steepness, value, sum) (-2.0f * sum * (value+1.0f) * steepness * steepness)

/* FANN_ELLIOT */
Expand All @@ -67,19 +80,19 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#define fann_elliot_symmetric_derive(steepness, value, sum) (steepness * 1.0f / ((1.0f + fann_abs(sum)) * (1.0f + fann_abs(sum))))

/* FANN_SIN_SYMMETRIC */
#define fann_sin_symmetric_real(sum) (sin(sum))
#define fann_sin_symmetric_real(sum) (FANN_SIN(sum))
#define fann_sin_symmetric_derive(steepness, sum) (steepness*cos(steepness*sum))

/* FANN_COS_SYMMETRIC */
#define fann_cos_symmetric_real(sum) (cos(sum))
#define fann_cos_symmetric_real(sum) (FANN_COS(sum))
#define fann_cos_symmetric_derive(steepness, sum) (steepness*-sin(steepness*sum))

/* FANN_SIN */
#define fann_sin_real(sum) (sin(sum)/2.0f+0.5f)
#define fann_sin_real(sum) (FANN_SIN(sum)/2.0f+0.5f)
#define fann_sin_derive(steepness, sum) (steepness*cos(steepness*sum)/2.0f)

/* FANN_COS */
#define fann_cos_real(sum) (cos(sum)/2.0f+0.5f)
#define fann_cos_real(sum) (FANN_COS(sum)/2.0f+0.5f)
#define fann_cos_derive(steepness, sum) (steepness*-sin(steepness*sum)/2.0f)

#define fann_activation_switch(activation_function, value, result) \
Expand All @@ -101,10 +114,10 @@ switch(activation_function) \
result = (fann_type)fann_sigmoid_symmetric_real(value); \
break; \
case FANN_SIGMOID_SYMMETRIC_STEPWISE: \
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); \
result = (fann_type)fann_stepwise(((fann_type)-2.64665293693542480469e+00), ((fann_type)-1.47221934795379638672e+00), ((fann_type)-5.49306154251098632812e-01), ((fann_type)5.49306154251098632812e-01), ((fann_type)1.47221934795379638672e+00), ((fann_type)2.64665293693542480469e+00), ((fann_type)-9.90000009536743164062e-01), ((fann_type)-8.99999976158142089844e-01), ((fann_type)-5.00000000000000000000e-01), ((fann_type)5.00000000000000000000e-01), ((fann_type)8.99999976158142089844e-01), ((fann_type)9.90000009536743164062e-01), -1, 1, value); \
break; \
case FANN_SIGMOID_STEPWISE: \
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); \
result = (fann_type)fann_stepwise(((fann_type)-2.64665246009826660156e+00), ((fann_type)-1.47221946716308593750e+00), ((fann_type)-5.49306154251098632812e-01), ((fann_type)5.49306154251098632812e-01), ((fann_type)1.47221934795379638672e+00), ((fann_type)2.64665293693542480469e+00), ((fann_type) 4.99999988824129104614e-03), ((fann_type) 5.00000007450580596924e-02), ((fann_type) 2.50000000000000000000e-01), ((fann_type)7.50000000000000000000e-01), ((fann_type)9.49999988079071044922e-01), ((fann_type)9.95000004768371582031e-01), 0, 1, value); \
break; \
case FANN_THRESHOLD: \
result = (fann_type)((value < 0) ? 0 : 1); \
Expand Down

0 comments on commit d407291

Please sign in to comment.