forked from Scintillare/AIGan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivations.py
66 lines (52 loc) · 1.97 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
import torch.nn as nn
import torch.nn.functional as F
import torch
class Rational(torch.nn.Module):
"""Rational Activation function.
It follows:
`f(x) = P(x) / Q(x),
where the coefficients of P and Q are initialized to the best rational
approximation of degree (3,2) to the ReLU function
# Reference
- [Rational neural networks](https://arxiv.org/abs/2004.01902)
"""
def __init__(self):
super().__init__()
self.coeffs = torch.nn.Parameter(torch.Tensor(4, 2))
self.reset_parameters()
def reset_parameters(self):
self.coeffs.data = torch.Tensor([[1.1915, 0.0],
[1.5957, 2.383],
[0.5, 0.0],
[0.0218, 1.0]])
def forward(self, input: torch.Tensor) -> torch.Tensor:
self.coeffs.data[0,1].zero_()
exp = torch.tensor([3., 2., 1., 0.], device=input.device, dtype=input.dtype)
X = torch.pow(input.unsqueeze(-1), exp)
PQ = X @ self.coeffs
output = torch.div(PQ[..., 0], PQ[..., 1])
return output
class Swish_fun(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i * i.sigmoid()
ctx.save_for_backward(result, i)
return result
@staticmethod
def backward(ctx, grad_output):
result, i = ctx.saved_variables
sigmoid_x = i.sigmoid()
return grad_output * (result + sigmoid_x * (1 - result))
class Swish(nn.Module):
swish = Swish_fun.apply
def forward(self, x):
return Swish.swish(x)
class AReLU(nn.Module):
def __init__(self, alpha=0.90, beta=2.0):
super().__init__()
self.alpha = nn.Parameter(torch.tensor([alpha]))
self.beta = nn.Parameter(torch.tensor([beta]))
def forward(self, input):
alpha = torch.clamp(self.alpha, min=0.01, max=0.99)
beta = 1 + torch.sigmoid(self.beta)
return F.relu(input) * beta - F.relu(-input) * alpha