MultiheadAttentionBlock
¶
MultiheadAttentionBlock(
channels: int, heads: int = 1, layer_norm: bool = True, dropout: float = 0.0
)
Bases: Module
The Multihead Attention Block (MAB) from the "Set Transformer: A Framework for Attention-based Permutation-Invariant Neural Networks" paper, as in PyG.
\[
\mathrm{MAB}(\mathbf{x}, \mathbf{y}) &= \mathrm{LayerNorm}(\mathbf{h} +
\mathrm{ReLU}(\mathbf{h}\mathbf{W})) \\
\mathbf{h} &= \mathrm{LayerNorm}(\mathbf{x} +
\mathrm{Multihead}(\mathbf{x}, \mathbf{y}, \mathbf{y}))
\]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Size of each input sample. |
required |
heads
|
int
|
Number of attention heads. Default: |
1
|
layer_norm
|
bool
|
Whether to apply layer normalization. Default: |
True
|
dropout
|
float
|
Dropout probability of the attention weights. Default: |
0.0
|
Methods:
| Name | Description |
|---|---|
__call__ |
Attends from |
Source code in mlx_graphs/nn/aggr/utils.py
__call__
¶
__call__(
x: array,
y: array,
x_mask: Optional[array] = None,
y_mask: Optional[array] = None,
) -> array
Attends from x ([B, N, channels]) to y ([B, M,
channels]); x_mask and y_mask are True for real rows.
Source code in mlx_graphs/nn/aggr/utils.py
from mlx_graphs.nn.aggr import MultiheadAttentionBlock · mlx_graphs/nn/aggr/utils.py