Skip to content

get_num_hops

get_num_hops(model: Module) -> int

Returns the number of hops the model is aggregating information from. This works only for networks based on MessagePassing.

Parameters:

Name Type Description Default
model Module

The GNN Model.

required

Returns:

Type Description
int

number of hops the model is aggregating information

Example:

class GNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = GCNConv(4, 16)
        self.conv2 = GCNConv(16, 16)
        self.lin = nn.linear(16, 2)

    def __call__(self, edge_index: mx.array, node_features: mx.array):
        x = nn.relu(self.conv1(node_features, edge_index))
        x = self.conv2(node_features, edge_index)
        return self.lin(x)


get_num_hops(GNN())
# 2
Source code in mlx_graphs/utils/topology.py
def get_num_hops(model: nn.Module) -> int:
    """
    Returns the number of hops the model is aggregating information
    from. This works only for networks based on `MessagePassing`.

    Args:
        model: The GNN Model.

    Returns:
        number of hops the model is aggregating information

    Example:

    .. code-block:: python

        class GNN(nn.Module):
            def __init__(self):
                super().__init__()
                self.conv1 = GCNConv(4, 16)
                self.conv2 = GCNConv(16, 16)
                self.lin = nn.linear(16, 2)

            def __call__(self, edge_index: mx.array, node_features: mx.array):
                x = nn.relu(self.conv1(node_features, edge_index))
                x = self.conv2(node_features, edge_index)
                return self.lin(x)


        get_num_hops(GNN())
        # 2
    """
    from mlx_graphs.nn import MessagePassing

    num_hops = 0
    for module in model.modules():
        if isinstance(module, MessagePassing):
            num_hops += 1
    return num_hops

from mlx_graphs.utils.topology import get_num_hops · mlx_graphs/utils/topology.py