PositionalEncoding
¶
Bases: Module
The sinusoidal positional encoding from the "Attention Is All You Need" paper, as PyG's
PositionalEncoding.
\[
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})
\]
where \(x\) is the position and \(i\) the dimension. As in PyG, the output is ordered as all the sines followed by all the cosines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_features_dim
|
int
|
Size \(d\) of each output sample (even). |
required |
base_freq
|
float
|
Base frequency of the sinusoids. Default: |
0.0001
|
granularity
|
float
|
Granularity of the positions: smaller values capture
finer changes in positions. Default: |
1.0
|
Example:
import mlx.core as mx
from mlx_graphs.nn import PositionalEncoding
encoder = PositionalEncoding(16)
>>> encoder(mx.array([0.0, 1.0, 2.5])).shape
(3, 16)
Methods:
| Name | Description |
|---|---|
__call__ |
Encodes the positions |
Source code in mlx_graphs/nn/encoding.py
__call__
¶
Encodes the positions x (any shape, flattened) into an array
of shape [x.size, out_features_dim].
Source code in mlx_graphs/nn/encoding.py
from mlx_graphs.nn import PositionalEncoding · mlx_graphs/nn/encoding.py