EdgePooling
¶
EdgePooling(
node_features_dim: int,
edge_score_method: Optional[Callable] = None,
dropout: float = 0.0,
add_to_edge_score: float = 0.5,
)
Bases: Module
The edge pooling operator from the "Towards Graph Pooling by Edge Contraction" and "Edge Contraction Pooling for Graph Neural Networks" papers.
Each edge gets a score from the concatenated features of its two nodes
(a linear layer, followed by edge_score_method, plus
add_to_edge_score). Edges are then contracted greedily by decreasing
score, skipping the edges that touch an already contracted node. The
features of a new node are the sum of the features of its merged nodes,
multiplied by the score of the contracted edge (1 for the nodes that were
not merged).
As in PyG, the greedy contraction runs on the host (numpy), and equal scores are ordered by edge index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
int
|
Size of the input node features. |
required |
edge_score_method
|
Optional[Callable]
|
Function |
None
|
dropout
|
float
|
Dropout probability of the raw edge scores. Default: |
0.0
|
add_to_edge_score
|
float
|
Value added to every edge score, so that the
features of merged nodes are not scaled down too much.
Default: |
0.5
|
Example:
import mlx.core as mx
from mlx_graphs.nn import EdgePooling
pool = EdgePooling(16)
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, batch_indices, unpool_info = pool(
edge_index, node_features, batch_indices
)
>>> x.shape
(3, 16)
x, edge_index, batch_indices = pool.unpool(x, unpool_info)
Methods:
| Name | Description |
|---|---|
compute_edge_score_softmax |
Normalizes the scores with a softmax over the incoming edges of |
compute_edge_score_tanh |
Normalizes the scores with a tanh. |
compute_edge_score_sigmoid |
Normalizes the scores with a sigmoid. |
__call__ |
Pools the graphs by contracting edges. |
unpool |
Unpools a pooled graph: every node gets the features of its |
Source code in mlx_graphs/nn/pooling/edge_pool.py
compute_edge_score_softmax
staticmethod
¶
compute_edge_score_softmax(
raw_edge_score: array, edge_index: array, num_nodes: int
) -> array
Normalizes the scores with a softmax over the incoming edges of each node.
Source code in mlx_graphs/nn/pooling/edge_pool.py
compute_edge_score_tanh
staticmethod
¶
compute_edge_score_tanh(
raw_edge_score: array,
edge_index: Optional[array] = None,
num_nodes: Optional[int] = None,
) -> array
Normalizes the scores with a tanh.
Source code in mlx_graphs/nn/pooling/edge_pool.py
compute_edge_score_sigmoid
staticmethod
¶
compute_edge_score_sigmoid(
raw_edge_score: array,
edge_index: Optional[array] = None,
num_nodes: Optional[int] = None,
) -> array
Normalizes the scores with a sigmoid.
Source code in mlx_graphs/nn/pooling/edge_pool.py
__call__
¶
__call__(
edge_index: array,
node_features: array,
batch_indices: Optional[array] = None,
) -> Tuple[array, array, array, UnpoolInfo]
Pools the graphs by contracting edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Edges of shape |
required |
node_features
|
array
|
Node features of shape |
required |
batch_indices
|
Optional[array]
|
Graph of each node, of shape |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
array
|
with the pooled graph (whose edges are coalesced, and include the |
array
|
self-loops of the contracted edges) and the information needed by |
UnpoolInfo
|
|
Source code in mlx_graphs/nn/pooling/edge_pool.py
unpool
¶
unpool(
node_features: array, unpool_info: UnpoolInfo
) -> Tuple[array, array, array]
Unpools a pooled graph: every node gets the features of its cluster, divided by the score of the contracted edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features
|
array
|
Features of the pooled nodes. |
required |
unpool_info
|
UnpoolInfo
|
The information returned by the pooling step. |
required |
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
array
|
unpooled graph. |
Source code in mlx_graphs/nn/pooling/edge_pool.py
from mlx_graphs.nn import EdgePooling · mlx_graphs/nn/pooling/edge_pool.py