-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivation.py
87 lines (68 loc) · 1.82 KB
/
activation.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
import numpy as np
def sigmoid(x, derivative=False):
if derivative:
return x*(1-x)
return 1/(1+np.exp(-x))
def tanh(x, derivative=False):
if derivative:
return 1-(x**2)
return np.tanh(x)
def linear(x, derivative=False):
if derivative:
return 1
return x
def relu(x, derivative=False):
result = x.copy()
if derivative:
result[result > 0] = 1.0
result[result < 0] = 0.0
return result
result[result < 0] = 0.0
return result
def arctan(x, derivative=False):
if derivative:
return np.cos(x)**2
return np.arctan(x)
def step(x, derivative=False):
if derivative:
for a in range(len(x)):
for b in range(len(x[a])):
if x[a][b] > 0:
x[a][b] = 1
else:
x[a][b] = 0
return x
for a in range(len(x)):
for b in range(len(x[a])):
if x[a][b] > 0:
x[a][b] = 1
else:
x[a][b] = 0
return x
def squash(x, derivative=False):
if derivative:
for a in range(len(x)):
for b in range(len(x[a])):
if x[a][b] > 0:
x[a][b] = x[a][b] / (1+x[a][b])
else:
x[a][b] = x[a][b] / (1-x[a][b])
return x
for a in range(len(x)):
for b in range(len(x[a])):
x[a][b] = x[a][b] / (1+abs(x[a][b]))
return x
def gaussian(x, derivative=False):
if derivative:
for a in range(len(x)):
for b in range(len(x[a])):
x[a][b] = -2*x[a][b] * np.exp(-x[a][b]**2)
for a in range(len(x)):
for b in range(len(x[a])):
x[a][b] = np.exp(-x[a][b]**2)
return x
func = {
'sigmoid': sigmoid,
'relu': relu,
'linear': linear
}