Polynormer
¶
Polynormer(
node_features_dim: int,
hidden_features_dim: int,
out_features_dim: int,
local_layers: int = 7,
global_layers: int = 2,
in_dropout: float = 0.15,
dropout: float = 0.5,
global_dropout: float = 0.5,
heads: int = 1,
beta: float = 0.9,
qk_shared: bool = False,
pre_ln: bool = False,
post_bn: bool = True,
local_attn: bool = False,
)
Bases: Module
The Polynormer model from the "Polynormer: Polynomial-Expressive Graph Transformer in Linear Time"
paper, as PyG's Polynormer.
A stack of local (GCN or GAT based) polynomial layers is followed, once
_global is set to True (usually after some warm-up epochs, as in
the paper), by global linear attention layers
(PolynormerAttention). The output is a
per-node log-softmax.
Note
Unlike PyG 2.8, the global attention layers take
heads * hidden_features_dim input features, so that
heads > 1 also works with _global = True (identical for
heads = 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
int
|
Size of the input node features. |
required |
hidden_features_dim
|
int
|
Size of the hidden embeddings of each head. |
required |
out_features_dim
|
int
|
Number of classes. |
required |
local_layers
|
int
|
Number of local layers. Default: |
7
|
global_layers
|
int
|
Number of global attention layers. Default: |
2
|
in_dropout
|
float
|
Dropout probability of the input. Default: |
0.15
|
dropout
|
float
|
Dropout probability of the local layers. Default: |
0.5
|
global_dropout
|
float
|
Dropout probability of the global layers.
Default: |
0.5
|
heads
|
int
|
Number of heads. Default: |
1
|
beta
|
float
|
Polynormer \(\beta\). Default: |
0.9
|
qk_shared
|
bool
|
Whether queries and keys share their weights in the
global attention. Default: |
False
|
pre_ln
|
bool
|
Whether to apply a layer norm before each local layer.
Default: |
False
|
post_bn
|
bool
|
Whether to apply a batch norm after each local layer.
Default: |
True
|
local_attn
|
bool
|
False
|
Example:
import mlx.core as mx
from mlx_graphs.nn import Polynormer
model = Polynormer(16, 32, 4, local_layers=2, global_layers=1)
model._global = True # enable the global attention layers
edge_index = mx.array([[0, 1, 1, 2], [1, 0, 2, 1]])
node_features = mx.random.normal((3, 16))
>>> model(edge_index, node_features).shape
(3, 4)
References
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the class log-probabilities of the nodes. |
Source code in mlx_graphs/nn/models/graph_transformers.py
__call__
¶
__call__(
edge_index: array,
node_features: array,
batch_indices: Optional[array] = None,
layout: Optional[DenseLayout] = None,
) -> array
Computes the class log-probabilities of the nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Edge index of shape |
required |
node_features
|
array
|
Node features of shape
|
required |
batch_indices
|
Optional[array]
|
Graph of each node; the global attention is
computed within each graph. Default: |
None
|
layout
|
Optional[DenseLayout]
|
Optional precomputed dense layout of the nodes per graph
( |
None
|
Returns:
| Type | Description |
|---|---|
array
|
Log-probabilities of shape |
Source code in mlx_graphs/nn/models/graph_transformers.py
from mlx_graphs.nn import Polynormer · mlx_graphs/nn/models/graph_transformers.py