Skip to content

ResGatedGraphConv

ResGatedGraphConv(
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    act: Optional[Callable[[array], array]] = sigmoid,
    edge_features_dim: Optional[int] = None,
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
)

Bases: MessagePassing

Residual gated graph convolution layer from the "Residual Gated Graph ConvNets" paper.

\[ \mathbf{h}_i = \mathbf{W}_1 \mathbf{x}_i + \sum_{j \in \mathcal{N}(i)} \eta_{i,j} \odot \mathbf{W}_2 \mathbf{x}_j + \mathbf{b} \]

where the gate \(\eta_{i,j}\) is defined as

\[ \eta_{i,j} = \sigma(\mathbf{W}_3 \mathbf{x}_i + \mathbf{W}_4 \mathbf{x}_j) \]

with \(\sigma\) the sigmoid function. With edge features (edge_features_dim set), the features of the edge from \(j\) to \(i\) are concatenated to \(\mathbf{x}_i\) and \(\mathbf{x}_j\) before the projections \(\mathbf{W}_2\), \(\mathbf{W}_3\) and \(\mathbf{W}_4\).

Parameters:

Name Type Description Default
node_features_dim Union[int, Tuple[int, int]]

Size of input node features, or a (src_dim, dst_dim) tuple for bipartite graphs

required
out_features_dim int

Size of output node embeddings

required
act Optional[Callable[[array], array]]

Gating function \(\sigma\). Default: mlx.core.sigmoid()

sigmoid
edge_features_dim Optional[int]

Size of the edge features, if any. Default: None

None
root_weight bool

Whether to add the transformed root node features \(\mathbf{W}_1 \mathbf{x}_i\). Default: True

True
bias bool

Whether to add a bias to the output. Default: True

True
**kwargs

Arguments of MessagePassing, e.g. aggr (default: "add")

{}

Example:

import mlx.core as mx
from mlx_graphs.nn import ResGatedGraphConv

edge_index = mx.array([[0, 1, 2, 3, 4], [1, 0, 3, 2, 0]])
node_features = mx.random.normal((5, 16))

conv = ResGatedGraphConv(16, 32)
h = conv(edge_index, node_features)
>>> h.shape
(5, 32)

conv = ResGatedGraphConv(16, 32, edge_features_dim=4)
h = conv(edge_index, node_features, mx.random.normal((5, 4)))

Methods:

Name Description
__call__

Computes the forward pass of ResGatedGraphConv.

Source code in mlx_graphs/nn/conv/res_gated_graph_conv.py
def __init__(
    self,
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    act: Optional[Callable[[mx.array], mx.array]] = mx.sigmoid,
    edge_features_dim: Optional[int] = None,
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
):
    kwargs.setdefault("aggr", "add")
    super(ResGatedGraphConv, self).__init__(**kwargs)

    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.act = act
    self.edge_features_dim = edge_features_dim
    self.root_weight = root_weight

    if isinstance(node_features_dim, int):
        src_dim = dst_dim = node_features_dim
    else:
        src_dim, dst_dim = node_features_dim
    edge_dim = edge_features_dim if edge_features_dim is not None else 0

    # Same names as PyG.
    self.lin_key = Linear(dst_dim + edge_dim, out_features_dim)
    self.lin_query = Linear(src_dim + edge_dim, out_features_dim)
    self.lin_value = Linear(src_dim + edge_dim, out_features_dim)
    if root_weight:
        self.lin_skip = Linear(dst_dim, out_features_dim, bias=False)
    if bias:
        self.bias = mx.zeros((out_features_dim,))

__call__

__call__(
    edge_index: array,
    node_features: Union[array, Tuple[array, array]],
    edge_features: Optional[array] = None,
) -> array

Computes the forward pass of ResGatedGraphConv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features Union[array, Tuple[array, array]]

Input node features, or a (src_features, dst_features) tuple for bipartite graphs

required
edge_features Optional[array]

Edge features of shape [num_edges, edge_features_dim], required if and only if edge_features_dim is set. Default: None

None

Returns:

Type Description
array

The computed node embeddings, one row per destination node

Source code in mlx_graphs/nn/conv/res_gated_graph_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: Union[mx.array, Tuple[mx.array, mx.array]],
    edge_features: Optional[mx.array] = None,
) -> mx.array:
    """Computes the forward pass of ResGatedGraphConv.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features, or a ``(src_features,
            dst_features)`` tuple for bipartite graphs
        edge_features: Edge features of shape `[num_edges, edge_features_dim]`,
            required if and only if ``edge_features_dim`` is set.
            Default: ``None``

    Returns:
        mx.array: The computed node embeddings, one row per destination node
    """
    if (edge_features is not None) != (self.edge_features_dim is not None):
        raise ValueError(
            "`edge_features` must be given if and only if the layer was "
            "created with `edge_features_dim`."
        )
    if isinstance(node_features, tuple):
        src_features, dst_features = node_features
    else:
        src_features = dst_features = node_features

    if edge_features is None:
        # Project the nodes once; queries and values travel together.
        src = mx.concatenate(
            [self.lin_query(src_features), self.lin_value(src_features)], axis=-1
        )
        dst = self.lin_key(dst_features)
    else:
        src, dst = src_features, dst_features

    out = self.propagate(
        edge_index=edge_index,
        node_features=(src, dst),
        message_kwargs={"edge_features": edge_features},
    )

    if self.root_weight:
        out = out + self.lin_skip(dst_features)
    if "bias" in self:
        out = out + self.bias
    return out

from mlx_graphs.nn import ResGatedGraphConv · mlx_graphs/nn/conv/res_gated_graph_conv.py