Buckets:
| # SPDX-License-Identifier: MIT | |
| # Implementation adapted from https://github.com/EdwardDixon/snake under the MIT license. | |
| import torch | |
| from torch import nn | |
| from torch.nn import Parameter | |
| 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 | |
| # Initialize alpha | |
| self.alpha_logscale = alpha_logscale | |
| if self.alpha_logscale: # Log scale alphas initialized to zeros | |
| self.alpha = Parameter(torch.zeros(in_features) * alpha) | |
| self.beta = Parameter(torch.zeros(in_features) * alpha) | |
| else: # Linear scale alphas initialized to ones | |
| 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) # Line up with x to [B, C, T] | |
| 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 | |
Xet Storage Details
- Size:
- 2.19 kB
- Xet hash:
- a4be13b4602873c1fbdd6f459c1d6c2afdd3d9a08ecaab7de8c9c34e8b8ff9cc
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.