Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using positional encoding in pytorch

Is there any built-in positional encoding in pytorch? Basically, I want to be able to specify the dimension of the encoding, and then be able to get the i'th encoding for every i.

like image 797
Bipolo Avatar asked Jul 14 '26 22:07

Bipolo


2 Answers

There isn't, as far as I'm aware.

However, you can use an implementation from PyTorch's documentation:

class PositionalEncoding(nn.Module):

    def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000):
        super().__init__()
        self.dropout = nn.Dropout(p=dropout)

        position = torch.arange(max_len).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model))
        pe = torch.zeros(max_len, 1, d_model)
        pe[:, 0, 0::2] = torch.sin(position * div_term)
        pe[:, 0, 1::2] = torch.cos(position * div_term)
        self.register_buffer('pe', pe)

    def forward(self, x: Tensor) -> Tensor:
        """
        Arguments:
            x: Tensor, shape ``[seq_len, batch_size, embedding_dim]``
        """
        x = x + self.pe[:x.size(0)]
        return self.dropout(x)

You can find it here.

like image 178
Yakov Dan Avatar answered Jul 17 '26 22:07

Yakov Dan


There is now an implementation of RoPE at torchtune.modules.RotaryPositionalEmbeddings; torchtune is authored by the official pytorch team.

like image 34
Ilya Kuleshov Avatar answered Jul 18 '26 00:07

Ilya Kuleshov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!