Go to the documentation of this file.00001 #include "activations.h"
00002
00003 #include <math.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <string.h>
00007
00008 char *get_activation_string(ACTIVATION a)
00009 {
00010 switch(a){
00011 case LOGISTIC:
00012 return "logistic";
00013 case LOGGY:
00014 return "loggy";
00015 case RELU:
00016 return "relu";
00017 case ELU:
00018 return "elu";
00019 case RELIE:
00020 return "relie";
00021 case RAMP:
00022 return "ramp";
00023 case LINEAR:
00024 return "linear";
00025 case TANH:
00026 return "tanh";
00027 case PLSE:
00028 return "plse";
00029 case LEAKY:
00030 return "leaky";
00031 case STAIR:
00032 return "stair";
00033 case HARDTAN:
00034 return "hardtan";
00035 case LHTAN:
00036 return "lhtan";
00037 default:
00038 break;
00039 }
00040 return "relu";
00041 }
00042
00043 ACTIVATION get_activation(char *s)
00044 {
00045 if (strcmp(s, "logistic")==0) return LOGISTIC;
00046 if (strcmp(s, "loggy")==0) return LOGGY;
00047 if (strcmp(s, "relu")==0) return RELU;
00048 if (strcmp(s, "elu")==0) return ELU;
00049 if (strcmp(s, "relie")==0) return RELIE;
00050 if (strcmp(s, "plse")==0) return PLSE;
00051 if (strcmp(s, "hardtan")==0) return HARDTAN;
00052 if (strcmp(s, "lhtan")==0) return LHTAN;
00053 if (strcmp(s, "linear")==0) return LINEAR;
00054 if (strcmp(s, "ramp")==0) return RAMP;
00055 if (strcmp(s, "leaky")==0) return LEAKY;
00056 if (strcmp(s, "tanh")==0) return TANH;
00057 if (strcmp(s, "stair")==0) return STAIR;
00058 fprintf(stderr, "Couldn't find activation function %s, going with ReLU\n", s);
00059 return RELU;
00060 }
00061
00062 float activate(float x, ACTIVATION a)
00063 {
00064 switch(a){
00065 case LINEAR:
00066 return linear_activate(x);
00067 case LOGISTIC:
00068 return logistic_activate(x);
00069 case LOGGY:
00070 return loggy_activate(x);
00071 case RELU:
00072 return relu_activate(x);
00073 case ELU:
00074 return elu_activate(x);
00075 case RELIE:
00076 return relie_activate(x);
00077 case RAMP:
00078 return ramp_activate(x);
00079 case LEAKY:
00080 return leaky_activate(x);
00081 case TANH:
00082 return tanh_activate(x);
00083 case PLSE:
00084 return plse_activate(x);
00085 case STAIR:
00086 return stair_activate(x);
00087 case HARDTAN:
00088 return hardtan_activate(x);
00089 case LHTAN:
00090 return lhtan_activate(x);
00091 }
00092 return 0;
00093 }
00094
00095 void activate_array(float *x, const int n, const ACTIVATION a)
00096 {
00097 int i;
00098 for(i = 0; i < n; ++i){
00099 x[i] = activate(x[i], a);
00100 }
00101 }
00102
00103 float gradient(float x, ACTIVATION a)
00104 {
00105 switch(a){
00106 case LINEAR:
00107 return linear_gradient(x);
00108 case LOGISTIC:
00109 return logistic_gradient(x);
00110 case LOGGY:
00111 return loggy_gradient(x);
00112 case RELU:
00113 return relu_gradient(x);
00114 case ELU:
00115 return elu_gradient(x);
00116 case RELIE:
00117 return relie_gradient(x);
00118 case RAMP:
00119 return ramp_gradient(x);
00120 case LEAKY:
00121 return leaky_gradient(x);
00122 case TANH:
00123 return tanh_gradient(x);
00124 case PLSE:
00125 return plse_gradient(x);
00126 case STAIR:
00127 return stair_gradient(x);
00128 case HARDTAN:
00129 return hardtan_gradient(x);
00130 case LHTAN:
00131 return lhtan_gradient(x);
00132 }
00133 return 0;
00134 }
00135
00136 void gradient_array(const float *x, const int n, const ACTIVATION a, float *delta)
00137 {
00138 int i;
00139 for(i = 0; i < n; ++i){
00140 delta[i] *= gradient(x[i], a);
00141 }
00142 }
00143