darknet/src/activations.h

88 lines
2.9 KiB
C
Raw Normal View History

#ifndef ACTIVATIONS_H
#define ACTIVATIONS_H
2017-06-02 06:31:13 +03:00
#include "darknet.h"
2015-04-15 11:04:38 +03:00
#include "cuda.h"
#include "math.h"
ACTIVATION get_activation(char *s);
2013-12-03 04:41:40 +04:00
2014-02-14 22:26:31 +04:00
char *get_activation_string(ACTIVATION a);
2014-08-08 23:04:15 +04:00
float activate(float x, ACTIVATION a);
float gradient(float x, ACTIVATION a);
2014-05-01 03:17:40 +04:00
void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta);
2014-08-08 23:04:15 +04:00
void activate_array(float *x, const int n, const ACTIVATION a);
2014-05-10 02:14:52 +04:00
#ifdef GPU
2017-06-18 23:05:37 +03:00
void activate_array_gpu(float *x, int n, ACTIVATION a);
void gradient_array_gpu(float *x, int n, ACTIVATION a, float *delta);
2014-05-10 02:14:52 +04:00
#endif
2016-05-07 02:25:16 +03:00
static inline float stair_activate(float x)
{
int n = floor(x);
if (n%2 == 0) return floor(x/2.);
else return (x - n) + floor(x/2.);
}
2016-06-10 03:20:31 +03:00
static inline float hardtan_activate(float x)
{
if (x < -1) return -1;
if (x > 1) return 1;
return x;
}
2015-03-08 21:25:28 +03:00
static inline float linear_activate(float x){return x;}
2015-03-08 21:31:12 +03:00
static inline float logistic_activate(float x){return 1./(1. + exp(-x));}
2016-02-01 02:52:03 +03:00
static inline float loggy_activate(float x){return 2./(1. + exp(-x)) - 1;}
2015-03-08 21:25:28 +03:00
static inline float relu_activate(float x){return x*(x>0);}
2015-11-26 22:48:01 +03:00
static inline float elu_activate(float x){return (x >= 0)*x + (x < 0)*(exp(x)-1);}
2018-09-05 03:25:05 +03:00
static inline float selu_activate(float x){return (x >= 0)*1.0507*x + (x < 0)*1.0507*1.6732*(exp(x)-1);}
2016-09-08 08:27:56 +03:00
static inline float relie_activate(float x){return (x>0) ? x : .01*x;}
2015-03-08 21:25:28 +03:00
static inline float ramp_activate(float x){return x*(x>0)+.1*x;}
2015-07-08 10:36:43 +03:00
static inline float leaky_activate(float x){return (x>0) ? x : .1*x;}
2015-03-08 21:25:28 +03:00
static inline float tanh_activate(float x){return (exp(2*x)-1)/(exp(2*x)+1);}
2015-03-21 22:25:14 +03:00
static inline float plse_activate(float x)
{
if(x < -4) return .01 * (x + 4);
if(x > 4) return .01 * (x - 4) + 1;
return .125*x + .5;
}
2015-03-08 21:25:28 +03:00
2016-06-20 23:18:59 +03:00
static inline float lhtan_activate(float x)
{
if(x < 0) return .001*x;
if(x > 1) return .001*(x-1) + 1;
return x;
}
static inline float lhtan_gradient(float x)
{
if(x > 0 && x < 1) return 1;
return .001;
}
2016-06-10 03:20:31 +03:00
static inline float hardtan_gradient(float x)
{
if (x > -1 && x < 1) return 1;
return 0;
}
2015-03-08 21:25:28 +03:00
static inline float linear_gradient(float x){return 1;}
2015-03-08 21:31:12 +03:00
static inline float logistic_gradient(float x){return (1-x)*x;}
2016-02-01 02:52:03 +03:00
static inline float loggy_gradient(float x)
{
float y = (x+1.)/2.;
return 2*(1-y)*y;
}
2016-05-07 02:25:16 +03:00
static inline float stair_gradient(float x)
{
if (floor(x) == x) return 0;
return 1;
}
2015-03-08 21:25:28 +03:00
static inline float relu_gradient(float x){return (x>0);}
2015-11-26 22:48:01 +03:00
static inline float elu_gradient(float x){return (x >= 0) + (x < 0)*(x + 1);}
2018-09-05 03:25:05 +03:00
static inline float selu_gradient(float x){return (x >= 0)*1.0507 + (x < 0)*(x + 1.0507*1.6732);}
2015-04-11 11:24:07 +03:00
static inline float relie_gradient(float x){return (x>0) ? 1 : .01;}
2015-03-08 21:25:28 +03:00
static inline float ramp_gradient(float x){return (x>0)+.1;}
2015-07-08 10:36:43 +03:00
static inline float leaky_gradient(float x){return (x>0) ? 1 : .1;}
2015-03-08 21:25:28 +03:00
static inline float tanh_gradient(float x){return 1-x*x;}
2015-03-21 22:25:14 +03:00
static inline float plse_gradient(float x){return (x < 0 || x > 1) ? .01 : .125;}
2015-03-08 21:25:28 +03:00
#endif