TransformerConv
¶
TransformerConv(
node_features_dim: Union[int, Tuple[int, int]],
out_features_dim: int,
heads: int = 1,
concat: bool = True,
beta: bool = False,
dropout: float = 0.0,
edge_features_dim: Optional[int] = None,
bias: bool = True,
root_weight: bool = True,
**kwargs,
)
Bases: MessagePassing
Graph transformer convolution layer from "Masked Label Prediction: Unified Message Passing Model for Semi-Supervised Classification" paper.
where the attention coefficients \(\alpha_{i,j}\) are computed with multi-head dot-product attention:
With edge features \(\mathbf{e}_{ij}\) (edge_features_dim set), these
are projected and added to both the keys and the values:
With beta=True, the skip connection and the aggregated messages
\(\mathbf{m}_i\) are combined with a learned gate instead of a sum:
The layer also works on bipartite graphs: pass node_features as a
(src_features, dst_features) tuple and, if the two sets have different
feature sizes, node_features_dim as a (src_dim, dst_dim) tuple. The
output has one row per destination node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
Union[int, Tuple[int, int]]
|
Size of input node features, or a |
required |
out_features_dim
|
int
|
Size of output node embeddings (per head) |
required |
heads
|
int
|
Number of attention heads. Default: |
1
|
concat
|
bool
|
Whether to concatenate the heads (output size
|
True
|
beta
|
bool
|
Whether to combine the skip connection and the aggregated messages
with a learned gate. Only used if |
False
|
dropout
|
float
|
Dropout probability applied on the attention coefficients.
Default: |
0.0
|
edge_features_dim
|
Optional[int]
|
Size of edge features. If |
None
|
bias
|
bool
|
Whether to use bias in the key, query, value and skip projections.
Default: |
True
|
root_weight
|
bool
|
Whether to add the transformed features of the node itself
(skip connection) to the output. Default: |
True
|
Example:
import mlx.core as mx
from mlx_graphs.nn.conv.transformer_conv import TransformerConv
conv = TransformerConv(16, 32, heads=2, edge_features_dim=4)
edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
node_features = mx.random.uniform(low=0, high=1, shape=(5, 16))
edge_features = mx.random.uniform(low=0, high=1, shape=(5, 4))
h = conv(edge_index, node_features, edge_features)
>>> h.shape
(5, 64)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the forward pass of TransformerConv. |
message |
Computes one attention-weighted message per edge. |
Source code in mlx_graphs/nn/conv/transformer_conv.py
__call__
¶
__call__(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
edge_features: Optional[array] = None,
) -> array
Computes the forward pass of TransformerConv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Input edge index of shape |
required |
node_features
|
Union[array, Tuple[array, array]]
|
Input node features, or a |
required |
edge_features
|
Optional[array]
|
Edge features of shape |
None
|
Returns:
| Type | Description |
|---|---|
array
|
The computed node embeddings, one row per destination node |
Source code in mlx_graphs/nn/conv/transformer_conv.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
message
¶
message(
src_features: array,
dst_features: array,
edge_weights: array,
edge_features: Optional[array] = None,
) -> array
Computes one attention-weighted message per edge.
Only used on the gather-scatter path (with edge features, or without
the fused kernels); without edge features it is the default
edge_weights * x_j that the fused SpMM computes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_features
|
array
|
Values of the source nodes,
shape |
required |
dst_features
|
array
|
Queries of the destination nodes (unused) |
required |
edge_weights
|
array
|
Attention coefficients, shape |
required |
edge_features
|
Optional[array]
|
Projected edge features,
shape |
None
|
Returns:
| Type | Description |
|---|---|
array
|
The messages, shape |
Source code in mlx_graphs/nn/conv/transformer_conv.py
from mlx_graphs.nn import TransformerConv · mlx_graphs/nn/conv/transformer_conv.py