Skip to content

gcn_norm

gcn_norm(
    edge_index: array,
    num_nodes: int,
    edge_weights: Optional[array] = None,
    add_self_loops: bool = True,
    fill_value: float = 1.0,
) -> Tuple[array, array]

Symmetric GCN normalization of a graph.

\[ \hat{e}_{j,i} = \frac{e_{j,i}}{\sqrt{\hat{d}_j \hat{d}_i}}, \quad \hat{d}_i = \sum_{j \in \mathcal{N}(i)} e_{j,i} \]

Degrees are the (weighted) in-degrees, computed after the self-loops are added. Nodes with a degree of 0 get a normalization of 0.

With add_self_loops, a self-loop of weight fill_value (1 by default, 2 for PyG's improved=True) is added to every node that does not have one. Existing self-loops keep their weight, as in PyG. The returned edge_index always has num_edges + num_nodes columns in that case: the loop appended to a node that already has one has weight 0.

Parameters:

Name Type Description Default
edge_index array

Graph connectivity of shape [2, num_edges]

required
num_nodes int

Number of nodes in the graph

required
edge_weights Optional[array]

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

None
add_self_loops bool

Whether to add self-loops before normalizing

True
fill_value float

Weight of the added self-loops. Default: 1.0

1.0

Returns:

Type Description
array

The edge index (with self-loops if requested) and one normalized weight

array

per edge, of shape [num_edges_out].

Source code in mlx_graphs/nn/conv/_norm.py
def gcn_norm(
    edge_index: mx.array,
    num_nodes: int,
    edge_weights: Optional[mx.array] = None,
    add_self_loops: bool = True,
    fill_value: float = 1.0,
) -> Tuple[mx.array, mx.array]:
    r"""Symmetric GCN normalization of a graph.

    .. math::
        \hat{e}_{j,i} = \frac{e_{j,i}}{\sqrt{\hat{d}_j \hat{d}_i}}, \quad
        \hat{d}_i = \sum_{j \in \mathcal{N}(i)} e_{j,i}

    Degrees are the (weighted) in-degrees, computed after the self-loops are
    added. Nodes with a degree of 0 get a normalization of 0.

    With ``add_self_loops``, a self-loop of weight ``fill_value`` (1 by
    default, 2 for PyG's ``improved=True``) is added to every node that does
    not have one. Existing self-loops keep their weight, as in PyG.
    The returned ``edge_index`` always has ``num_edges + num_nodes`` columns in
    that case: the loop appended to a node that already has one has weight 0.

    Args:
        edge_index: Graph connectivity of shape `[2, num_edges]`
        num_nodes: Number of nodes in the graph
        edge_weights: Optional scalar weight per edge, of shape `[num_edges]`
            or `[num_edges, 1]`. Default: ``None`` (all ones)
        add_self_loops: Whether to add self-loops before normalizing
        fill_value: Weight of the added self-loops. Default: ``1.0``

    Returns:
        The edge index (with self-loops if requested) and one normalized weight
        per edge, of shape `[num_edges_out]`.
    """
    row, col = edge_index[0], edge_index[1]
    weights = None if edge_weights is None else edge_weights.reshape(-1)

    # In-degree; the unweighted case uses the exact integer count.
    deg = degree(col, num_nodes, edge_weights=weights)
    if weights is None:
        weights = mx.ones((edge_index.shape[1],), dtype=mx.float32)

    if add_self_loops:
        is_loop = (row == col).astype(mx.int32)
        num_loops = mx.zeros((num_nodes,), dtype=mx.int32).at[row].add(is_loop)
        loop_weights = (num_loops == 0).astype(weights.dtype)
        if fill_value != 1.0:
            loop_weights = loop_weights * fill_value

        deg = deg + loop_weights
        weights = mx.concatenate([weights, loop_weights])
        edge_index = _append_self_loops(edge_index, num_nodes)
        row, col = edge_index[0], edge_index[1]

    deg_inv_sqrt = invert_sqrt_degree(deg)
    return edge_index, deg_inv_sqrt[row] * weights * deg_inv_sqrt[col]

from mlx_graphs.nn import gcn_norm · mlx_graphs/nn/conv/_norm.py