SGFormer
¶
SGFormer(
node_features_dim: int,
hidden_features_dim: int,
out_features_dim: int,
trans_num_layers: int = 2,
trans_num_heads: int = 1,
trans_dropout: float = 0.5,
gnn_num_layers: int = 3,
gnn_dropout: float = 0.5,
graph_weight: float = 0.5,
aggregate: str = "add",
)
Bases: Module
The SGFormer model from the "SGFormer: Simplifying and Empowering Transformers for Large-Graph Representations" paper, as PyG's SGFormer.
A single-layer-style linear global attention branch
(SGFormerAttention) and a GCN branch
are combined, then a linear layer and a log-softmax give per-node class
log-probabilities.
Note
As in PyG, the global attention runs on the zero-padded dense batch of each graph, so the padding nodes take part in the attention normalizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
int
|
Size of the input node features. |
required |
hidden_features_dim
|
int
|
Size of the hidden node embeddings. |
required |
out_features_dim
|
int
|
Number of classes. |
required |
trans_num_layers
|
int
|
Number of global attention layers. Default: |
2
|
trans_num_heads
|
int
|
Number of attention heads. Default: |
1
|
trans_dropout
|
float
|
Dropout probability of the attention branch.
Default: |
0.5
|
gnn_num_layers
|
int
|
Number of GCN layers. Default: |
3
|
gnn_dropout
|
float
|
Dropout probability of the GCN branch. Default: |
0.5
|
graph_weight
|
float
|
Weight of the GCN branch when |
0.5
|
aggregate
|
str
|
How the branches are combined: |
'add'
|
Example:
import mlx.core as mx
from mlx_graphs.nn import SGFormer
model = SGFormer(16, 32, 4)
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)
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; 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 SGFormer · mlx_graphs/nn/models/graph_transformers.py