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)Adds a child module to the current module.
apply
(fn)Applies
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])Returns an iterator over module buffers.
children
()Returns an iterator over immediate children modules.
cpu
()Moves all model parameters and buffers to the CPU.
cuda
([device])Moves all model parameters and buffers to the GPU.
double
()Casts all floating point parameters and buffers to
double
datatype.eval
()Sets the module in evaluation mode.
extra_repr
()Set 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)Returns the buffer given by
target
if it exists, otherwise throws an error.get_extra_state
()Returns any extra state to include in the module's state_dict.
get_parameter
(target)Returns the parameter given by
target
if it exists, otherwise throws an error.get_submodule
(target)Returns the submodule given by
target
if it exists, otherwise throws an error.half
()Casts all floating point parameters and buffers to
half
datatype.ipu
([device])Moves all model parameters and buffers to the IPU.
load_state_dict
(state_dict[, strict])Copies parameters and buffers from
state_dict
into this module and its descendants.modules
()Returns an iterator over all modules in the network.
named_buffers
([prefix, recurse, ...])Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children
()Returns 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])Returns 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, ...])Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters
([recurse])Returns an iterator over module parameters.
register_backward_hook
(hook)Registers a backward hook on the module.
register_buffer
(name, tensor[, persistent])Adds a buffer to the module.
register_forward_hook
(hook, *[, prepend, ...])Registers a forward hook on the module.
register_forward_pre_hook
(hook, *[, ...])Registers a forward pre-hook on the module.
register_full_backward_hook
(hook[, prepend])Registers a backward hook on the module.
register_full_backward_pre_hook
(hook[, prepend])Registers a backward pre-hook on the module.
register_load_state_dict_post_hook
(hook)Registers a post hook to be run after module's
load_state_dict
is called.register_module
(name, module)Alias for
add_module()
.register_parameter
(name, param)Adds a parameter to the module.
register_state_dict_pre_hook
(hook)These hooks will be called with arguments:
self
,prefix
, andkeep_vars
before callingstate_dict
onself
.requires_grad_
([requires_grad])Change if autograd should record operations on parameters in this module.
set_extra_state
(state)This function is called from
load_state_dict()
to handle any extra state found within the state_dict.share_memory
()See
torch.Tensor.share_memory_()
state_dict
(*args[, destination, prefix, ...])Returns a dictionary containing references to the whole state of the module.
to
(*args, **kwargs)Moves and/or casts the parameters and buffers.
to_empty
(*, device)Moves the parameters and buffers to the specified device without copying storage.
train
([mode])Sets the module in training mode.
type
(dst_type)Casts all parameters and buffers to
dst_type
.xpu
([device])Moves all model parameters and buffers to the XPU.
zero_grad
([set_to_none])Sets gradients of all model parameters to zero.
__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}\]Then, an aggregation step is applied:
\[\begin{align*} &🟧 \quad m_x^{(r)} = AGG_{\mathcal{N}\_k \in \mathcal{N}} (m_x^k) \end{align*}\]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.