Cwn_Layer#

Implementation of CWN layer from Bodnar et al.: Weisfeiler and Lehman Go Cellular: CW Networks.

class topomodelx.nn.cell.cwn_layer.CWNLayer(in_channels_0, in_channels_1, in_channels_2, out_channels, conv_1_to_1=None, conv_0_to_1=None, aggregate_fn=None, update_fn=None, **kwargs)[source]#

Layer of a CW Network (CWN).

Implementation of the CWN layer proposed in [1].

This module is composed of the following layers: 1. A convolutional layer that sends messages from r-cells to r-cells. 2. A convolutional layer that sends messages from (r-1)-cells to r-cells. 3. A layer that creates representations in r-cells based on the received messages. 4. A layer that updates representations in r-cells.

Parameters:
in_channels_0int

Dimension of input features on (r-1)-cells (nodes in case r = 1).

in_channels_1int

Dimension of input features on r-cells (edges in case r = 1).

in_channels_2int

Dimension of input features on (r+1)-cells (faces in case r = 1).

out_channelsint

Dimension of output features on r-cells.

conv_1_to_1torch.nn.Module, optional

A module that convolves the representations of upper-adjacent neighbors of r-cells and their corresponding co-boundary (r+1) cells.

If None is passed, a default implementation of this module is used (check the docstring of _CWNDefaultFirstConv for more detail).

conv_0_to_1torch.nn.Module, optional

A module that convolves the representations of (r-1)-cells on the boundary of r-cells.

If None is passed, a default implementation of this module is used (check the docstring of _CWNDefaultSecondConv for more detail).

aggregate_fntorch.nn.Module, optional

A module that aggregates the representations of r-cells obtained by convolutional layers.

If None is passed, a default implementation of this module is used (check the docstring of _CWNDefaultAggregate for more detail).

update_fntorch.nn.Module, optional

A module that updates the aggregated representations of r-cells.

If None is passed, a default implementation of this module is used (check the docstring of _CWNDefaultUpdate for more detail).

**kwargsoptional

Additional arguments for the modules of the CWN layer.

Methods

add_module(name, module)

Add a child module to the current module.

apply(fn)

Apply fn recursively to every submodule (as returned by .children()) as well as self.

bfloat16()

Casts all floating point parameters and buffers to bfloat16 datatype.

buffers([recurse])

Return an iterator over module buffers.

children()

Return an iterator over immediate children modules.

compile(*args, **kwargs)

Compile this Module's forward using torch.compile().

cpu()

Move all model parameters and buffers to the CPU.

cuda([device])

Move all model parameters and buffers to the GPU.

double()

Casts all floating point parameters and buffers to double datatype.

eval()

Set the module in evaluation mode.

extra_repr()

Return the extra representation of the module.

float()

Casts all floating point parameters and buffers to float datatype.

forward(x_0, x_1, x_2, adjacency_0, ...)

Forward pass.

get_buffer(target)

Return the buffer given by target if it exists, otherwise throw an error.

get_extra_state()

Return any extra state to include in the module's state_dict.

get_parameter(target)

Return the parameter given by target if it exists, otherwise throw an error.

get_submodule(target)

Return the submodule given by target if it exists, otherwise throw an error.

half()

Casts all floating point parameters and buffers to half datatype.

ipu([device])

Move all model parameters and buffers to the IPU.

load_state_dict(state_dict[, strict, assign])

Copy parameters and buffers from state_dict into this module and its descendants.

modules([remove_duplicate])

Return an iterator over all modules in the network.

mtia([device])

Move all model parameters and buffers to the MTIA.

named_buffers([prefix, recurse, ...])

Return an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

named_children()

Return an iterator over immediate children modules, yielding both the name of the module as well as the module itself.

named_modules([memo, prefix, remove_duplicate])

Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself.

named_parameters([prefix, recurse, ...])

Return an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.

parameters([recurse])

Return an iterator over module parameters.

register_backward_hook(hook)

Register a backward hook on the module.

register_buffer(name, tensor[, persistent])

Add a buffer to the module.

register_forward_hook(hook, *[, prepend, ...])

Register a forward hook on the module.

register_forward_pre_hook(hook, *[, ...])

Register a forward pre-hook on the module.

register_full_backward_hook(hook[, prepend])

Register a backward hook on the module.

register_full_backward_pre_hook(hook[, prepend])

Register a backward pre-hook on the module.

register_load_state_dict_post_hook(hook)

Register a post-hook to be run after module's load_state_dict() is called.

register_load_state_dict_pre_hook(hook)

Register a pre-hook to be run before module's load_state_dict() is called.

register_module(name, module)

