Skip to content

Instantly share code, notes, and snippets.

@sergeyprokudin
Created May 18, 2024 09:55
Show Gist options
  • Save sergeyprokudin/9db387861ff5452a64f2463f52c331e7 to your computer and use it in GitHub Desktop.
Save sergeyprokudin/9db387861ff5452a64f2463f52c331e7 to your computer and use it in GitHub Desktop.
Simple NeRF positional embedding
# simple positional embedding (NeRF style)
# as in https://arxiv.org/pdf/2003.08934 (Equation 4)
def positional_embedding(x, k):
batch_size, n_dims = x.shape
x_rep = x.unsqueeze(-1).expand(-1, -1, k)
k_pows = torch.pow(2, torch.arange(0, k, device=x.device)).view(1, 1, k)
x_k = k_pows * torch.pi * x_rep
x_pos = torch.cat([torch.sin(x_k), torch.cos(x_k)], dim=-1)
return x_pos.view(batch_size, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment