braindecode.functional.sinusoidal_positional_encoding#

braindecode.functional.sinusoidal_positional_encoding(n_positions, dim)[source]#

Fixed sine/cosine positional-encoding table of shape (n_positions, dim).

The standard encoding of Vaswani et al. (2017): for position \(p\) and channel \(i\), \(pe[p, 2i] = \sin(p / 10000^{2i/d})\) and \(pe[p, 2i+1] = \cos(p / 10000^{2i/d})\). PyTorch ships no sinusoidal encoding (nn.Embedding is a learned lookup), so braindecode models that need one share this primitive instead of re-deriving it. An odd dim is computed on the next even width and truncated, reproducing the per-model wrappers (e.g. medformer odd-d_model).

Parameters:
  • n_positions (int) – Number of positions (sequence length) to encode.

  • dim (int) – Embedding dimension of each position.

Returns:

(n_positions, dim) float table; callers add their own batch axis, dropout, or offset.

Return type:

Tensor