Alias for add_module().

register_parameter(name, param)

Add a parameter to the module.

register_state_dict_post_hook(hook)

Register a post-hook for the state_dict() method.

register_state_dict_pre_hook(hook)

Register a pre-hook for the state_dict() method.

requires_grad_([requires_grad])

Change if autograd should record operations on parameters in this module.

set_extra_state(state)

Set extra state contained in the loaded state_dict.

set_submodule(target, module[, strict])

Set the submodule given by target if it exists, otherwise throw an error.

share_memory()

See torch.Tensor.share_memory_().

state_dict(*args[, destination, prefix, ...])

Return a dictionary containing references to the whole state of the module.

to(*args, **kwargs)

Move and/or cast the parameters and buffers.

to_empty(*, device[, recurse])

Move the parameters and buffers to the specified device without copying storage.

train([mode])

Set the module in training mode.

type(dst_type)

Casts all parameters and buffers to dst_type.

xpu([device])

Move all model parameters and buffers to the XPU.

zero_grad([set_to_none])

Reset gradients of all model parameters.

__call__

References

[1]

Bodnar, et al. Weisfeiler and Lehman go cellular: CW networks. NeurIPS 2021. https://arxiv.org/abs/2106.12575

forward(x_0, x_1, x_2, adjacency_0, incidence_2, incidence_1_t)[source]#

Forward pass.

The forward pass was initially proposed in [1]_. Its equations are given in [2] and graphically illustrated in [3].

The forward pass of this layer is composed of two convolutional steps that are followed by an aggregation step and a final update step.

1. The first convolution between r-cells through (r+1)-cells exploits upper-adjacency neighborhood matrix and co-boundary matrix:

\[\begin{split}\begin{align*} &🟥 \quad m_{y \rightarrow \{z\} \rightarrow x}^{(r \rightarrow r' \rightarrow r)} = M_{\mathcal{L}\uparrow}(h_x^{t,(r)}, h_y^{t,(r)}, h_z^{t,(r')})\\ &🟧 \quad m_x^{(r \rightarrow r' \rightarrow r)} = \text{AGG}_{y \in \mathcal{L}(x)} m_{y \rightarrow \{z\} \rightarrow x}^{(r \rightarrow r' \rightarrow r)} \end{align*}\end{split}\]

2. The second convolution from (r-1)-cells to r-cells exploits boundary neighborhood matrix:

\[\begin{split}\begin{align*} &🟥 m_{y \rightarrow x}^{(r'' \rightarrow r)} = M_{\mathcal{B}}(h_x^{t,(r)}, h_y^{t,(r'')})\\ &🟧 \quad m_x^{(r'' \rightarrow r)} = \text{AGG}_{y \in \mathcal{B}(x)} m_{y \rightarrow x}^{(r'' \rightarrow r)} \end{align*}\end{split}\]
  1. Then, an aggregation step is applied:

\[\begin{align*} &🟧 \quad m_x^{(r)} = AGG_{\mathcal{N}\_k \in \mathcal{N}} (m_x^k) \end{align*}\]
  1. Finally, an update step is applied:

\[\begin{align*} &🟦 \quad h_x^{t+1,(r)} = U\left(h_x^{t,(r)}, m_x^{(r)}\right) \end{align*}\]
Parameters:
x_0torch.Tensor, shape = (n_{r-1}_cells, in_channels_{r-1})

Input features on the (r-1)-cells.

x_1torch.Tensor, shape = (n_{r}_cells, in_channels_{r})

Input features on the r-cells.

x_2torch.Tensor, shape = (n_{r+1}_cells, in_channels_{r+1})

Input features on the (r+1)-cells.

adjacency_0torch.sparse, shape = (n_{r}_cells, n_{r}_cells)

Neighborhood matrix mapping r-cells to r-cells (A_{up,r}).

incidence_2torch.sparse, shape = (n_{r}_cells, n_{r+1}_cells)

Neighborhood matrix mapping (r+1)-cells to r-cells (B_{r+1}).

incidence_1_ttorch.sparse, shape = (n_{r}_cells, n_{r-1}_cells)

Neighborhood matrix mapping (r-1)-cells to r-cells (B^T_r).

Returns:
torch.Tensor, shape = (n_{r}_cells, out_channels)

Updated representations of the r-cells.

References

[2]

Papillon, Sanborn, Hajij, Miolane. Equations of topological neural networks (2023). awesome-tnns/awesome-tnns

[3]

Papillon, Sanborn, Hajij, Miolane. Architectures of topological deep learning: a survey on topological neural networks (2023). https://arxiv.org/abs/2304.10031.