Skip to content

GraphNetworkBlock

GraphNetworkBlock(
    node_model: Optional[Module] = None,
    edge_model: Optional[Module] = None,
    graph_model: Optional[Module] = None,
)

Bases: Module

Implements a generic Graph Network block as defined in [1].

A Graph Network block takes as input a graph with N nodes and E edges and returns a graph with the same topology.

The input graph can have - node_features: features associated with each node in the graph, provided as an array of size [N, F_N] - edge_features: features associated with each edge in the graph, provided as an array of size [E, F_E] - graph_features: features associated to the graph itself, of size [F_U]

The topology of the graph is specified as an edge_index, an array of size [2, E], containing the source and destination nodes of each edge as column vectors. A Graph Network block is initialized by providing node, edge and global models (all optional), that are used to update node, edge and global features (if present). Depending on which models are provided and how they are implemented, the Graph Network block acts as a flexible meta-layer that can be used to implement other architectures, like message-passing networks, deep sets, relation networks and more (see [1]).

For a usage example see here.

Parameters:

Name Type Description Default
node_model Optional[Module]

a callable Module which updates a graph's node features

None
edge_model Optional[Module]

a callable Module which updates a graph's edge features

None
graph_model Optional[Module]

a callable Module which updates a graph's global features

None

Methods:

Name Description
__call__

Forward pass of the Graph Network block

Source code in mlx_graphs/nn/graph_network_block.py
def __init__(
    self,
    node_model: Optional[Module] = None,
    edge_model: Optional[Module] = None,
    graph_model: Optional[Module] = None,
):
    super().__init__()
    self.node_model = node_model
    self.edge_model = edge_model
    self.graph_model = graph_model

__call__

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

Forward pass of the Graph Network block

Parameters:

Name Type Description Default
edge_index array

array of size [2, E], where each column contains the source and destination nodes of an edge.

required
node_features Optional[array]

features associated with each node in the graph, provided as an array of size [N, F_N]

None
edge_features Optional[array]

features associated with each edge in the graph, provided as an array of size [E, F_E]

None
graph_features Optional[array]

features associated to the graph itself, of size [F_U]

None

Returns:

Type Description
tuple[Optional[array], Optional[array], Optional[array]]

The tuple of updated node, edge and global attributes.

Source code in mlx_graphs/nn/graph_network_block.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: Optional[mx.array] = None,
    edge_features: Optional[mx.array] = None,
    graph_features: Optional[mx.array] = None,
) -> tuple[Optional[mx.array], Optional[mx.array], Optional[mx.array]]:
    """Forward pass of the Graph Network block

    Args:
        edge_index: array of size [2, E], where each column contains the source
            and destination nodes of an edge.
        node_features: features associated with each node in the
            graph, provided as an array of size [N, F_N]
        edge_features: features associated with each edge in the
            graph, provided as an array of size [E, F_E]
        graph_features: features associated to the graph itself,
            of size [F_U]

    Returns:
        The tuple of updated node, edge and global attributes.
    """
    if self.edge_model:
        edge_features = self.edge_model(
            edge_index, node_features, edge_features, graph_features
        )

    if self.node_model:
        node_features = self.node_model(
            edge_index, node_features, edge_features, graph_features
        )

    if self.graph_model:
        graph_features = self.graph_model(
            edge_index, node_features, edge_features, graph_features
        )

    return node_features, edge_features, graph_features

from mlx_graphs.nn import GraphNetworkBlock · mlx_graphs/nn/graph_network_block.py