Reduce¶
The most general interface is Reduce, which is the
parent class for every \(\texttt{reduce}\) operator in tgp.
For graph-level aggregation, AggrReduce and
GlobalReduce rely on PyG’s
torch_geometric.nn.aggr.Aggregation modules. The helper
function get_aggr() lets you obtain these aggregators
by string alias, mirroring how tgp.poolers.get_pooler()
instantiates poolers by alias.
from tgp.reduce import get_aggr, AggrReduce, GlobalReduce
# Aggregator instance from alias
aggr = get_aggr("mean")
reducer = AggrReduce(aggr)
# Graph-level readout with GlobalReduce
readout_layer = GlobalReduce(
reduce_op="set2set", in_channels=64, processing_steps=3
)
x_graph = readout_layer(x, batch=batch)
Aggregator aliases¶
The table below lists the supported aliases for get_aggr() and
the corresponding PyG aggregation classes:
Alias |
PyG aggregation class |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Reduce operator that wraps a PyG |
|
A template class for implementing the \(\texttt{reduce}\) operator. |
|
The basic \(\texttt{reduce}\) operator that computes \(\mathbf{S}^\top \mathbf{X}\). |
|
The \(\texttt{reduce}\) operator for EigenPooling. |
|
Graph-level readout as a module wrapping |
- class AggrReduce(aggr)[source]¶
Reduce operator that wraps a PyG
torch_geometric.nn.aggr.Aggregation.Aggregates node features within each supernode using the given aggregation module. Supports sparse assignment matrices and graph-level readout mode (
so=None). DenseSelectOutputassignments are not supported: useBaseReducefor dense/soft reductions.In
SelectOutput, pooled-node indices are namedcluster_indexfor historical reasons; here this is equivalent to “supernode index”.so=Noneis supported for graph-level readout: all nodes are assigned to one supernode per graph (usingbatchas supernode index), or to a single supernode whenbatchisNone.- Parameters:
aggr – A PyG Aggregation instance (e.g.
torch_geometric.nn.aggr.SumAggregation,torch_geometric.nn.aggr.MeanAggregation).
- forward(x: Tensor, so: SelectOutput | None = None, *, batch: Tensor | None = None, size: int | None = None, **kwargs) Tuple[Tensor, Tensor | None][source]¶
Aggregate node features according to the supernode assignments.
- Parameters:
x (Tensor) – Node features of shape \([N, F]\) or \([B, N, F]\).
so (SelectOutput, optional) – Select output containing assignment information. If
None, performs graph-level readout usingbatch.batch (Tensor, optional) – Batch vector assigning each node to a graph.
size (int, optional) – Expected number of pooled nodes (readout groups). If
None, inferred fromsoorbatch.
- Returns:
A pair
(x_pool, batch_pool)with pooled features and pooled batch indices.- Return type:
- class Reduce(*args, **kwargs)[source]¶
A template class for implementing the \(\texttt{reduce}\) operator.
- static reduce_batch(select_output: SelectOutput, batch: Tensor | None) Tensor | None[source]¶
Computes the batch vector of the coarsened graph.
- forward(x: Tensor, so: SelectOutput, *, batch: Tensor | None = None, **kwargs) Tuple[Tensor, Tensor | None][source]¶
Forward pass.
- Parameters:
x (Tensor) – The node feature matrix. For a sparse pooler,
xhas shape \([N, F]\), where \(N\) is the number of nodes in the batch and \(F\) is the number of node features. For a dense pooler,xhas shape \([B, N, F]\), where \(B\) is the batch size.so (SelectOutput) – The output of the \(\texttt{select}\) operator.
batch (torch.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)
- Returns:
The pooled features \(\mathbf{X}_{pool}\) of the supernodes.
- Return type:
- class BaseReduce[source]¶
The basic \(\texttt{reduce}\) operator that computes \(\mathbf{S}^\top \mathbf{X}\).
For sparse assignment \(\mathbf{S}\), this is implemented as a sum over nodes in each cluster (with optional weighting by
so.s.values()). For dense assignment, it is a matrix multiply \(\mathbf{S}^\top \mathbf{X}\).For dense multi-graph batches (dense \([N, K]\) with a batch vector), each graph is processed separately (unbatch then per-graph matmul) for memory efficiency when using unbatched dense poolers (
batched=False).For dense unbatched assignments \([N, K]\) with multi-graph batches,
return_batched=Truereturns \([B, K, F]\); otherwise \([B \cdot K, F]\). For dense batched assignments \([B, N, K]\), output is always \([B, K, F]\).- forward(x: Tensor, so: SelectOutput, *, batch: Tensor | None = None, return_batched: bool = False, **kwargs) Tuple[Tensor, Tensor | None][source]¶
Forward pass computing \(\mathbf{S}^\top \mathbf{X}\).
- Parameters:
x (Tensor) – The node feature matrix. For a sparse pooler,
xhas shape \([N, F]\), where \(N\) is the number of nodes in the batch and \(F\) is the number of node features. For a dense pooler,xhas shape \([B, N, F]\), where \(B\) is the batch size.so (SelectOutput) – The output of the \(\texttt{select}\) operator.
batch (torch.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)return_batched (bool, optional) – For dense unbatched \([N, K]\) assignments with multi-graph batches, controls output shape:
Truegives \([B, K, F]\),Falsegives \([B \cdot K, F]\). For single-graph \([N, K]\),Truewraps output as \([1, K, F]\). (default:False)
- class EigenPoolReduce(num_modes: int = 5, reduce_op: str = 'sum')[source]¶
The \(\texttt{reduce}\) operator for EigenPooling.
It uses the pooling matrix \(\boldsymbol{\Theta}\) computed by
EigenPoolSelectand stored inso.theta. For each graph:\[\mathbf{X}_{\text{pool,raw}} = \boldsymbol{\Theta}^{\top}\mathbf{X},\]then \(\mathbf{X}_{\text{pool,raw}}\) is reshaped from mode-major layout \([H \cdot K, F]\) to \([K, H \cdot F]\).
return_batchedis only used when returning multi-graph results (stack vs concatenate). EigenPool uses 2D inputs with a batch vector; there is no separate dense path.- Parameters:
num_modes (int, optional) – Number of eigenvector modes \(H\). Kept for API symmetry with the EigenPool components. (default:
5)reduce_op (ReduceType, optional) – Kept for API compatibility with
Reduce. (default:"sum")
- forward(x: Tensor, so: SelectOutput, *, batch: Tensor | None = None, edge_index: Tensor | None = None, edge_weight: Tensor | None = None, return_batched: bool = False, **kwargs) Tuple[Tensor, Tensor | None][source]¶
Forward pass.
- Parameters:
x (Tensor) – Node feature matrix \(\mathbf{X}\) of shape \([N, F]\).
so (SelectOutput) – Output of the \(\texttt{select}\) operator with dense assignment matrix
so.sand pooling matrixso.theta.batch (Tensor, optional) – Batch vector for sparse multi-graph inputs. If
None, this method usesso.batchwhen available. (default:None)edge_index (Tensor, optional) – Unused by EigenPooling. (default:
None)edge_weight (Tensor, optional) – Unused by EigenPooling. (default:
None)return_batched (bool, optional) – Only used for multi-graph batches. If
True, returns \([B, K, H \cdot F]\); otherwise concatenated \([B \cdot K, H \cdot F]\). For single graphs, ifTruereturns \([1, K, H \cdot F]\). (default:False)
- Returns:
Pooled features and pooled batch vector.
- Return type:
(~torch.Tensor, ~torch.Tensor or
None)
- class GlobalReduce(reduce_op: str | object = 'sum', **aggr_kwargs)[source]¶
Graph-level readout as a module wrapping
AggrReduce.This module aggregates node features to one vector per graph, supporting both sparse inputs
[N, F]with an optionalbatchvector and dense inputs[B, N, F]with an optional booleanmaskof shape[B, N].- Parameters:
reduce_op – Aggregation to use: a string alias (e.g.
\"sum\",\"mean\",\"max\",\"min\",\"lstm\",\"set2set\") or a PyGtorch_geometric.nn.aggr.Aggregationinstance. Strings are resolved viaget_aggr().**aggr_kwargs – Passed to
get_aggr()whenreduce_opis a string (e.g.in_channels,out_channels,processing_steps).
- forward(x: Tensor, batch: Tensor | None = None, size: int | None = None, mask: Tensor | None = None) Tensor[source]¶
Aggregate node features to one vector per graph.
Infers sparse vs dense from
x.ndim: 2D[N, F]is sparse (usebatchfor grouping); 3D[B, N, F]is dense (reduce over the node dimension). Nodes must be on the second-to-last dimension.- Parameters:
x – Node features. Shape
[N, F](sparse) or[B, N, F](dense).batch – Batch vector for sparse
x, shape[N]. Ignored for dense.size – Number of graphs for sparse readout when
batchis provided. Passingsizewith sparsexandbatch=NoneraisesValueError.mask – Input-node validity mask for batched (dense)
xonly, shape[B, N]. Passingmaskwith sparsexor with a mismatched shape raisesValueError.
- Returns:
Tensor of shape
[B, F](or[1, F]for single graph sparse).
- get_aggr(alias: str, **kwargs: Any) Any[source]¶
Return a PyG
torch_geometric.nn.aggr.Aggregationinstance by alias.This is the aggregation analogue of
tgp.poolers.get_pooler(): instead of instantiating poolers by alias,get_aggr()instantiates PyG aggregation modules. Use it withAggrReduceorGlobalReducewhen you prefer to configure aggregation via a string name.- Parameters:
alias – Name of the aggregator (e.g.
"sum","mean","lstm","set2set"). Case-insensitive; dashes are normalized to underscores. The full list of supported aliases is documented intgp.reduce(see the Aggregator aliases section).**kwargs – Passed to the aggregator constructor. Parametrized aggregators typically need
in_channels,out_channels, and/orprocessing_steps(e.g. for Set2Set, LSTM). Any extra keyword arguments that are not accepted by the underlying PyG class are silently dropped.
- Returns:
An instance of the requested PyG Aggregation.
- Raises:
ImportError – If
torch_geometric.nn.aggris not available.ValueError – If
aliasis not recognized.
Example
>>> from tgp.reduce import get_aggr, AggrReduce, GlobalReduce >>> # Inside a pooling layer >>> reducer = AggrReduce(get_aggr("mean")) >>> # For graph-level readout with Set2Set >>> readout_layer = GlobalReduce( ... reduce_op="set2set", in_channels=64, processing_steps=3 ... ) >>> x_graph = readout_layer(x, batch=batch)