PRIYANSHUDHAKED commited on
Commit
36c1db5
·
verified ·
1 Parent(s): 71c3c40

Delete src/miocodec/pupu/modules/activation_functions/snake.py with huggingface_hub

Browse files
src/miocodec/pupu/modules/activation_functions/snake.py DELETED
@@ -1,155 +0,0 @@
1
- import torch
2
- from torch import nn, pow, sin
3
- from torch.nn import Parameter
4
-
5
-
6
- class Snake(nn.Module):
7
- r"""Implementation of a sine-based periodic activation function.
8
- Alpha is initialized to 1 by default, higher values means higher frequency.
9
- It will be trained along with the rest of your model.
10
-
11
- Args:
12
- in_features: shape of the input
13
- alpha: trainable parameter
14
-
15
- Shape:
16
- - Input: (B, C, T)
17
- - Output: (B, C, T), same shape as the input
18
-
19
- References:
20
- This activation function is from this paper by Liu Ziyin, Tilman Hartwig,
21
- Masahito Ueda: https://arxiv.org/abs/2006.08195
22
-
23
- Examples:
24
- >>> a1 = Snake(256)
25
- >>> x = torch.randn(256)
26
- >>> x = a1(x)
27
- """
28
-
29
- def __init__(
30
- self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False
31
- ):
32
- super(Snake, self).__init__()
33
- self.in_features = in_features
34
-
35
- # initialize alpha
36
- self.alpha_logscale = alpha_logscale
37
- if self.alpha_logscale: # log scale alphas initialized to zeros
38
- self.alpha = Parameter(torch.zeros(in_features) * alpha)
39
- else: # linear scale alphas initialized to ones
40
- self.alpha = Parameter(torch.ones(in_features) * alpha)
41
-
42
- self.alpha.requires_grad = alpha_trainable
43
-
44
- self.no_div_by_zero = 0.000000001
45
-
46
- def forward(self, x):
47
- r"""Forward pass of the function. Applies the function to the input elementwise.
48
- Snake ∶= x + 1/a * sin^2 (ax)
49
- """
50
-
51
- alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
52
- if self.alpha_logscale:
53
- alpha = torch.exp(alpha)
54
- x = x + (1.0 / (alpha + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
55
-
56
- return x
57
-
58
-
59
- class SnakeBeta(nn.Module):
60
- r"""A modified Snake function which uses separate parameters for the magnitude
61
- of the periodic components. Alpha is initialized to 1 by default,
62
- higher values means higher frequency. Beta is initialized to 1 by default,
63
- higher values means higher magnitude. Both will be trained along with the
64
- rest of your model.
65
-
66
- Args:
67
- in_features: shape of the input
68
- alpha: trainable parameter that controls frequency
69
- beta: trainable parameter that controls magnitude
70
-
71
- Shape:
72
- - Input: (B, C, T)
73
- - Output: (B, C, T), same shape as the input
74
-
75
- References:
76
- This activation function is a modified version based on this paper by Liu Ziyin,
77
- Tilman Hartwig, Masahito Ueda: https://arxiv.org/abs/2006.08195
78
-
79
- Examples:
80
- >>> a1 = SnakeBeta(256)
81
- >>> x = torch.randn(256)
82
- >>> x = a1(x)
83
- """
84
-
85
- def __init__(
86
- self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False
87
- ):
88
- super(SnakeBeta, self).__init__()
89
- self.in_features = in_features
90
-
91
- # initialize alpha
92
- self.alpha_logscale = alpha_logscale
93
- if self.alpha_logscale: # log scale alphas initialized to zeros
94
- self.alpha = Parameter(torch.zeros(in_features) * alpha)
95
- self.beta = Parameter(torch.zeros(in_features) * alpha)
96
- else: # linear scale alphas initialized to ones
97
- self.alpha = Parameter(torch.ones(in_features) * alpha)
98
- self.beta = Parameter(torch.ones(in_features) * alpha)
99
-
100
- self.alpha.requires_grad = alpha_trainable
101
- self.beta.requires_grad = alpha_trainable
102
-
103
- self.no_div_by_zero = 0.000000001
104
-
105
- def forward(self, x):
106
- r"""Forward pass of the function. Applies the function to the input elementwise.
107
- SnakeBeta ∶= x + 1/b * sin^2 (xa)
108
- """
109
-
110
- alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
111
- beta = self.beta.unsqueeze(0).unsqueeze(-1)
112
- if self.alpha_logscale:
113
- alpha = torch.exp(alpha)
114
- beta = torch.exp(beta)
115
- x = x + (1.0 / (beta + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
116
-
117
- return x
118
-
119
-
120
- class ADAASnakeBeta(nn.Module):
121
- def __init__(
122
- self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=True
123
- ):
124
- super(ADAASnakeBeta, self).__init__()
125
- self.in_features = in_features
126
-
127
- self.alpha_logscale = alpha_logscale
128
- if self.alpha_logscale:
129
- self.alpha = nn.Parameter(torch.zeros(in_features) * alpha)
130
- self.beta = nn.Parameter(torch.zeros(in_features) * alpha)
131
- else:
132
- self.alpha = nn.Parameter(torch.ones(in_features) * alpha)
133
- self.beta = nn.Parameter(torch.ones(in_features) * alpha)
134
-
135
- self.alpha.requires_grad = alpha_trainable
136
- self.beta.requires_grad = alpha_trainable
137
-
138
- self.no_div_by_zero = 0.000000001
139
-
140
- def forward(self, x):
141
- alpha = self.alpha.unsqueeze(0).unsqueeze(-1)
142
- beta = self.beta.unsqueeze(0).unsqueeze(-1)
143
- if self.alpha_logscale:
144
- alpha = torch.exp(alpha)
145
- beta = torch.exp(beta)
146
- y = torch.zeros_like(x, device=x.device)
147
- x_shifted = torch.cat(
148
- [torch.zeros_like(x[..., :1], device=x.device), x[..., :-1]], dim=-1
149
- )
150
- x_delta = x - x_shifted
151
- x_sum = x + x_shifted
152
- y = x_sum / 2.0 + (1.0 / (2.0 * (beta + self.no_div_by_zero))) * (
153
- 1.0 - torch.cos(alpha * x_sum) * torch.sinc(alpha * x_delta / torch.pi)
154
- )
155
- return y