Gaussian Fourier embeddings for noise levels.
| 1326 | |
| 1327 | |
| 1328 | class GaussianFourierProjection(nn.Module): |
| 1329 | """Gaussian Fourier embeddings for noise levels.""" |
| 1330 | |
| 1331 | def __init__( |
| 1332 | self, embedding_size: int = 256, scale: float = 1.0, set_W_to_weight=True, log=True, flip_sin_to_cos=False |
| 1333 | ): |
| 1334 | super().__init__() |
| 1335 | self.weight = nn.Parameter(torch.randn(embedding_size) * scale, requires_grad=False) |
| 1336 | self.log = log |
| 1337 | self.flip_sin_to_cos = flip_sin_to_cos |
| 1338 | |
| 1339 | if set_W_to_weight: |
| 1340 | # to delete later |
| 1341 | del self.weight |
| 1342 | self.W = nn.Parameter(torch.randn(embedding_size) * scale, requires_grad=False) |
| 1343 | self.weight = self.W |
| 1344 | del self.W |
| 1345 | |
| 1346 | def forward(self, x): |
| 1347 | if self.log: |
| 1348 | x = torch.log(x) |
| 1349 | |
| 1350 | x_proj = x[:, None] * self.weight[None, :] * 2 * np.pi |
| 1351 | |
| 1352 | if self.flip_sin_to_cos: |
| 1353 | out = torch.cat([torch.cos(x_proj), torch.sin(x_proj)], dim=-1) |
| 1354 | else: |
| 1355 | out = torch.cat([torch.sin(x_proj), torch.cos(x_proj)], dim=-1) |
| 1356 | return out |
| 1357 | |
| 1358 | |
| 1359 | class SinusoidalPositionalEmbedding(nn.Module): |
no outgoing calls
no test coverage detected
searching dependent graphs…