torch_geometric.nn.encoding.PositionalEncoding

class PositionalEncoding(out_channels: int, base_freq: float = 0.0001, granularity: float = 1.0, device: Optional[device] = None)[source]

Bases: Module

The positional encoding scheme from the “Attention Is All You Need” paper.

\[ \begin{align}\begin{aligned}PE(x)_{2 \cdot i} &= \sin(x / 10000^{2 \cdot i / d})\\PE(x)_{2 \cdot i + 1} &= \cos(x / 10000^{2 \cdot i / d})\end{aligned}\end{align} \]

where \(x\) is the position and \(i\) is the dimension.

Parameters:
  • out_channels (int) – Size \(d\) of each output sample.

  • base_freq (float, optional) – The base frequency of sinusoidal functions. (default: 1e-4)

  • granularity (float, optional) – The granularity of the positions. If set to smaller value, the encoder will capture more fine-grained changes in positions. (default: 1.0)

  • device (torch.device, optional) – The device of the module. (default: None)