TopKPooling
¶
TopKPooling(
node_features_dim: int,
ratio: Union[int, float] = 0.5,
min_score: Optional[float] = None,
multiplier: float = 1.0,
nonlinearity: Union[str, Callable] = "tanh",
)
Bases: Module
\(\mathrm{top}_k\) pooling operator from the "Graph U-Nets", "Towards Sparse Hierarchical Graph Classifiers" and "Understanding Attention and Generalization in Graph Neural Networks" papers.
If min_score \(\tilde{\alpha}\) is None, computes:
If min_score \(\tilde{\alpha}\) is a value in \([0, 1]\),
computes:
where nodes are dropped based on a learnable projection score \(\mathbf{p}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
int
|
Size of the input node features. |
required |
ratio
|
Union[int, float]
|
The graph pooling ratio, used to compute
\(k = \lceil \mathrm{ratio} \cdot N \rceil\) for a float, or
\(k\) itself for an integer. Ignored if |
0.5
|
min_score
|
Optional[float]
|
Minimal node score \(\tilde{\alpha}\) of the kept nodes.
When given, |
None
|
multiplier
|
float
|
Coefficient by which the features are multiplied after
pooling. This can be useful for large graphs and when |
1.0
|
nonlinearity
|
Union[str, Callable]
|
The non-linearity \(\sigma\), as a callable or the
name of a function of |
'tanh'
|
Example:
import mlx.core as mx
from mlx_graphs.nn import TopKPooling
pool = TopKPooling(16, ratio=0.5)
edge_index = mx.array([[0, 1, 2, 3, 4], [1, 2, 0, 4, 3]])
node_features = mx.random.normal((5, 16))
batch_indices = mx.array([0, 0, 0, 1, 1])
x, edge_index, edge_features, batch_indices, perm, score = pool(
edge_index, node_features, batch_indices=batch_indices
)
>>> x.shape
(3, 16)
Methods:
| Name | Description |
|---|---|
__call__ |
Pools the nodes with the best scores. |
Source code in mlx_graphs/nn/pooling/topk_pool.py
__call__
¶
__call__(
edge_index: array,
node_features: array,
edge_features: Optional[array] = None,
batch_indices: Optional[array] = None,
attn: Optional[array] = None,
) -> tuple[array, array, Optional[array], array, array, array]
Pools the nodes with the best scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Edges of shape |
required |
node_features
|
array
|
Node features of shape |
required |
edge_features
|
Optional[array]
|
Optional edge features of shape |
None
|
batch_indices
|
Optional[array]
|
Graph of each node, of shape |
None
|
attn
|
Optional[array]
|
Optional node-level array used to compute the scores instead
of |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple `(node_features, edge_index, edge_features, batch_indices, |
array
|
perm, score) |
Optional[array]
|
kept nodes and their |
Source code in mlx_graphs/nn/pooling/topk_pool.py
from mlx_graphs.nn import TopKPooling · mlx_graphs/nn/pooling/topk_pool.py