Skip to content

FiLMConv

FiLMConv(
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    num_relations: int = 1,
    mlp: Optional[Module] = None,
    act: Optional[Callable[[array], array]] = relu,
    aggr: str = "mean",
    **kwargs,
)

Bases: MessagePassing

Feature-wise linear modulation (FiLM) graph convolution layer from the "GNN-FiLM: Graph Neural Networks with Feature-wise Linear Modulation" paper.

\[ \mathbf{h}_i = \sigma \left( \boldsymbol{\gamma}_{\textrm{skip},i} \odot \mathbf{\Theta}_\textrm{skip} \mathbf{x}_i + \boldsymbol{\beta}_{\textrm{skip},i} \right) + \sum_{r \in \mathcal{R}} \bigoplus_{j \in \mathcal{N}_r(i)} \sigma \left( \boldsymbol{\gamma}_{r,i} \odot \mathbf{\Theta}_r \mathbf{x}_j + \boldsymbol{\beta}_{r,i} \right) \]

where \(\boldsymbol{\beta}_{r,i}, \boldsymbol{\gamma}_{r,i} = g_r(\mathbf{x}_i)\), with \(g_r\) a linear layer by default (or a copy of mlp) whose output is split in two halves, and \(\mathcal{N}_r(i)\) the neighbours of \(i\) through edges of relation \(r\).

Parameters:

Name Type Description Default
node_features_dim Union[int, Tuple[int, int]]

Size of input node features, or a (src_dim, dst_dim) tuple for bipartite graphs

required
out_features_dim int

Size of output node embeddings

required
num_relations int

Number of relations (edge types). Default: 1

1
mlp Optional[Module]

Module mapping destination node features to 2 * out_features_dim values \([\boldsymbol{\beta}, \boldsymbol{\gamma}]\). It is deep-copied for every relation and for the skip connection. Default: None (a linear layer)

None
act Optional[Callable[[array], array]]

Activation function \(\sigma\), or None. Default: mlx.nn.relu()

relu
aggr str

Aggregation \(\bigoplus\) within each relation. Default: "mean"

'mean'
**kwargs

Arguments of MessagePassing

{}

Example:

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

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

conv = FiLMConv(16, 32, num_relations=2)
h = conv(edge_index, node_features, edge_type)
>>> h.shape
(5, 32)

Methods:

Name Description
__call__

Computes the forward pass of FiLMConv.

Source code in mlx_graphs/nn/conv/film_conv.py
def __init__(
    self,
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    num_relations: int = 1,
    mlp: Optional[nn.Module] = None,
    act: Optional[Callable[[mx.array], mx.array]] = nn.relu,
    aggr: str = "mean",
    **kwargs,
):
    super(FiLMConv, self).__init__(aggr=aggr, **kwargs)

    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.num_relations = max(num_relations, 1)
    self.act = act

    if isinstance(node_features_dim, int):
        src_dim = dst_dim = node_features_dim
    else:
        src_dim, dst_dim = node_features_dim

    # Same names as PyG.
    self.lins = [
        Linear(src_dim, out_features_dim, bias=False)
        for _ in range(self.num_relations)
    ]
    self.films = [
        Linear(dst_dim, 2 * out_features_dim) if mlp is None else copy.deepcopy(mlp)
        for _ in range(self.num_relations)
    ]
    self.lin_skip = Linear(dst_dim, out_features_dim, bias=False)
    self.film_skip = (
        Linear(dst_dim, 2 * out_features_dim, bias=False)
        if mlp is None
        else copy.deepcopy(mlp)
    )

__call__

__call__(
    edge_index: array,
    node_features: Union[array, Tuple[array, array]],
    edge_type: Optional[array] = None,
) -> array

Computes the forward pass of FiLMConv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features Union[array, Tuple[array, array]]

Input node features, or a (src_features, dst_features) tuple for bipartite graphs

required
edge_type Optional[array]

Relation of each edge, of shape [num_edges], with values in [0, num_relations). Required if num_relations > 1. Default: None

None

Returns:

Type Description
array

The computed node embeddings, one row per destination node

Source code in mlx_graphs/nn/conv/film_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: Union[mx.array, Tuple[mx.array, mx.array]],
    edge_type: Optional[mx.array] = None,
) -> mx.array:
    """Computes the forward pass of FiLMConv.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features, or a ``(src_features,
            dst_features)`` tuple for bipartite graphs
        edge_type: Relation of each edge, of shape `[num_edges]`, with values
            in ``[0, num_relations)``. Required if ``num_relations > 1``.
            Default: ``None``

    Returns:
        mx.array: The computed node embeddings, one row per destination node
    """
    if isinstance(node_features, tuple):
        src_features, dst_features = node_features
    else:
        src_features = dst_features = node_features
    num_dst, R = dst_features.shape[0], self.num_relations

    beta, gamma = mx.split(self.film_skip(dst_features), 2, axis=-1)
    out = gamma * self.lin_skip(dst_features) + beta
    if self.act is not None:
        out = self.act(out)

    if R == 1:
        x_src = self.lins[0](src_features)
        film = self.films[0](dst_features)
    else:
        if edge_type is None:
            raise ValueError("`edge_type` is required when num_relations > 1.")
        # All relations in one pass: node j of relation r becomes node
        # r * num_nodes + j, so messages are aggregated per (node,
        # relation) pair, and then summed over the relations.
        num_src = src_features.shape[0]
        edge_type = edge_type.reshape(-1).astype(edge_index.dtype)
        edge_index = mx.stack(
            [
                edge_index[0] + edge_type * num_src,
                edge_index[1] + edge_type * num_dst,
            ]
        )
        x_src = mx.concatenate([lin(src_features) for lin in self.lins])
        film = mx.concatenate([f(dst_features) for f in self.films])

    # The FiLM parameters [beta, gamma] travel as destination features.
    aggregated = self.propagate(edge_index=edge_index, node_features=(x_src, film))
    return out + aggregated.reshape(R, num_dst, -1).sum(axis=0)

from mlx_graphs.nn import FiLMConv · mlx_graphs/nn/conv/film_conv.py