GeneralConv
¶
GeneralConv(
node_features_dim: Union[int, Tuple[int, int]],
out_features_dim: int,
edge_features_dim: Optional[int] = None,
aggr: Literal["add", "mean", "max", "min"] = "add",
skip_linear: bool = False,
directed_msg: bool = True,
heads: int = 1,
attention: bool = False,
attention_type: Literal["additive", "dot_product"] = "additive",
l2_normalize: bool = False,
bias: bool = True,
)
Bases: MessagePassing
A general GNN layer adapted from the "Design Space for Graph Neural Networks" paper.
Messages are linear projections of the source features (plus, optionally,
of the destination features and of the edge features), computed for
heads independent ensembles that are averaged:
where \(\alpha^{h}_{i,j} = 1\) without attention, and otherwise a
softmax over the neighbors of \(i\) of either an additive score
(attention_type="additive") or a scaled dot product between the
messages in both directions (attention_type="dot_product"), followed by
a LeakyReLU with slope 0.2. \(\mathbf{W}_{\textrm{self}}\) is the
identity if skip_linear=False and the input and output sizes match.
The layer also works on bipartite graphs: pass node_features as a
(src_features, dst_features) tuple and 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 |
required |
edge_features_dim
|
Optional[int]
|
Size of edge features. Default: |
None
|
aggr
|
Literal['add', 'mean', 'max', 'min']
|
Aggregation of the messages ( |
'add'
|
skip_linear
|
bool
|
Whether to apply a linear layer in the skip connection
even if the input and output sizes match. Default: |
False
|
directed_msg
|
bool
|
If |
True
|
heads
|
int
|
Number of message passing ensembles (attention heads if
|
1
|
attention
|
bool
|
Whether to weigh messages with attention. Default: |
False
|
attention_type
|
Literal['additive', 'dot_product']
|
|
'additive'
|
l2_normalize
|
bool
|
Whether to \(\ell_2\)-normalize the output features.
Default: |
False
|
bias
|
bool
|
Whether the linear layers use a bias. Default: |
True
|
Example:
import mlx.core as mx
from mlx_graphs.nn import GeneralConv
conv = GeneralConv(16, 32, heads=2, attention=True)
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))
h = conv(edge_index, node_features)
>>> h.shape
(5, 32)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the forward pass of GeneralConv. |
Source code in mlx_graphs/nn/conv/general_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 GeneralConv.
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]
|
Optional 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/general_conv.py
from mlx_graphs.nn import GeneralConv · mlx_graphs/nn/conv/general_conv.py