Skip to content

GCN2Conv

GCN2Conv(
    node_features_dim: int,
    alpha: float,
    theta: Optional[float] = None,
    layer: Optional[int] = None,
    shared_weights: bool = True,
    cached: bool = False,
    add_self_loops: bool = True,
    normalize: bool = True,
    **kwargs,
)

Bases: MessagePassing

Graph convolution with initial residual connections and identity mapping (GCNII) from the "Simple and Deep Graph Convolutional Networks" paper.

\[ \mathbf{H}^{(\ell + 1)} = \left( (1 - \alpha) \hat{\mathbf{P}} \mathbf{H}^{(\ell)} + \alpha \mathbf{H}^{(0)} \right) \left( (1 - \beta) \mathbf{I} + \beta \mathbf{W} \right) \]

with \(\hat{\mathbf{P}} = \hat{\mathbf{D}}^{-1/2} \hat{\mathbf{A}} \hat{\mathbf{D}}^{-1/2}\), where \(\hat{\mathbf{A}} = \mathbf{A} + \mathbf{I}\) is the (weighted) adjacency matrix with self-loops, \(\mathbf{H}^{(0)}\) the initial node features, and \(\beta = \log(\theta / \ell + 1)\) (beta = 1 if theta and layer are not given). With shared_weights=False, the initial features get their own weight matrix:

\[ \mathbf{H}^{(\ell + 1)} = (1 - \alpha) \hat{\mathbf{P}} \mathbf{H}^{(\ell)} \left( (1 - \beta) \mathbf{I} + \beta \mathbf{W}_1 \right) + \alpha \mathbf{H}^{(0)} \left( (1 - \beta) \mathbf{I} + \beta \mathbf{W}_2 \right) \]

Parameters:

Name Type Description Default
node_features_dim int

Size of the input and output node features

required
alpha float

Strength \(\alpha\) of the initial residual connection

required
theta Optional[float]

Hyperparameter \(\theta\) used to compute the strength of the identity mapping. Default: None

None
layer Optional[int]

Index \(\ell\) of the layer (starting from 1), used to compute the strength of the identity mapping. Default: None

None
shared_weights bool

If False, uses a separate weight matrix for the initial features. Default: True

True
cached bool

If True, the normalized graph is computed on the first call and reused afterwards (transductive learning only). Call clear_cache() to drop it. Default: False

False
add_self_loops bool

Whether to add a self-loop to every node that has none. Default: True

True
normalize bool

Whether to apply the symmetric degree normalization. Default: True

True
**kwargs

Arguments of MessagePassing

{}

Example:

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

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

conv = GCN2Conv(16, alpha=0.1, theta=0.5, layer=1)
h = conv(edge_index, initial_features, initial_features)
>>> h.shape
(5, 16)

Methods:

Name Description
clear_cache

Drops the normalized graph stored when cached=True.

__call__

Computes the forward pass of GCN2Conv.

Source code in mlx_graphs/nn/conv/gcn2_conv.py
def __init__(
    self,
    node_features_dim: int,
    alpha: float,
    theta: Optional[float] = None,
    layer: Optional[int] = None,
    shared_weights: bool = True,
    cached: bool = False,
    add_self_loops: bool = True,
    normalize: bool = True,
    **kwargs,
):
    kwargs.setdefault("aggr", "add")
    super(GCN2Conv, self).__init__(**kwargs)

    self.node_features_dim = node_features_dim
    self.alpha = alpha
    self.beta = 1.0
    if theta is not None or layer is not None:
        if theta is None or layer is None:
            raise ValueError("`theta` and `layer` must be given together.")
        self.beta = log(theta / layer + 1)
    self.cached = cached
    self.normalize = normalize
    self._add_self_loops = add_self_loops
    self._cached_graph: Optional[Tuple[mx.array, mx.array]] = None

    # Applied as `x @ weight`, as in PyG.
    self.weight1 = glorot((node_features_dim, node_features_dim))
    if not shared_weights:
        self.weight2 = glorot((node_features_dim, node_features_dim))

clear_cache

clear_cache() -> None

Drops the normalized graph stored when cached=True.

Source code in mlx_graphs/nn/conv/gcn2_conv.py
def clear_cache(self) -> None:
    """Drops the normalized graph stored when ``cached=True``."""
    self._cached_graph = None

__call__

__call__(
    edge_index: array,
    node_features: array,
    initial_node_features: array,
    edge_weights: Optional[array] = None,
) -> array

Computes the forward pass of GCN2Conv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features array

Node features of the previous layer \(\mathbf{H}^{(\ell)}\)

required
initial_node_features array

Initial node features \(\mathbf{H}^{(0)}\) (usually the output of the first projection of the model)

required
edge_weights Optional[array]

Optional scalar weight per edge, of shape [num_edges] or [num_edges, 1]. Default: None

None

Returns:

Type Description
array

The computed node embeddings

Source code in mlx_graphs/nn/conv/gcn2_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: mx.array,
    initial_node_features: mx.array,
    edge_weights: Optional[mx.array] = None,
) -> mx.array:
    """Computes the forward pass of GCN2Conv.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Node features of the previous layer
            :math:`\\mathbf{H}^{(\\ell)}`
        initial_node_features: Initial node features :math:`\\mathbf{H}^{(0)}`
            (usually the output of the first projection of the model)
        edge_weights: Optional scalar weight per edge, of shape `[num_edges]`
            or `[num_edges, 1]`. Default: ``None``

    Returns:
        mx.array: The computed node embeddings
    """
    if self.normalize:
        if self._cached_graph is not None:
            edge_index, edge_weights = self._cached_graph
        else:
            edge_index, edge_weights = cached_gcn_norm(
                edge_index,
                node_features.shape[0],
                edge_weights,
                add_self_loops=self._add_self_loops,
            )
            if self.cached:
                self._cached_graph = (edge_index, edge_weights)

    h = self.propagate(
        edge_index=edge_index,
        node_features=node_features,
        message_kwargs={"edge_weights": edge_weights},
    )
    h = (1 - self.alpha) * h
    h_0 = self.alpha * initial_node_features[: h.shape[0]]

    if "weight2" not in self:
        h = h + h_0
        return (1 - self.beta) * h + self.beta * (h @ self.weight1)

    out = (1 - self.beta) * h + self.beta * (h @ self.weight1)
    return out + (1 - self.beta) * h_0 + self.beta * (h_0 @ self.weight2)

from mlx_graphs.nn import GCN2Conv · mlx_graphs/nn/conv/gcn2_conv.py