Skip to content

SplineConv

SplineConv(
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    dim: int,
    kernel_size: Union[int, List[int]],
    is_open_spline: Union[bool, List[bool]] = True,
    degree: int = 1,
    aggr: str = "mean",
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
)

Bases: MessagePassing

The spline-based convolutional operator from the "SplineCNN: Fast Geometric Deep Learning with Continuous B-Spline Kernels" paper.

\[ \mathbf{x}^{\prime}_i = \frac{1}{|\mathcal{N}(i)|} \sum_{j \in \mathcal{N}(i)} \mathbf{x}_j \cdot h_{\mathbf{\Theta}}(\mathbf{e}_{i,j}) + \mathbf{\Theta}_r \mathbf{x}_i + \mathbf{b}, \]

where \(h_{\mathbf{\Theta}}\) is a kernel function defined over the weighted B-spline tensor product basis of the pseudo-coordinates \(\mathbf{e}_{i,j} \in [0, 1]^D\) (edge_features, e.g. from the Cartesian transform).

Parameters:

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

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

required
out_features_dim int

Size of the output features.

required
dim int

Pseudo-coordinate dimensionality \(D\).

required
kernel_size Union[int, List[int]]

Size of the convolving kernel in each dimension.

required
is_open_spline Union[bool, List[bool]]

Whether to use open (True) or closed B-spline bases, in each dimension. Default: True

True
degree int

B-spline basis degree (1, 2 or 3). Default: 1

1
aggr str

Aggregation of the messages. Default: "mean"

'mean'
root_weight bool

Whether to add the transformed root node features. Default: True

True
bias bool

Whether to add a bias. Default: True

True
**kwargs

Arguments of MessagePassing

{}

Example:

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

edge_index = mx.array([[0, 1, 2, 3], [1, 0, 3, 2]])
x = mx.random.normal((4, 16))
pseudo = mx.random.uniform(shape=(4, 2))
conv = SplineConv(16, 32, dim=2, kernel_size=5)
>>> conv(edge_index, x, pseudo).shape
(4, 32)

Methods:

Name Description
__call__

Computes the forward pass of SplineConv.

Source code in mlx_graphs/nn/conv/spline_conv.py
def __init__(
    self,
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    dim: int,
    kernel_size: Union[int, List[int]],
    is_open_spline: Union[bool, List[bool]] = True,
    degree: int = 1,
    aggr: str = "mean",
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
):
    super().__init__(aggr=aggr, **kwargs)
    if degree not in (1, 2, 3):
        raise ValueError(f"degree must be 1, 2 or 3, got {degree}")
    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.dim = dim
    self.degree = degree
    self.root_weight = root_weight
    self.kernel_size = tuple(int(k) for k in _repeat(kernel_size, dim))
    self.is_open_spline = tuple(bool(o) for o in _repeat(is_open_spline, dim))
    self.K = int(np.prod(self.kernel_size))
    src_dim, dst_dim = (
        (node_features_dim, node_features_dim)
        if isinstance(node_features_dim, int)
        else node_features_dim
    )
    bound = 1.0 / math.sqrt(self.K * src_dim)
    self.weight = mx.random.uniform(
        -bound, bound, (self.K, src_dim, out_features_dim)
    )
    if root_weight:
        self.lin = Linear(dst_dim, out_features_dim, bias=False)
    if bias:
        self.bias = mx.zeros((out_features_dim,))

__call__

__call__(
    edge_index: array,
    node_features: Union[array, Tuple[array, array]],
    edge_features: array,
) -> array

Computes the forward pass of SplineConv.

Parameters:

Name Type Description Default
edge_index array

Edges of shape [2, num_edges].

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

Node features, or a (src_features, dst_features) tuple for bipartite graphs.

required
edge_features array

Pseudo-coordinates of shape [num_edges, dim], in [0, 1].

required

Returns:

Type Description
array

The features of the destination nodes.

Source code in mlx_graphs/nn/conv/spline_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: Union[mx.array, Tuple[mx.array, mx.array]],
    edge_features: mx.array,
) -> mx.array:
    """Computes the forward pass of SplineConv.

    Args:
        edge_index: Edges of shape ``[2, num_edges]``.
        node_features: Node features, or a ``(src_features,
            dst_features)`` tuple for bipartite graphs.
        edge_features: Pseudo-coordinates of shape ``[num_edges, dim]``,
            in ``[0, 1]``.

    Returns:
        The features of the destination nodes.
    """
    if isinstance(node_features, tuple):
        dst_features = node_features[1]
    else:
        dst_features = node_features
        node_features = (node_features, node_features)
    out = self.propagate(
        edge_index=edge_index,
        node_features=node_features,
        message_kwargs={"edge_features": edge_features},
    )
    if self.root_weight:
        out = out + self.lin(dst_features)
    if "bias" in self:
        out = out + self.bias
    return out

from mlx_graphs.nn import SplineConv · mlx_graphs/nn/conv/spline_conv.py