-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathactivations.py
493 lines (320 loc) · 13.9 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
from neunet.autograd import Tensor
from neunet.nn.modules import Module
# import numpy as np
class _SigmoidTensor(Tensor): # Static sigmoid tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(x: Tensor, f_x, grad):
x.apply_grad(grad * f_x * (1 - f_x))
self.grad_fn = grad_fn
class Sigmoid(Module): # Static sigmoid computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = 1 / (1 + x.xp.exp(-x.data))
return _SigmoidTensor(f_x, [x, f_x], "sigmoid", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Sigmoid(Tensor): #Dynamic sigmoid computation (slower than static)
# def __init__(self):
# pass
# def forward(self, x):
# return x.exp().div(x.exp().add(1))
# def __call__(self, x):
# return self.forward(x)
class _ReLUTensor(Tensor): # Static ReLU tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, grad):
t.apply_grad(grad * (f_x > 0))
self.grad_fn = grad_fn
class ReLU(Module): # Static ReLU computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = x.xp.maximum(0, x.data)
return _ReLUTensor(f_x, [x, f_x], "relu", device=x.device)
def __call__(self, x):
return self.forward(x)
# class ReLU(Tensor): #Dynamic ReLU computation (slower than static)
# def __init__(self):
# pass
# def forward(self, x):
# return x.maximum(0)
# def __call__(self, x):
# return self.forward(x)
class _LeakyReLUTensor(Tensor): # Static LeakyReLU tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, alpha, grad):
t.apply_grad(
grad * t.xp.where(f_x <= 0, alpha, 1).astype(grad.dtype)
)
self.grad_fn = grad_fn
class LeakyReLU(Module): # Static LeakyReLU computation
def __init__(self, alpha=0.01):
self.alpha = alpha
def forward(self, x: Tensor):
f_x = x.xp.where(x.data <= 0, self.alpha * x.data, x.data).astype(x.dtype)
return _LeakyReLUTensor(f_x, [x, f_x, self.alpha], "leakyrelu", device=x.device)
def __call__(self, x):
return self.forward(x)
# class LeakyReLU(Tensor): #Dynamic LeakyReLU computation (slower than static)
# def __init__(self, alpha = 0.01):
# self.alpha = alpha
# def forward(self, x):
# return x.maximum(0).add(x.minimum(0).mul(self.alpha))
# def __call__(self, x):
# return self.forward(x)
class _TanhTensor(Tensor): # Static Tanh tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, grad):
t.apply_grad(grad * (1 - f_x ** 2))
self.grad_fn = grad_fn
class Tanh(Module): # Static Tanh computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = x.xp.tanh(x.data)
return _TanhTensor(f_x, [x, f_x], "tanh", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Tanh(Tensor): #Dynamic Tanh computation (slower than static)
# def __init__(self):
# pass
# def forward(self, x):
# return x.exp().sub(x.mul(-1).exp()).div(x.exp().add(x.mul(-1).exp()))
# def __call__(self, x):
# return self.forward(x)
class _SoftplusTensor(Tensor): # Static Softplus tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, grad):
x = t.data
t.apply_grad(grad * (1 / (1 + t.xp.exp(-x))))
self.grad_fn = grad_fn
class Softplus(Module): # Static Softplus computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = x.xp.log(1 + x.xp.exp(x.data))
return _SoftplusTensor(f_x, [x], "softplus", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Softplus(Tensor):
# def __init__(self):
# pass
# def forward(self, x):
# return x.exp().add(1).log()
# def __call__(self, x):
# return self.forward(x)
class _SoftsignTensor(Tensor): # Static Softsign tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, grad):
x = t.data
t.apply_grad(grad * (1 / (1 + t.xp.abs(x)) ** 2))
self.grad_fn = grad_fn
class Softsign(Module): # Static Softsign computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = x.data / (1 + x.xp.abs(x.data))
return _SoftsignTensor(f_x, [x], "softsign", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Softsign(Tensor):
# def __init__(self):
# pass
# def forward(self, x):
# return x.div(x.abs().add(1))
# def __call__(self, x):
# return self.forward(x)
class _SwishTensorTensor(Tensor): # Static Swish tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, beta, grad):
x = t.data
sigmoid = lambda x: 1 / (1 + t.xp.exp(-x))
t.apply_grad(grad * (beta * f_x + sigmoid(beta * x) * (1 - beta * f_x)))
self.grad_fn = grad_fn
class Swish(Module): # Static Swish computation
def __init__(self, beta=1):
self.beta = beta
def forward(self, x: Tensor):
xp = x.xp
sigmoid = lambda x: 1 / (1 + xp.exp(-x))
f_x = x.data * sigmoid(self.beta * x.data)
return _SwishTensorTensor(f_x, [x, f_x, self.beta], "swish", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Swish(Tensor): #Dynamic Swish computation (slower than static)
# def __init__(self, beta = 1):
# self.beta = beta
# def forward(self, x):
# z = x.mul(self.beta)
# sigmoid = z.exp().div(z.exp().add(1))
# return x.mul(sigmoid)
# def __call__(self, x):
# return self.forward(x)
class _MishTensor(Tensor): # Static Mish tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, grad):
xp = t.xp
x = t.data
grad_x = grad * (
xp.exp(x)
* (4 * (x + 1) + 4 * xp.exp(2 * x) + xp.exp(3 * x) + xp.exp(x) * (4 * x + 6))
/ xp.power((2 * xp.exp(x) + xp.exp(2 * x) + 2), 2)
)
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class Mish(Module): # Static Mish computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = x.data * x.xp.tanh(x.xp.log(1 + x.xp.exp(x.data)))
return _MishTensor(f_x, [x], "mish", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Mish(Tensor): #Dynamic Mish computation (slower than static)
# def __init__(self):
# pass
# def forward(self, x):
# return x.mul(x.tanh().mul(x.exp().add(1)).log())
# def __call__(self, x):
# return self.forward(x)
class _TanhExpTensor(Tensor): # Static TanhExp tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, grad):
xp = t.xp
x = t.data
grad_x = grad * (xp.tanh(xp.exp(x)) - x * xp.exp(x) * (xp.power(xp.tanh(xp.exp(x)), 2) - 1))
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class TanhExp(Module): # Static TanhExp computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = x.data * x.xp.tanh(x.xp.exp(x.data))
return _TanhExpTensor(f_x, [x], "tanh_exp", device=x.device)
def __call__(self, x):
return self.forward(x)
# class TanhExp(Tensor): #Dynamic TanhExp computation (slower than static)
# def __init__(self):
# pass
# def forward(self, x):
# return x.mul(x.exp().tanh())
# def __call__(self, x):
# return self.forward(x)
class _ELUTensor(Tensor): # Static ELU tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, alpha, grad):
x = t.data
grad_x = grad * (t.xp.where(x <= 0, alpha + f_x, 1).astype(grad.dtype))
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class ELU(Module): # Static ELU computation
def __init__(self, alpha=0.1):
self.alpha = alpha
def forward(self, x: Tensor):
f_x = x.xp.where(x.data <= 0, self.alpha * (x.xp.exp(x.data) - 1), x.data).astype(x.dtype)
return _ELUTensor(f_x, [x, f_x, self.alpha], "elu", device=x.device)
def __call__(self, x):
return self.forward(x)
class _SELUTensor(Tensor): # Static SELU tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, alpha, lmbda, grad):
x = t.data
grad_x = grad * (lmbda * t.xp.where(x > 0, 1, alpha * t.xp.exp(x)).astype(grad.dtype))
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class SELU(Module): # Static SELU computation
def __init__(self):
self.alpha = 1.6732632423543772848170429916717
self.lmbda = 1.0507009873554804934193349852946
def forward(self, x: Tensor):
f_x = self.lmbda * x.xp.where(
x.data > 0, x.data, self.alpha * (x.xp.exp(x.data) - 1).astype(x.dtype)
)
return _SELUTensor(f_x, [x, self.alpha, self.lmbda], "selu", device=x.device)
def __call__(self, x):
return self.forward(x)
class _GELUTensor(Tensor): # Static GELU tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, grad):
xp = t.xp
x = t.data
# sech = lambda z: 2 / (np.exp(z) + np.exp(-z))
sech = lambda z: 1 / xp.cosh(z)
grad_x = grad * (
0.5 * xp.tanh(0.0356774 * xp.power(x, 3) + 0.797885 * x)
+ (0.0535161 * xp.power(x, 3) + 0.398942 * x)
* xp.power(sech(0.0356774 * xp.power(x, 3) + 0.797885 * x), 2)
+ 0.5
)
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class GELU(Module): # Static GELU computation
def __init__(self):
pass
def forward(self, x: Tensor):
f_x = (
0.5
* x.data
* (1 + x.xp.tanh(x.xp.sqrt(2 / x.xp.pi) * (x.data + 0.044715 * x.xp.power(x.data, 3))))
)
return _GELUTensor(f_x, [x], "gelu", device=x.device)
def __call__(self, x):
return self.forward(x)
# class Softmax(Module): # Dynamic Softmax computation
# def __init__(self, axis=1):
# self.axis = axis
# def forward(self, x: Tensor):
# e_x = x.sub(x.max(axis=self.axis, keepdims=True)).exp()
# return e_x.div(e_x.sum(axis=self.axis, keepdims=True))
# def __call__(self, x):
# return self.forward(x)
class _SoftmaxTensor(Tensor): # Static Softmax tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, axis, grad):
grad_x=(grad - (grad * f_x).sum(axis, keepdims=True)) * f_x
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class Softmax(Module): # Static Softmax computation
def __init__(self, axis=1):
self.axis = axis
def forward(self, x: Tensor):
e_x = x.xp.exp(x.data - x.xp.max(x.data, axis = self.axis, keepdims=True))
f_x = e_x / x.xp.sum(e_x, axis = self.axis, keepdims=True)
return _SoftmaxTensor(f_x, [x, f_x, self.axis], "softmax", device=x.device)
def __call__(self, x):
return self.forward(x)
class _LogSoftmax(Tensor): # Static LogSoftmax tensor for backpropagation
def __init__(self, data, args, op, device):
super().__init__(data, args, op, device=device)
def grad_fn(t: Tensor, f_x, axis, grad):
softmax = t.xp.exp(f_x) # e^(loge_softmax) = softmax
grad_x = grad - softmax * grad.sum(axis = axis, keepdims=True)
t.apply_grad(grad_x)
self.grad_fn = grad_fn
class LogSoftmax(Module):
def __init__(self, axis=1):
self.axis = axis
def forward(self, x: Tensor):
# e_x = x.xp.exp(x.data - x.xp.max(x.data, axis = self.axis, keepdims=True))
# f_x = x.xp.log(e_x / x.xp.sum(e_x, axis = self.axis, keepdims=True))
# Numerically stable computation
max_x = x.xp.max(x.data, axis = self.axis, keepdims=True)
e_x = x.xp.exp(x.data - max_x)
f_x = x.data - max_x - x.xp.log(x.xp.sum(e_x, axis = self.axis, keepdims=True))
return _LogSoftmax(f_x, [x, f_x, self.axis], "log_softmax", device=x.device)
def __call__(self, x):
return self.forward(x)