SRCPooling¶
The most general interface is SRCPooling, which is the
parent class for every pooling operator in tgp.
A base class for pooling layers based on the paper "Understanding Pooling in Graph Neural Networks" (Grattarola et al., TNNLS 2022). |
|
A base class for dense pooling layers that extends |
|
The pooling output of a model of class |
- class SRCPooling(selector: Select | None = None, reducer: Reduce | None = None, lifter: Lift | None = None, connector: Connect | None = None, cached: bool = False)[source]¶
A base class for pooling layers based on the paper “Understanding Pooling in Graph Neural Networks” (Grattarola et al., TNNLS 2022). Each pooler should inherit from this class.
SRCPoolingdecomposes a pooling layer into three components:Selectdefines how input nodes map to supernodes.Reducedefines how input node features are aggregated.Liftdefines how pooled node features are un-pooled.Connectdecides how the supernodes are connected to each other.
This class should return an object of type
PoolingOutput.- Parameters:
selector (
Select) – The node selection operator.reducer (
Reduce) – The node feature aggregation operator.lifter (
Lift) – The node feature un-pooling operator.connector (
Connect) – The edge connection operator.cached (bool, optional) – If set to
True, will cache theSelectoutput and theConnectoutput. (default:False)
- select(**kwargs) SelectOutput[source]¶
Calls the
Selectoperator.- Returns:
An object of type
SelectOutputcontaining the mapping from nodes to supernodes \(\mathbf{S} \in \mathbb{R}^{N \times K}\).
- reduce(**kwargs) Tensor[source]¶
Calls the
Reduceoperator.- Returns:
The pooled supernode features \(\mathbf{X}_{\text{pool}}\).
- lift(**kwargs) Tensor | SparseTensor[source]¶
Calls the
Liftoperator.- Returns:
The un-pooled node features \(\mathbf{X}_{\text{lift}} \approx \mathbf{X}\).
- connect(**kwargs) Tuple[Tensor | SparseTensor, Tensor | None][source]¶
Calls the
Connectoperator.- Returns:
The adjacency matrix of the coarse graph \(\mathbf{A}_{\text{pool}}\).
- preprocessing(x: Tensor, edge_index: Tensor | SparseTensor, **kwargs) Tuple[Tensor, Tensor | SparseTensor, Tensor | None][source]¶
Preprocess inputs, if needed.
- property is_trainable: bool¶
Returns
Trueif any parameter belonging to the pooler or any of its registered sub-modules is trainable.
- class DenseSRCPooling(selector: Select | None = None, reducer: Reduce | None = None, lifter: Lift | None = None, connector: Connect | None = None, cached: bool = False, adj_transpose: bool = False, batched: bool = True, sparse_output: bool = False, cache_preprocessing: bool = False)[source]¶
A base class for dense pooling layers that extends
SRCPooling.It provides a preprocessing function that transform a batch of graphs in sparse representation into a batch of dense graphs. When
batched=True, dense poolers accept either raw sparse inputs (which are converted internally) or already-dense padded tensors. In the latter case, an external input-node validity mask can be provided to mark real (non-padded) nodes; otherwise a full-ones mask is assumed.- Parameters:
selector (
Select) – The dense \(\texttt{select}\) operator.reducer (
Reduce) – The dense \(\texttt{reduce}\) operator.lifter (
Lift) – The dense \(\texttt{lift}\) operator.connector (
Connect) – The dense \(\texttt{connect}\) operator.cached (bool, optional) – If set to
True, will cache theSelectoutput and theConnectoutput. (default:False)cache_preprocessing (bool, optional) – If set to
True, will cache the dense adjacency produced bypreprocessing(). This should only be enabled when the same graph is reused across iterations (e.g., transductive single-graph tasks). (default:False)adj_transpose (bool, optional) – If
True, the preprocessing step and theDenseConnectoperation returns transposed adjacency matrices, so that they could be passed “as is” to the dense message-passing layers. (default:True)batched (bool, optional) – If
True, sparse inputs are converted to batched dense tensors internally. IfFalse, the pooler expects unbatched dense assignments without padding. (default:True)sparse_output (bool, optional) – If
True, the pooled outputs are returned as block-diagonal sparse representations. IfFalse, outputs are returned in batched dense form. (default:False)
- preprocessing(x: Tensor, edge_index: Tensor | SparseTensor, edge_weight: Tensor | None = None, batch: Tensor | None = None, max_num_nodes: int | None = None, batch_size: int | None = None, use_cache: bool = False, **kwargs) Tuple[Tensor, Tensor, Tensor][source]¶
Preprocess inputs for dense pooling methods.
Transform a batch of graphs in sparse representation into a batch of graphs with dense representation.
- Parameters:
x (Tensor) – The node features. A tensor of shape \([N, F]\), where \(N\) is the total number of nodes in the batch and \(F\) is the number of node features.
edge_index (Tensor) – The edge indices. A tensor of of shape \([2, E]\), where \(E\) is the number of edges in the batch.
edge_weight (Tensor, optional) – A vector of shape \([E]\) or \([E, 1]\) containing the weights of the edges. (default:
None)batch (Tensor, optional) – The batch vector \(\mathbf{b} \in {\{ 0, \ldots, B-1\}}^N\), which indicates to which graph in the batch each node belongs. (default:
None)max_num_nodes (int, optional) – The maximum number of nodes of a graph in the batch. (default:
None)batch_size (int, optional) – The number of graphs in the batch. (default:
None)use_cache (bool, optional) – If
True, it stores the preprocessed adjacency matrix inpreprocessing_cache. This is intended for static, single-graph inputs. (default:False)
- Returns:
A tuple
(x, adj, mask)wherexcontains batched node features of shape \([B, N_\text{max}, F]\),adjcontains batched dense adjacencies of shape \([B, N_\text{max}, N_\text{max}]\), andmaskmarks real versus padded nodes with shape \([B, N_\text{max}]\).- Return type:
- class PoolingOutput(x: Tensor | None = None, edge_index: Tensor | None = None, edge_weight: Tensor | None = None, batch: Tensor | None = None, so: SelectOutput | None = None, loss: Dict | None = None)[source]¶
The pooling output of a model of class
SRCPooling.- Parameters:
x (Tensor) – The pooled node features.
edge_index (Tensor) – The edge indices of the pooled graph.
edge_weight (Tensor, optional) – The edge features of the coarsened graph. (default:
None)batch (Tensor, optional) – The batch vector of the pooled nodes.
so (
SelectOutput) – The selection output. (default:None)mask – Derived from
so.out_maskwhensois set. This is a pooled-supernode validity mask of shape \([B, K]\) (or \([1, K]\) for single-graph dense assignments).loss (Optional[Dict], optional) – The loss dictionary. (default:
None)