-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhyperbolic_nonlinearities.py
350 lines (280 loc) · 13.4 KB
/
hyperbolic_nonlinearities.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import keras.backend as K
import numpy as np
from keras.engine import Layer
#from keras import initializations
def _hyperbolicReLU(x, tau):
return (x + K.sqrt(x**2 + tau**2) ) /2
def _assymetricBiHyperbolic(x, lmbda, tau_1, tau_2):
return K.sqrt(lmbda**2 * (x + 1 / (4*lmbda))**2 + tau_1**2) - K.sqrt(lmbda**2 * (x - 1 / (4*lmbda))**2 + tau_2**2) + 1 / 2
def _ext_assymetricBiHyperbolic_old(x, lmbda, tau_1, tau_2):
return 2*(K.sqrt(lmbda**2 * (x + 1 / (4*lmbda))**2 + tau_1**2) - K.sqrt(lmbda**2 * (x - 1 / (4*lmbda))**2 + tau_2**2))
def _ext_assymetricBiHyperbolic(x, lmbda, tau_1, tau_2):
return K.sqrt(lmbda**2 * (x + 1 / (2*lmbda))**2 + tau_1**2) - K.sqrt(lmbda**2 * (x - 1 / (2*lmbda))**2 + tau_2**2)
def _biHyperbolic(x, lmbda, tau):
return _assymetricBiHyperbolic(x, lmbda, tau, tau)
def _ext_biHyperbolic(x, lmbda, tau):
return _ext_assymetricBiHyperbolic(x, lmbda, tau, tau)
def _hyperbolic(x, rho):
return 0.5 * (1 + x/K.sqrt(x**2 + 4*rho**2) )
def _ext_hyperbolic(x, rho):
return x / K.sqrt(x**2 + rho**2)
class HyperbolicReLU(Layer):
def __init__(self, tau, **kwargs):
self.supports_masking = True
self.tau = K.variable(tau)
super(HyperbolicReLU, self).__init__(**kwargs)
def call(self, x, mask=None):
return _hyperbolicReLU(x, self.tau)
def get_config(self):
config = {'tau': self.tau}
base_config = super(HyperbolicReLU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class AssymetricBiHyperbolic(Layer):
def __init__(self, lmbda, tau_1, tau_2, **kwargs):
self.supports_masking = True
self.lmbda = K.variable(lmbda)
self.tau_1 = K.variable(tau_1)
self.tau_2 = K.variable(tau_2)
super(BiHyperbolic, self).__init__(**kwargs)
def call(self, x, mask=None):
return _assymetricBiHyperbolic(x, self.lmbda, self.tau_1, self.tau_2)
def get_config(self):
config = {'lmbda': self.lmbda,
'tau_1': self.tau_1,
'tau_2': self.tau_2}
base_config = super(BiHyperbolic, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class BiHyperbolic(Layer):
def __init__(self, lmbda, tau, mode='ext', **kwargs):
self.supports_masking = True
self.lmbda = K.variable(lmbda)
self.tau = K.variable(tau)
self.mode = mode
super(BiHyperbolic, self).__init__(**kwargs)
def call(self, x, mask=None):
if self.mode == 'basic':
return _biHyperbolic(x, self.lmbda, self.tau)
if self.mode == 'ext':
return _ext_biHyperbolic(x, self.lmbda, self.tau)
def get_config(self):
config = {'lmbda': self.lmbda,
'tau': self.tau,
'mode': self.mode}
base_config = super(BiHyperbolic, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class Hyperbolic(Layer):
def __init__(self, rho, mode='ext', **kwargs):
self.supports_masking = True
self.rho = K.variable(rho)
self.mode = mode
super(Hyperbolic, self).__init__(**kwargs)
def call(self, x, mask=None):
if self.mode == 'basic':
return _hyperbolic(x, self.rho)
else:
return _ext_hyperbolic(x, self.rho)
def get_config(self):
config = {'rho': self.rho, 'mode': self.mode}
base_config = super(Hyperbolic, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
# class AdaptativeAssymetricBiHyperbolic(Layer):
# def __init__(self, lmbda_init='one', tau_1_init='glorot_normal', tau_2_init='glorot_normal', mode='ext', shared_axes=None, weights=None, **kwargs):
# self.supports_masking = True
# self.lmbda_init = lmbda_init
# self.tau_1_init = tau_1_init
# self.tau_2_init = tau_2_init
# self.mode = mode
# self.initial_weights = weights
# if not isinstance(shared_axes, (list, tuple)):
# self.shared_axes = [shared_axes]
# else:
# self.shared_axes = list(shared_axes)
# super(AdaptativeAssymetricBiHyperbolic, self).__init__(**kwargs)
# def build(self, input_shape):
# #input_shape = input_shape[1:]
# param_shape = list(input_shape[1:])
# self.param_broadcast = [False] * len(param_shape)
# if self.shared_axes[0] is not None:
# for i in self.shared_axes:
# param_shape[i - 1] = 1
# self.param_broadcast[i - 1] = True
# """
# self.lmbda = K.variable(self.lambda_init * np.ones(input_shape),
# name='{}_lambda'.format(self.name))
# self.tau_1 = K.variable(self.tau_1_init * np.ones(input_shape),
# name='{}_tau_1'.format(self.name))
# self.tau_2 = K.variable(self.tau_2_init * np.ones(input_shape),
# name='{}_tau_2'.format(self.name))
# """
# lmbda_init = initializations.get(self.lmbda_init)
# tau_1_init = initializations.get(self.tau_1_init)
# tau_2_init = initializations.get(self.tau_2_init)
# self.lmbda = lmbda_init(param_shape,
# name='{}_lmbda'.format(self.name))
# self.tau_1 = tau_1_init(param_shape,
# name='{}_tau_1'.format(self.name))
# self.tau_2 = tau_2_init(param_shape,
# name='{}_tau_2'.format(self.name))
# self.trainable_weights = [self.lmbda, self.tau_1, self.tau_2]
# if self.initial_weights is not None:
# self.set_weights(self.initial_weights)
# del self.initial_weights
# def call(self, x, mask=None):
# if self.mode == 'basic':
# return _assymetricBiHyperbolic(x, self.lmbda, self.tau_1, self.tau_2)
# return _ext_assymetricBiHyperbolic(x, self.lmbda, self.tau_1, self.tau_2)
# def get_config(self):
# config = {'lmbda_init': self.lambda_init,
# 'tau_1_init': self.tau_1_init,
# 'tau_2_init': self.tau_2_init}
# base_config = super(AdaptativeAssymetricBiHyperbolic, self).get_config()
# return dict(list(base_config.items()) + list(config.items()))
# class AdaptativeBiHyperbolic(Layer):
# def __init__(self, lmbda_init='one', tau_init='glorot_normal', mode='ext', shared_axes=None, weights=None, **kwargs):
# self.supports_masking = True
# self.lmbda_init = lmbda_init
# self.tau_init = tau_init
# self.mode = mode
# self.initial_weights = weights
# if not isinstance(shared_axes, (list, tuple)):
# self.shared_axes = [shared_axes]
# else:
# self.shared_axes = list(shared_axes)
# super(AdaptativeBiHyperbolic, self).__init__(**kwargs)
# def build(self, input_shape):
# #input_shape = input_shape[1:]
# param_shape = list(input_shape[1:])
# self.param_broadcast = [False] * len(param_shape)
# if self.shared_axes[0] is not None:
# for i in self.shared_axes:
# param_shape[i - 1] = 1
# self.param_broadcast[i - 1] = True
# lmbda_init = initializations.get(self.lmbda_init)
# tau_init = initializations.get(self.tau_init)
# self.lmbda = lmbda_init(param_shape,
# name='{}_lmbda'.format(self.name))
# self.tau = tau_init(param_shape,
# name='{}_tau'.format(self.name))
# self.trainable_weights = [self.lmbda, self.tau]
# if self.initial_weights is not None:
# self.set_weights(self.initial_weights)
# del self.initial_weights
# def call(self, x, mask=None):
# if self.mode == 'basic':
# return _biHyperbolic(x, self.lmbda, self.tau)
# return _ext_biHyperbolic(x, self.lmbda, self.tau)
# def get_config(self):
# config = {'lmbda_init': self.lambda_init,
# 'tau_init': self.tau_init}
# base_config = super(AdaptativeBiHyperbolic, self).get_config()
# return dict(list(base_config.items()) + list(config.items()))
# class AdaptativeHyperbolic(Layer):
# def __init__(self, rho_init='glorot_normal', mode='ext', shared_axes=None, weights=None, **kwargs):
# self.supports_masking = True
# self.rho_init = rho_init
# self.mode = mode
# self.initial_weights = weights
# if not isinstance(shared_axes, (list, tuple)):
# self.shared_axes = [shared_axes]
# else:
# self.shared_axes = list(shared_axes)
# super(AdaptativeHyperbolic, self).__init__(**kwargs)
# def build(self, input_shape):
# #input_shape = input_shape[1:]
# param_shape = list(input_shape[1:])
# self.param_broadcast = [False] * len(param_shape)
# if self.shared_axes[0] is not None:
# for i in self.shared_axes:
# param_shape[i - 1] = 1
# self.param_broadcast[i - 1] = True
# rho_init = initializations.get(self.rho_init)
# self.rho = rho_init(param_shape,
# name='{}_rho'.format(self.name))
# self.trainable_weights = [self.rho]
# if self.initial_weights is not None:
# self.set_weights(self.initial_weights)
# del self.initial_weights
# def call(self, x, mask=None):
# if self.mode == 'basic':
# return _hyperbolic(x, self.rho)
# return _ext_hyperbolic(x, self.rho)
# def get_config(self):
# config = {'rho_init': self.rho_init}
# base_config = super(AdaptativeHyperbolic, self).get_config()
# return dict(list(base_config.items()) + list(config.items()))
# class AdaptativeHyperbolicReLU(Layer):
# '''Parametric Hyperbolic Smoothing Rectifier
# '''
# def __init__(self, tau_init='glorot_normal', shared_axes=None, weights=None, **kwargs):
# self.supports_masking = True
# self.tau_init = tau_init
# self.initial_weights = weights
# if not isinstance(shared_axes, (list, tuple)):
# self.shared_axes = [shared_axes]
# else:
# self.shared_axes = list(shared_axes)
# super(AdaptativeHyperbolicReLU, self).__init__(**kwargs)
# def build(self, input_shape):
# #input_shape = input_shape[1:]
# param_shape = list(input_shape[1:])
# self.param_broadcast = [False] * len(param_shape)
# if self.shared_axes[0] is not None:
# for i in self.shared_axes:
# param_shape[i - 1] = 1
# self.param_broadcast[i - 1] = True
# tau_init = initializations.get(self.tau_init)
# self.tau = tau_init(param_shape,
# name='{}_tau'.format(self.name))
# self.trainable_weights = [self.tau]
# if self.initial_weights is not None:
# self.set_weights(self.initial_weights)
# del self.initial_weights
# def call(self, x, mask=None):
# return _hyperbolicReLU(x, self.tau)
# def get_config(self):
# config = {'tau_init': self.tau_init}
# base_config = super(AdaptativeHyperbolicReLU, self).get_config()
# return dict(list(base_config.items()) + list(config.items()))
class PELU(Layer):
'''Parametric Exponential Linear Unit
`f(x) = alpha * (exp(x / beta) - 1) for x < 0`,
`f(x) = alpha / beta * x for x >= 0`.
# Input shape
Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.
# Output shape
Same shape as the input.
# Arguments
alpha_init: float. Initial value of the alpha weights.
beta_init: float. Initial values of the beta weights.
weights: initial weights, as a list of 2 numpy arrays.
# References
- [Parametric Exponential Linear Unit for Deep Convolutional Neural Networks](https://arxiv.org/abs/1605.09332)
'''
def __init__(self, alpha_init=1.0, beta_init=1.0, weights=None, **kwargs):
self.supports_masking = True
self.alpha_init = K.cast_to_floatx(alpha_init)
self.beta_init = K.cast_to_floatx(beta_init)
self.initial_weights = weights
super(PELU, self).__init__(**kwargs)
def build(self, input_shape):
input_shape = input_shape[1:]
self.alphas = K.variable(self.alpha_init * np.ones(input_shape),
name='{}_alphas'.format(self.name))
self.betas = K.variable(self.beta_init * np.ones(input_shape),
name='{}_betas'.format(self.name))
self.trainable_weights = [self.alphas, self.betas]
if self.initial_weights is not None:
self.set_weights(self.initial_weights)
del self.initial_weights
def call(self, x, mask=None):
pos = K.relu(x) * self.alphas / self.betas
neg = (x - abs(x)) * 0.5
neg = self.alphas * (K.exp(neg / self.betas) - 1)
return pos + neg
def get_config(self):
config = {'alpha_init': self.alpha_init,
'beta_init': self.beta_init}
base_config = super(PELU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))