acs.c

来自「NIST Handwriting OCR Testbed」· C语言 代码 · 共 129 行

C
129
字号
/* Some activation functions suitable for use by e_and_g.  Each musthave the following form: return value is void, and function takesthree args, the first arg being the input value (float) and the secondand third being the output function value and output derivative value(float pointers).The functions implemented here so far, all have the same derivative atzero, namely 1/4.  This is a normalization that makes for bettercomparison tests between nets using the various functions.Contains:# proc: ac_sinusoid - MLP sinusoid activation function (returns derivative).# proc: ac_v_sinusoid - MLP sinusoid activation function (activation only).# proc: ac_sigmoid - MLP sigmoid (logistic) activation function (returns# proc:              derivative).# proc: ac_v_sigmoid - MLP sigmoid (logistic) activation function (activation# proc:              only).# proc: ac_linear - MLP linear activaiton function (returns derivative).# proc: ac_v_linear - MLP linear activaiton function (activation only).*/#include <math.h>/*******************************************************************//* Sinusoid activation function and its derivative.  The scaling by .5before taking the sine, and the adding of 1 and scaling by .5 aftertaking the sine, cause this function to have the following desirableproperties: range is [0, 1] (almost same as range of sigmoid, which is(0,1)); value at 0 is 1/2 (like sigmoid); and derivative at 0 is 1/4(like sigmoid). */voidac_sinusoid(x, val, deriv)float x, *val, *deriv;{  double s, c, a;#ifndef HAVE_SINCOS  *val = .5 * (1. + (float)sin(a = .5 * x));  *deriv = .25 * (float)cos(a);#else  /* If sincos exists, using it is presumably going to be faster than  calling sin and cos. */  sincos(.5 * x, &s, &c);  *val = .5 * (1. + (float)s);  *deriv = .25 * (float)c;#endif}/*******************************************************************//* Sinusoid activation function, value only.Input/output arg:  p: The address for input of the value, and for output of the    result of applying the activation function to this value.*/voidac_v_sinusoid(p)float *p;{  *p = .5 * (1. + (float)sin((double)(.5 * *p)));}/*******************************************************************//* Sigmoid activation function (also called the logistic function) andits derivative.  (The idea with SMIN is that it is a large-magnitudenegative number, such that exp(-SMIN), a large positive number, justbarely avoids overflow.) */#define SMIN -1.e6 /* ok for now */voidac_sigmoid(x, val, deriv)float x, *val, *deriv;{  float v;  *val = v = (x >= SMIN ? 1. / (1. + (float)exp(-x)) : 0.);  *deriv = v * (1. - v);}/*******************************************************************//* Sigmoid (also called logistic) activation function, value only.Input/output arg:  p: The address for input of the value, and for output of the    result of applying the activation function to this value.*/voidac_v_sigmoid(p)float *p;{  *p = (*p >= SMIN ? 1. / (1. + (float)exp(-*p)) : 0.);}/*******************************************************************//* A linear activation function and its derivative. */voidac_linear(x, val, deriv)float x, *val, *deriv;{  *val = .25 * (float)x;  *deriv = .25;}/*******************************************************************//* Linear activation function, value only.Input/output arg:  p: The address for input of the value, and for output of the    result of applying the activation function to this value.*/voidac_v_linear(p)float *p;{  *p = .25 * (float)*p;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?