Skip to content

GCNNorm

GCNNorm(add_self_loops: bool = True)

Bases: BaseTransform

Applies the GCN normalization from "Semi-supervised Classification with Graph Convolutional Networks" to the edges of a graph and stores the result in edge_weights:

\[ \mathbf{\hat{A}} = \mathbf{\hat{D}}^{-1/2} (\mathbf{A} + \mathbf{I}) \mathbf{\hat{D}}^{-1/2} \]

where \(\hat{D}_{ii} = \sum_{j} (\mathbf{A} + \mathbf{I})_{ji}\) is the weighted in-degree. The edge_weights attribute of the graph is used as \(\mathbf{A}\) if it exists.

With add_self_loops, every node gets exactly one self-loop, placed after the other edges: a node that already has a self-loop keeps its weight, the others get a self-loop of weight 1. The other per-edge attributes (e.g. edge_features) are kept aligned with the new edge_index: existing self-loops keep their values and the added ones get zeros.

Parameters:

Name Type Description Default
add_self_loops bool

Whether to add self-loops before normalizing. Default: True

True

Example:

graph = GraphData(edge_index=mx.array([[0, 1], [1, 0]]))
graph = GCNNorm()(graph)
graph.edge_index
# mx.array([[0, 1, 0, 1], [1, 0, 0, 1]])
graph.edge_weights
# mx.array([0.5, 0.5, 0.5, 0.5])

Methods:

Name Description
process

Normalizes the edges of data.

Source code in mlx_graphs/transforms/features.py
def __init__(self, add_self_loops: bool = True):
    self.add_self_loops = add_self_loops

process

process(data: GraphData) -> GraphData

Normalizes the edges of data.

Parameters:

Name Type Description Default
data GraphData

The graph to transform.

required

Returns:

Type Description
GraphData

The graph with the normalized edge_weights (and the self-loops

GraphData

if requested).

Source code in mlx_graphs/transforms/features.py
def process(self, data: GraphData) -> GraphData:
    """Normalizes the edges of ``data``.

    Args:
        data: The graph to transform.

    Returns:
        The graph with the normalized ``edge_weights`` (and the self-loops
        if requested).
    """
    num_nodes = _num_nodes(data)
    edges = _host_edges(data)
    weights = _edge_weights(data)

    if self.add_self_loops:
        _, edge_keys = _attribute_names(data, num_nodes)
        is_loop = edges[0] == edges[1]
        kept = np.nonzero(~is_loop)[0]
        loops = np.nonzero(is_loop)[0]
        loop_nodes = edges[0][loops]

        for key in edge_keys:
            if key == "edge_weights":
                continue
            values = getattr(data, key)
            loop_values = mx.zeros((num_nodes, *values.shape[1:]), values.dtype)
            if loops.size > 0:
                loop_values[mx.array(loop_nodes)] = values[mx.array(loops)]
            if kept.size > 0:
                loop_values = mx.concatenate(
                    [values[mx.array(kept)], loop_values], axis=0
                )
            setattr(data, key, loop_values)

        loop_weights = np.ones(num_nodes, dtype=np.float64)
        loop_weights[loop_nodes] = weights[loops]
        weights = np.concatenate([weights[kept], loop_weights])
        nodes = np.arange(num_nodes, dtype=np.int64)
        edges = np.concatenate([edges[:, kept], np.stack([nodes, nodes])], axis=1)
        data.edge_index = mx.array(edges, dtype=data.edge_index.dtype)
        data.num_nodes = num_nodes

    in_degree = _weighted_bincount(edges[1], weights, num_nodes)
    deg_inv_sqrt = np.zeros_like(in_degree)
    np.power(in_degree, -0.5, out=deg_inv_sqrt, where=in_degree > 0)
    weights = deg_inv_sqrt[edges[0]] * weights * deg_inv_sqrt[edges[1]]
    data.edge_weights = mx.array(weights.astype(np.float32))
    return data

from mlx_graphs.transforms import GCNNorm · mlx_graphs/transforms/features.py