Create sinusoidal timestep embeddings. :param timesteps: a 1-D Tensor of N indices, one per batch element. These may be fractional. :param dim: the dimension of the output. :param max_period: controls the minimum frequency of the embeddings. :return: an [N
(timesteps, dim, max_period=10000)
| 13 | |
| 14 | |
| 15 | def timestep_embedding(timesteps, dim, max_period=10000): |
| 16 | """ |
| 17 | Create sinusoidal timestep embeddings. |
| 18 | :param timesteps: a 1-D Tensor of N indices, one per batch element. |
| 19 | These may be fractional. |
| 20 | :param dim: the dimension of the output. |
| 21 | :param max_period: controls the minimum frequency of the embeddings. |
| 22 | :return: an [N x dim] Tensor of positional embeddings. |
| 23 | """ |
| 24 | half = dim // 2 |
| 25 | freqs = torch.exp( |
| 26 | -math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half |
| 27 | ).to(device=timesteps.device) |
| 28 | args = timesteps[:, None].float() * freqs[None] |
| 29 | embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1) |
| 30 | if dim % 2: |
| 31 | embedding = torch.cat([embedding, torch.zeros_like(embedding[:, :1])], dim=-1) |
| 32 | return embedding |
| 33 | |
| 34 | |
| 35 | def set_requires_grad(nets, requires_grad=False): |