-
Notifications
You must be signed in to change notification settings - Fork 1
/
activation_functions.py
157 lines (133 loc) · 4.46 KB
/
activation_functions.py
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
#!/usr/bin/python
"""
Created on July 11, 2018
@author: flg-ma
@attention: compare different activation functions for ML
@contact: albus.marcel@gmail.com (Marcel Albus)
@version: 1.1.0
#############################################################################################
History:
- v1.1.0: - updated axis ticks
- added new functions
- v1.0.0: first init
"""
import numpy as np
from matplotlib import pyplot as plt
class ActivationFunctions():
""" Class with different activation functions for NN implemented """
def __init__(self):
self.functions = ['ReLU', 'Softsign', 'PReLU', 'Sigmoid', 'ELU']
def ReLU(self, data):
"""
$f(x) = \begin{cases}
0 & \text{für } x < 0 \\
x & \text{für } x >= 0
\end{cases}$
returns the given data as ReLU (Rectified linear unit) function
data: numpy array
return: numpy array as ReLU function
"""
data[data < 0] = 0
return data
def softsign(self, data):
"""
$f(x) = \frac{x}{1 + \abs{x}}$
returns the given data as softsign function
data: numpy array
return: numpy array as softsign function
"""
return (data / (1 + np.abs(data)))
def PReLU(self, data, alpha):
"""
$f(x) = \begin{cases}
\alpha \cdot 0 & \text{für } x < 0 \\
x & \text{für } x >= 0
\end{cases}$
returns the given data as PReLU (parametric rectified linear unit) function
data: numpy array
return: numpy array as PReLU function
"""
data[ data >= 0] = alpha * data[ data >= 0]
return data
def sigmoid(self, data):
"""
$f(x) = \frac{1}{1 + exp{-x}}$
returns the given data as sigmoid (logistic) function
data: numpy array
return: numpy array as sigmoid function
"""
return (1 / (1 + np.exp(- data)))
def ELU(self, data, alpha):
"""
$f(x) = \begin{cases}
\alpha (\exp{x} - 1) & \text{für } x < 0 \\
x & \text{für } x >= 0
\end{cases}$
returns the given data as ELU (exponential linear unit) function
data: numpy array
return: numpy array as ELU function
"""
data[ data < 0] = alpha * (np.exp(data[ data < 0]) - 1)
return data
def softmax(self, data):
# TODO: implement
pass
def log_softmax(self, data):
# TODO: implement
pass
X_MIN = -5
X_MAX = 5
Y_MIN = -5
Y_MAX = 5
A = np.linspace(X_MIN, X_MAX, 1000)
x = [a for a in np.linspace(X_MIN, X_MAX, len(A))]
ticks = [a for a in range(X_MIN, X_MAX + 1)]
if __name__ == '__main__':
af = ActivationFunctions()
###########
# plotting
###########
fig, ax = plt.subplots(2, 3, figsize=(12, 8))
fig.suptitle('Activation Functions', fontsize=15, fontweight='normal')
ax[0, 0].plot(x, A.copy(), 'b')
ax[0, 0].legend(['Normal'])
ax[0, 0].axis([X_MIN, X_MAX, Y_MIN, Y_MAX])
ax[0, 0].grid()
ax[0, 0].set_yticks(ticks)
ax[0, 0].set_xticks(ticks)
ax[0, 1].plot(x, af.softsign(A.copy()), 'b')
ax[0, 1].legend(['Softsign'])
ax[0, 1].axis([X_MIN, X_MAX, Y_MIN, Y_MAX])
ax[0, 1].grid()
ax[0, 1].set_yticks(ticks)
ax[0, 1].set_xticks(ticks)
alpha = 2.0
ax[0, 2].plot(x, af.PReLU(A.copy(), alpha), 'b')
ax[0, 2].legend(['PReLU\nAlpha: ' + str(alpha)])
ax[0, 2].axis([X_MIN, X_MAX, Y_MIN, Y_MAX])
ax[0, 2].grid()
ax[0, 2].set_yticks(ticks)
ax[0, 2].set_xticks(ticks)
ax[1, 0].plot(x, af.ReLU(A.copy()), 'r')
ax[1, 0].legend(['ReLU'])
ax[1, 0].axis([X_MIN, X_MAX, Y_MIN, Y_MAX])
ax[1, 0].grid()
ax[1, 0].set_yticks(ticks)
ax[1, 0].set_xticks(ticks)
ax[1, 1].plot(x, af.sigmoid(A.copy()), 'r')
ax[1, 1].legend(['Sigmoid'])
ax[1, 1].axis([X_MIN, X_MAX, Y_MIN, Y_MAX])
ax[1, 1].grid()
ax[1, 1].set_yticks(ticks)
ax[1, 1].set_xticks(ticks)
beta = .7
ax[1, 2].plot(x, af.ELU(A.copy(), beta), 'r')
ax[1, 2].legend(['ELU\nAlpha: ' + str(beta)])
ax[1, 2].axis([X_MIN, X_MAX, Y_MIN, Y_MAX])
ax[1, 2].grid()
ax[1, 2].set_yticks(ticks)
ax[1, 2].set_xticks(ticks)
# rect = [left, bottom, right, top] in the normalized figure coordinate
# that the whole subplots area (including labels) will fit into. Default is [0, 0, 1, 1].
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.show()