#ifndef ACTIVATIONS_H #define ACTIVATIONS_H #include "darknet.h" #include "dn_cuda.h" #include "math.h" namespace DN{ ACTIVATION get_activation(char *s); char *get_activation_string(ACTIVATION a); float activate(float x, ACTIVATION a); float gradient(float x, ACTIVATION a); void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta); void activate_array(float *x, const int n, const ACTIVATION a); #ifdef GPU void activate_array_gpu(float *x, int n, ACTIVATION a); void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta); #endif static __inline float stair_activate(float x) { int n = (int)floor(x); if (n % 2 == 0) return (float)floor(x / 2.); else return (float)((x - n) + floor(x / 2.)); } static __inline float hardtan_activate(float x) { if (x < -1) return -1; if (x > 1) return 1; return x; } static __inline float linear_activate(float x){ return x; } static __inline float logistic_activate(float x){ return (float)(1. / (1. + exp(-x))); } static __inline float loggy_activate(float x){ return (float)(2. / (1. + exp(-x)) - 1); } static __inline float relu_activate(float x){ return x*(x > 0); } static __inline float elu_activate(float x){ return (float)((x >= 0)*x + (x < 0)*(exp(x) - 1)); } static __inline float relie_activate(float x){ return (float)((x>0) ? x : .01*x); } static __inline float ramp_activate(float x){ return (float)(x*(x > 0) + .1*x); } static __inline float leaky_activate(float x){ return (float)((x > 0) ? x : .1*x); } static __inline float tanh_activate(float x){ return (float)((exp(2 * x) - 1) / (exp(2 * x) + 1)); } static __inline float plse_activate(float x) { if (x < -4) return (float)(.01 * (x + 4)); if (x > 4) return (float)(.01 * (x - 4) + 1); return (float)(.125*x + .5); } static __inline float lhtan_activate(float x) { if (x < 0) return (float)(.001*x); if (x > 1) return (float)(.001*(x - 1) + 1); return x; } static __inline float lhtan_gradient(float x) { if (x > 0 && x < 1) return (float)1; return (float).001; } static __inline float hardtan_gradient(float x) { if (x > -1 && x < 1) return (float)1; return (float)0; } static __inline float linear_gradient(float x){ return (float)1; } static __inline float logistic_gradient(float x){ return (float)((1 - x)*x); } static __inline float loggy_gradient(float x) { float y = (float)((x + 1.) / 2.); return (float)(2 * (1 - y)*y); } static __inline float stair_gradient(float x) { if (floor(x) == x) return (float)0; return (float)1; } static __inline float relu_gradient(float x){ return (float)(x > 0); } static __inline float elu_gradient(float x){ return (float)((x >= 0) + (x < 0)*(x + 1)); } static __inline float relie_gradient(float x){ return (float)((x>0) ? 1 : .01); } static __inline float ramp_gradient(float x){ return (float)((x > 0) + .1); } static __inline float leaky_gradient(float x){ return (float)((x > 0) ? 1 : .1); } static __inline float tanh_gradient(float x){ return (float)(1 - x*x); } static __inline float plse_gradient(float x){ return (float)((x < 0 || x > 1) ? .01 : .125); } }; #endif