-
Notifications
You must be signed in to change notification settings - Fork 11
/
activations.py
120 lines (81 loc) · 2.31 KB
/
activations.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
import theano
import theano.tensor as T
class Softmax(object):
def __init__(self):
pass
def __call__(self, x):
e_x = T.exp(x - x.max(axis=1).dimshuffle(0, 'x'))
return e_x / e_x.sum(axis=1).dimshuffle(0, 'x')
class ConvSoftmax(object):
def __init__(self):
pass
def __call__(self, x):
e_x = T.exp(x - x.max(axis=1, keepdims=True))
return e_x / e_x.sum(axis=1, keepdims=True)
class Maxout(object):
def __init__(self, n_pool=2):
self.n_pool = n_pool
def __call__(self, x):
if x.ndim == 2:
x = T.max([x[:, n::self.n_pool] for n in range(self.n_pool)], axis=0)
elif x.ndim == 4:
x = T.max([x[:, n::self.n_pool, :, :] for n in range(self.n_pool)], axis=0)
else:
raise NotImplementedError
return x
class Rectify(object):
def __init__(self):
pass
def __call__(self, x):
return (x + abs(x)) / 2.0
class ClippedRectify(object):
def __init__(self, clip=10.):
self.clip = clip
def __call__(self, x):
return T.clip((x + abs(x)) / 2.0, 0., self.clip)
class LeakyRectify(object):
def __init__(self, leak=0.2):
self.leak = leak
def __call__(self, x):
f1 = 0.5 * (1 + self.leak)
f2 = 0.5 * (1 - self.leak)
return f1 * x + f2 * abs(x)
class Prelu(object):
def __init__(self):
pass
def __call__(self, x, leak):
if x.ndim == 4:
leak = leak.dimshuffle('x', 0, 'x', 'x')
f1 = 0.5 * (1 + leak)
f2 = 0.5 * (1 - leak)
return f1 * x + f2 * abs(x)
class Tanh(object):
def __init__(self):
pass
def __call__(self, x):
return T.tanh(x)
class Sigmoid(object):
def __init__(self):
pass
def __call__(self, x):
return T.nnet.sigmoid(x)
class Linear(object):
def __init__(self):
pass
def __call__(self, x):
return x
class HardSigmoid(object):
def __init__(self):
pass
def __call__(self, X):
return T.clip(X + 0.5, 0., 1.)
class TRec(object):
def __init__(self, t=1):
self.t = t
def __call__(self, X):
return X*(X > self.t)
class HardTanh(object):
def __init__(self):
pass
def __call__(self, X):
return T.clip(X, -1., 1.)