| |
| |
|
|
| import torch |
| from torch import nn |
| from torch.nn import Parameter |
|
|
|
|
| @torch.jit.script |
| def snakebeta(x, alpha, beta): |
| shape = x.shape |
| x = x.reshape(shape[0], shape[1], -1) |
| x = x + (beta + 1e-9).reciprocal() * torch.sin(alpha * x).pow(2) |
| x = x.reshape(shape) |
| return x |
|
|
|
|
| class SnakeBeta(nn.Module): |
| def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False): |
| """ |
| Initialization. |
| INPUT: |
| - in_features: shape of the input |
| - alpha - trainable parameter that controls frequency |
| - beta - trainable parameter that controls magnitude |
| alpha is initialized to 1 by default, higher values = higher-frequency. |
| beta is initialized to 1 by default, higher values = higher-magnitude. |
| alpha will be trained along with the rest of your model. |
| """ |
| super(SnakeBeta, self).__init__() |
| self.in_features = in_features |
|
|
| |
| self.alpha_logscale = alpha_logscale |
| if self.alpha_logscale: |
| self.alpha = Parameter(torch.zeros(in_features) * alpha) |
| self.beta = Parameter(torch.zeros(in_features) * alpha) |
| else: |
| self.alpha = Parameter(torch.ones(in_features) * alpha) |
| self.beta = Parameter(torch.ones(in_features) * alpha) |
|
|
| self.alpha.requires_grad = alpha_trainable |
| self.beta.requires_grad = alpha_trainable |
|
|
| self.no_div_by_zero = 0.000000001 |
|
|
| def forward(self, x): |
| """ |
| Forward pass of the function. |
| Applies the function to the input elementwise. |
| SnakeBeta := x + 1/b * sin^2 (xa) |
| """ |
| alpha = self.alpha.unsqueeze(0).unsqueeze(-1) |
| beta = self.beta.unsqueeze(0).unsqueeze(-1) |
| if self.alpha_logscale: |
| alpha = torch.exp(alpha) |
| beta = torch.exp(beta) |
| x = snakebeta(x, alpha, beta) |
|
|
| return x |
|
|