GPSConv
¶
GPSConv(
channels: int,
conv: Optional[Module],
heads: int = 1,
dropout: float = 0.0,
act: Union[str, Callable, None] = "relu",
act_kwargs: Optional[Dict[str, Any]] = None,
norm: Union[str, Callable[..., Module], None] = "batch_norm",
norm_kwargs: Optional[Dict[str, Any]] = None,
attn_type: str = "multihead",
attn_kwargs: Optional[Dict[str, Any]] = None,
)
Bases: Module
The general, powerful, scalable (GPS) graph transformer layer from the
"Recipe for a General, Powerful, Scalable Graph Transformer" paper, as PyG's GPSConv.
The layer combines a local message passing layer (MPNN) and a global attention layer over all the nodes of each graph, each with a residual connection and a normalization, followed by a two-layer MLP:
Positional and structural encodings (the first part of the GPS recipe)
are added to the input features beforehand, e.g. with the
AddRandomWalkPE or
AddLaplacianEigenvectorPE transforms and
RWSEEncoder / LapPEEncoder.
The global attention is dense: it costs
\(\mathcal{O}(N_{\max}^2)\) memory per graph and head, where
\(N_{\max}\) is the size of the largest graph of the batch. Use
attn_type="performer" for linear attention.
The dense batch of the attention is laid out from batch_indices on
the host (a GPU synchronization, which also prevents mx.compile())
unless a precomputed layout is passed: see __call__() and
from_sizes().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channels
|
int
|
Size of the input and output node features. |
required |
conv
|
Optional[Module]
|
The local message passing layer, called as
|
required |
heads
|
int
|
Number of attention heads. Default: |
1
|
dropout
|
float
|
Dropout probability of the intermediate embeddings.
Default: |
0.0
|
act
|
Union[str, Callable, None]
|
Activation of the MLP: a name ( |
'relu'
|
act_kwargs
|
Optional[Dict[str, Any]]
|
Arguments of the activation. Default: |
None
|
norm
|
Union[str, Callable[..., Module], None]
|
Normalization: |
'batch_norm'
|
norm_kwargs
|
Optional[Dict[str, Any]]
|
Arguments of the normalization. Default: |
None
|
attn_type
|
str
|
Global attention: |
'multihead'
|
attn_kwargs
|
Optional[Dict[str, Any]]
|
Arguments of the attention, e.g. |
None
|
Example:
import mlx.core as mx
import mlx.nn as nn
from mlx_graphs.nn import GINEConv, GPSConv
mlp = nn.Sequential(nn.Linear(64, 64), nn.ReLU(), nn.Linear(64, 64))
conv = GPSConv(64, GINEConv(mlp), heads=4, attn_kwargs={"dropout": 0.5})
edge_index = mx.array([[0, 1, 2, 3], [1, 0, 3, 2]])
node_features = mx.random.normal((4, 64))
edge_features = mx.random.normal((4, 64))
batch_indices = mx.array([0, 0, 1, 1])
>>> conv(edge_index, node_features, batch_indices,
... edge_features=edge_features).shape
(4, 64)
References
Methods:
| Name | Description |
|---|---|
redraw_projection_matrix |
Redraws the random projection of the performer attention (no-op |
__call__ |
Computes the forward pass of GPSConv. |
Source code in mlx_graphs/nn/conv/gps_conv.py
redraw_projection_matrix
¶
Redraws the random projection of the performer attention (no-op for the multi-head attention).
__call__
¶
__call__(
edge_index: array,
node_features: array,
batch_indices: Optional[array] = None,
layout: Optional[DenseLayout] = None,
**kwargs: Any,
) -> array
Computes the forward pass of GPSConv.
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, of shape |
None
|
layout
|
Optional[DenseLayout]
|
Optional precomputed dense layout of the nodes per graph
( |
None
|
**kwargs
|
Any
|
Additional arguments of the local message passing
layer (e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
array
|
The updated node features, of shape |
Source code in mlx_graphs/nn/conv/gps_conv.py
from mlx_graphs.nn import GPSConv · mlx_graphs/nn/conv/gps_conv.py