-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactfs.c
171 lines (154 loc) · 3.91 KB
/
actfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*----------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Midas Zhou
-----------------------------------------------------------------------*/
#include "actfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <sys/time.h>
/*-----------------------------------------------
* Note:
* A step transfer function.
* Params:
* @u input param for transfer function;
* Return:
* a double.
------------------------------------------------*/
double func_step(double x, double f, int token)
{
/* Normal func */
if(token==NORMAL_FUNC) {
if(x>=0)
return 1.0;
else
return 0.0;
}
/* Derivative func */
else {
//printf("%s: Derivative function NOT defined!!! \n",__func__);
return 0; ///////////////////////////////// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
/*-------------------------------------------------------------------
* Note:
* A Log-Sigmoid(logistic) function:
* f(x)=1/(1+exp(-ax)).
* f'(x)=a*f(x)*[1-f(-x)].
* Here take a=1.
* Params:
* @x,f input param for function;
* if token==0: x, for sigmoid f(x)
* if token!=0: f=f(x), f'(x)=f(x)(1-f(-x)), where a=1.
* @token 0 ---normal func; 1 ---derivative func
* Return:
* a double.
---------------------------------------------------------------------*/
double func_sigmoid(double x, double f, int token)
{
/* Normal func */
if(token==NORMAL_FUNC) {
return 1.0/(1.0+exp(-x));
}
/* if DERIVATIVE_FUNC Derivative func
* Note: input of f'(u) is f(u), NOT u!!
*/
else {
return f*(1-f);
}
}
/*-------------------------------------------------------------------
* Note:
* A Tan-Sigmoid function:
* f(x)=2/(1+exp(-ax))-1
* f'(x)=a*[1-f(x)^2]/2
* Here take a=2.
*
* ( OR call it tanh, same as tanh=(e^x-e^(-x))/(e^x+e^(-x)) )
*
* Params:
* @x,f input param for function;
* if token==0: x for Tan-Sigmoid f(x)
* if token!=0: f=f(x) for f'(x)=1-f(x)^2 where a=2
* @token 0 ---normal func; 1 ---derivative func
* Return:
* a double.
---------------------------------------------------------------------*/
double func_TanSigmoid(double x, double f, int token)
{
double a=2.0;
/* Normal func */
if(token==NORMAL_FUNC) {
return 2.0/(1.0+exp(-a*x))-1.0;
}
/* if DERIVATIVE_FUNC, Derivative func
* Note: input of f'(u) is f(u), NOT u!!
*/
else {
return a*(1-f*f)/2.0;
}
}
/*-------------------------------------------------------------------
* Note:
* A Rectified Linear Unit (ReLU) function:
* f(x)=0 for x<0
* f(x) =x for x>=0
* f'(x)=0 for x<0
* f'(x)=1 for x>=0
*
* Here take a=2.
* Params:
* @x,f=f(x) input param for function;
* @token 0 ---normal func; 1 ---derivative func
* Return:
* a double.
---------------------------------------------------------------------*/
double func_ReLU(double x, double f, int token)
{
/* Normal func */
if(token==NORMAL_FUNC) {
if(x<0)
return 0.0;
else
return x;
}
/* if DERIVATIVE_FUNC, Derivative func */
else {
if(x<0) return 0.0;
else return 1.0;
}
}
/*-------------------------------------------------------------------
* Note:
* A Parameteric Rectified Linear Unit (ReLU) function:
* f(x)=ax for x<0 a(0-1)
* f(x)=x for x>=0
* f'(x)=a for x<0
* f'(x)=1 for x>=0
*
* Params:
* @x,f=f(x) input param for function;
* @token 0 ---normal func; 1 ---derivative func
* Return:
* a double.
---------------------------------------------------------------------*/
double func_PReLU(double x, double f, int token)
{
double a=0.5;
/* Normal func */
if(token==NORMAL_FUNC) {
if(x<0)
return a*x;
else
return x;
}
/* if DERIVATIVE_FUNC, Derivative func */
else {
if(x<0) return a;
else return 1.0;
}
}