AllSet_Transformer_Layer#
AllSetTransformer Layer Module.
- class topomodelx.nn.hypergraph.allset_transformer_layer.AllSetTransformerBlock(in_channels, hidden_channels, heads: int = 4, number_queries: int = 1, dropout: float = 0.0, mlp_num_layers: int = 1, mlp_activation=<class 'torch.nn.modules.activation.ReLU'>, mlp_dropout: float = 0.0, mlp_norm=None, initialization: Literal['xavier_uniform', 'xavier_normal']='xavier_uniform')[source]#
AllSetTransformer Block Module.
A module for AllSet Transformer block in a bipartite graph.
- Parameters:
- in_channelsint
Dimension of the input features.
- hidden_channelsint
Dimension of the hidden features.
- headsint, default=4
Number of attention heads.
- number_queriesint, default=1
Number of queries.
- dropoutfloat, default=0.0
Dropout probability.
- mlp_num_layersint, default=1
Number of layers in the MLP.
- mlp_activationcallable or None, optional
Activation function in the MLP.
- mlp_dropoutfloat, optional
Dropout probability in the MLP.
- mlp_normstr or None, optional
Type of layer normalization in the MLP.
- initializationLiteral[“xavier_uniform”, “xavier_normal”], default=”xavier_uniform”
Initialization method.
Methods
add_module(name, module)Add a child module to the current module.
apply(fn)Apply
fnrecursively to every submodule (as returned by.children()) as well as self.bfloat16()Casts all floating point parameters and buffers to
bfloat16datatype.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
doubledatatype.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
floatdatatype.forward(x_source, neighborhood)Forward computation.
get_buffer(target)Return the buffer given by
targetif 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
targetif it exists, otherwise throw an error.get_submodule(target)Return the submodule given by
targetif it exists, otherwise throw an error.half()Casts all floating point parameters and buffers to
halfdatatype.ipu([device])Move all model parameters and buffers to the IPU.
load_state_dict(state_dict[, strict, assign])Copy parameters and buffers from
state_dictinto 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.
Reset learnable parameters.
set_extra_state(state)Set extra state contained in the loaded state_dict.
set_submodule(target, module[, strict])Set the submodule given by
targetif 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__
- forward(x_source, neighborhood)[source]#
Forward computation.
- Parameters:
- x_sourceTensor, shape = (…, n_source_cells, in_channels)
Inputer features.
- neighborhoodtorch.sparse, shape = (n_target_cells, n_source_cells)
Neighborhood matrix.
- Returns:
- x_message_on_target
Output sum over features on target cells.
- class topomodelx.nn.hypergraph.allset_transformer_layer.AllSetTransformerLayer(in_channels, hidden_channels, heads: int = 4, number_queries: int = 1, dropout: float = 0.0, mlp_num_layers: int = 1, mlp_activation=<class 'torch.nn.modules.activation.ReLU'>, mlp_dropout: float = 0.0, mlp_norm=None, **kwargs)[source]#
Implementation of the AllSetTransformer Layer proposed in [1].
- Parameters:
- in_channelsint
Dimension of the input features.
- hidden_channelsint
Dimension of the hidden features.
- headsint, default=4
Number of attention heads.
- number_queriesint, default=1
Number of queries.
- dropoutfloat, optional
Dropout probability.
- mlp_num_layersint, default=1
Number of layers in the MLP.
- mlp_activationcallable or None, optional
Activation function in the MLP.
- mlp_dropoutfloat, optional
Dropout probability in the MLP.
- mlp_normstr or None, optional
Type of layer normalization in the MLP.
- **kwargsoptional
Additional arguments for the layer modules.
Methods
add_module(name, module)Add a child module to the current module.
apply(fn)Apply
fnrecursively to every submodule (as returned by.children()) as well as self.bfloat16()Casts all floating point parameters and buffers to
bfloat16datatype.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
doubledatatype.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
floatdatatype.forward(x_0, incidence_1)Forward computation.
get_buffer(target)Return the buffer given by
targetif 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
targetif it exists, otherwise throw an error.get_submodule(target)Return the submodule given by
targetif it exists, otherwise throw an error.half()Casts all floating point parameters and buffers to
halfdatatype.ipu([device])Move all model parameters and buffers to the IPU.
load_state_dict(state_dict[, strict, assign])Copy parameters and buffers from
state_dictinto 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.
Reset parameters.
set_extra_state(state)Set extra state contained in the loaded state_dict.
set_submodule(target, module[, strict])Set the submodule given by
targetif 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]Chien, Pan, Peng and Milenkovic. You are AllSet: a multiset function framework for hypergraph neural networks. ICLR 2022. https://arxiv.org/abs/2106.13264
- forward(x_0, incidence_1)[source]#
Forward computation.
Vertex to edge:
\[\begin{split}\begin{align*} &🟧 \quad m_{\rightarrow z}^{(\rightarrow 1)} = AGG_{y \in \\mathcal{B}(z)} (h_y^{t, (0)}, h_z^{t,(1)}) \quad \text{with attention}\\ &🟦 \quad h_z^{t+1,(1)} = \text{LN}(m_{\rightarrow z}^{(\rightarrow 1)} + \text{MLP}(m_{\rightarrow z}^{(\rightarrow 1)} )) \end{align*}\end{split}\]Edge to vertex:
\[\begin{split}\begin{align*} &🟧 \quad m_{\rightarrow x}^{(\rightarrow 0)} = AGG_{z \in \mathcal{C}(x)} (h_z^{t+1,(1)}, h_x^{t,(0)}) \quad \text{with attention}\\ &🟦 \quad h_x^{t+1,(0)} = \text{LN}(m_{\rightarrow x}^{(\rightarrow 0)} + \text{MLP}(m_{\rightarrow x}^{(\rightarrow 0)} )) \end{align*}\end{split}\]- Parameters:
- x_0torch.Tensor, shape = (n_nodes, channels)
Node input features.
- incidence_1torch.sparse, shape = (n_nodes, n_hyperedges)
Incidence matrix \(B_1\) mapping hyperedges to nodes.
- Returns:
- x_0torch.Tensor
Output node features.
- x_1torch.Tensor
Output hyperedge features.
- class topomodelx.nn.hypergraph.allset_transformer_layer.MLP(in_channels, hidden_channels, norm_layer=None, activation_layer=<class 'torch.nn.modules.activation.ReLU'>, dropout: float = 0.0, inplace: bool | None = None, bias: bool = False)[source]#
MLP Module.
A module for a multi-layer perceptron (MLP).
- Parameters:
- in_channelsint
Dimension of the input features.
- hidden_channelslist of int
List of dimensions of the hidden features.
- norm_layercallable or None, optional
Type of layer normalization.
- activation_layercallable or None, optional
Type of activation function.
- dropoutfloat, optional
Dropout probability.
- inplacebool, default=False
Whether to do the operation in-place.
- biasbool, default=False
Whether to add bias.
Methods
add_module(name, module)Add a child module to the current module.
append(module)Append a given module to the end.
apply(fn)Apply
fnrecursively to every submodule (as returned by.children()) as well as self.bfloat16()Casts all floating point parameters and buffers to
bfloat16datatype.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
doubledatatype.eval()Set the module in evaluation mode.
extend(sequential)Extends the current Sequential container with layers from another Sequential container.
extra_repr()Return the extra representation of the module.
float()Casts all floating point parameters and buffers to
floatdatatype.forward(input)Runs the forward pass.
get_buffer(target)Return the buffer given by
targetif 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
targetif it exists, otherwise throw an error.get_submodule(target)Return the submodule given by
targetif it exists, otherwise throw an error.half()Casts all floating point parameters and buffers to
halfdatatype.insert(index, module)Inserts a module into the Sequential container at the specified index.
ipu([device])Move all model parameters and buffers to the IPU.
load_state_dict(state_dict[, strict, assign])Copy parameters and buffers from
state_dictinto 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.
pop(key)Pop
keyfrom self.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
targetif 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__
- class topomodelx.nn.hypergraph.allset_transformer_layer.MultiHeadAttention(in_channels, hidden_channels, aggr_norm: bool = False, update_func=None, heads: int = 4, number_queries: int = 1, initialization: Literal['xavier_uniform', 'xavier_normal'] = 'xavier_uniform', initialization_gain: float = 1.414)[source]#
Computes the multi-head attention mechanism (QK^T)V of transformer-based architectures.
MH module from Eq(7) in AllSet paper [1]_.
- Parameters:
- in_channelsint
Dimension of input features.
- hidden_channelsint
Dimension of hidden features.
- aggr_normbool, default=False
Whether to normalize the aggregated message by the neighborhood size.
- update_funcstr or None, optional
Update method to apply to message.
- headsint, default=4
Number of attention heads.
- number_queriesint, default=1
Number of queries.
- initializationLiteral[“xavier_uniform”, “xavier_normal”], default=”xavier_uniform”
Initialization method.
- initialization_gainfloat, default=1.414
Gain factor for initialization.
Methods
add_module(name, module)Add a child module to the current module.
aggregate(x_message)Aggregate messages on each target cell.
apply(fn)Apply
fnrecursively to every submodule (as returned by.children()) as well as self.attention(x_source, neighborhood)Compute (QK^T) of transformer-based architectures.
bfloat16()Casts all floating point parameters and buffers to
bfloat16datatype.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
doubledatatype.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
floatdatatype.forward(x_source, neighborhood)Forward pass.
get_buffer(target)Return the buffer given by
targetif 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
targetif it exists, otherwise throw an error.get_submodule(target)Return the submodule given by
targetif it exists, otherwise throw an error.half()Casts all floating point parameters and buffers to
halfdatatype.ipu([device])Move all model parameters and buffers to the IPU.
load_state_dict(state_dict[, strict, assign])Copy parameters and buffers from
state_dictinto this module and its descendants.message(x_source[, x_target])Construct message from source cells to target cells.
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.
Reset learnable parameters.
set_extra_state(state)Set extra state contained in the loaded state_dict.
set_submodule(target, module[, strict])Set the submodule given by
targetif 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__
- attention(x_source, neighborhood)[source]#
Compute (QK^T) of transformer-based architectures.
- Parameters:
- x_sourcetorch.Tensor, shape = (n_source_cells, in_channels)
Input features on source cells. Assumes that all source cells have the same rank r.
- neighborhoodtorch.sparse, shape = (n_target_cells, n_source_cells)
Neighborhood matrix.
- Returns:
- torch.Tensor, shape = (n_target_cells, heads, number_queries, n_source_cells)
Attention weights: one scalar per message between a source and a target cell.
- forward(x_source, neighborhood)[source]#
Forward pass.
Computes (QK^T)V attention mechanism of transformer-based architectures. Module MH from Eq (7) in AllSet paper [1]_.
- Parameters:
- x_sourceTensor, shape = (…, n_source_cells, in_channels)
Input features on source cells. Assumes that all source cells have the same rank r.
- neighborhoodtorch.sparse, shape = (n_target_cells, n_source_cells)
Neighborhood matrix.
- Returns:
- Tensor, shape = (…, n_target_cells, out_channels)
Output features on target cells. Assumes that all target cells have the same rank s.