ASAPooling
¶
ASAPooling(
node_features_dim: int,
ratio: Union[float, int] = 0.5,
GNN: Optional[Callable[..., Module]] = None,
dropout: float = 0.0,
negative_slope: float = 0.2,
add_self_loops: bool = False,
**kwargs,
)
Bases: Module
The Adaptive Structure Aware Pooling operator from the "ASAP: Adaptive Structure Aware Pooling for Learning Hierarchical Graph Representations" paper.
Every node \(i\) defines a cluster made of its 1-hop neighbourhood
(and itself). The cluster representation is an attention-weighted sum of
the features of its members, where the attention (a softmax over the
members) compares each member with a master query (the feature-wise
maximum of the members). The clusters with the best fitness (a sigmoid of
an LEConv layer) are kept, scaled by their
fitness, and the pooled adjacency is \(\mathbf{S}^{\top} \mathbf{A}
\mathbf{S}\), where \(\mathbf{S}\) holds the attention scores of the
kept clusters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
int
|
Size of the input node features. |
required |
ratio
|
Union[float, int]
|
The graph pooling ratio, used to compute
\(k = \lceil \mathrm{ratio} \cdot N \rceil\) for a float, or
\(k\) itself for an integer. Default: |
0.5
|
GNN
|
Optional[Callable[..., Module]]
|
Optional class of a graph neural network layer computing the
features used by the attention, created as
|
None
|
dropout
|
float
|
Dropout probability of the attention scores.
Default: |
0.0
|
negative_slope
|
float
|
Negative slope of the leaky ReLU of the attention.
Default: |
0.2
|
add_self_loops
|
bool
|
Whether to add a self-loop to every pooled node (the
pooled self-loops are removed otherwise). Default: |
False
|
**kwargs
|
Additional arguments of |
{}
|
Example:
import mlx.core as mx
from mlx_graphs.nn import ASAPooling
pool = ASAPooling(16, ratio=0.5)
edge_index = mx.array([[0, 1, 1, 2, 3, 4], [1, 0, 2, 1, 4, 3]])
node_features = mx.random.normal((5, 16))
batch_indices = mx.array([0, 0, 0, 1, 1])
x, edge_index, edge_weights, batch_indices, perm = pool(
edge_index, node_features, batch_indices=batch_indices
)
>>> x.shape
(3, 16)
Methods:
| Name | Description |
|---|---|
__call__ |
Pools the graphs. |
Source code in mlx_graphs/nn/pooling/asap.py
__call__
¶
__call__(
edge_index: array,
node_features: array,
edge_weights: Optional[array] = None,
batch_indices: Optional[array] = None,
) -> Tuple[array, array, Optional[array], array, array]
Pools the graphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Edges of shape |
required |
node_features
|
array
|
Node features of shape |
required |
edge_weights
|
Optional[array]
|
Optional weight of each edge, of shape
|
None
|
batch_indices
|
Optional[array]
|
Graph of each node, of shape |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple `(node_features, edge_index, edge_weights, batch_indices, |
array
|
perm) |
Optional[array]
|
was not given) and the indices |
Source code in mlx_graphs/nn/pooling/asap.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
from mlx_graphs.nn import ASAPooling · mlx_graphs/nn/pooling/asap.py