Positional encoding
embedders.s2vec.positional_encoding ¶
Utility functions for generating sinusoidal positional embeddings in 1D and 2D.
get_1d_sincos_pos_embed ¶
Generate 1D sin-cos positional embeddings.
PARAMETER | DESCRIPTION |
---|---|
embed_dim
|
Dimension of the embeddings (must be even).
TYPE:
|
pos
|
a 1D tensor of positions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: Positional embeddings of shape (len(pos), embed_dim) |
Source code in srai/embedders/s2vec/positional_encoding.py
get_2d_sincos_pos_embed ¶
Generate 2D sin-cos positional embeddings.
PARAMETER | DESCRIPTION |
---|---|
embed_dim
|
Dimension of the embeddings.
TYPE:
|
grid_size
|
The height (and width) of the grid.
TYPE:
|
cls_token
|
Whether to add an extra class token embedding.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: Positional embeddings of shape |
Tensor
|
(1, grid_size*grid_size (+1 if cls_token), embed_dim) |