MemPooling
¶
MemPooling(
node_features_dim: int,
out_features_dim: int,
heads: int,
num_clusters: int,
tau: float = 1.0,
)
Bases: Module
Memory based pooling layer from the "Memory-Based Graph Networks" paper, which learns a coarsened graph representation based on soft cluster assignments.
where \(H\) denotes the number of heads, and \(K\) the number of
clusters (the keys \(\mathbf{k}^{(h)}_j\) are learned). The
auxiliary KL divergence loss of the paper is given by kl_loss().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
int
|
Size of the input node features. |
required |
out_features_dim
|
int
|
Size of the output (pooled) features. |
required |
heads
|
int
|
Number of heads \(H\). |
required |
num_clusters
|
int
|
Number of clusters \(K\) per head. |
required |
tau
|
float
|
The temperature \(\tau\). Default: |
1.0
|
Example:
import mlx.core as mx
from mlx_graphs.nn import MemPooling
pool = MemPooling(16, 32, heads=4, num_clusters=3)
node_features = mx.random.normal((7, 16))
batch_indices = mx.array([0, 0, 0, 1, 1, 1, 1])
x, S = pool(node_features, batch_indices)
>>> x.shape, S.shape
((2, 3, 32), (2, 4, 3))
loss = MemPooling.kl_loss(S)
Methods:
| Name | Description |
|---|---|
kl_loss |
The additional KL divergence-based loss. |
__call__ |
Pools the graphs. |
Source code in mlx_graphs/nn/pooling/mem_pool.py
kl_loss
staticmethod
¶
The additional KL divergence-based loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
S
|
array
|
The soft assignments of shape |
required |
Returns:
| Type | Description |
|---|---|
array
|
The loss, averaged over the graphs of the batch. |
Source code in mlx_graphs/nn/pooling/mem_pool.py
__call__
¶
__call__(
node_features: array,
batch_indices: Optional[array] = None,
mask: Optional[array] = None,
max_num_nodes: Optional[int] = None,
batch_size: Optional[int] = None,
) -> Tuple[array, array]
Pools the graphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features
|
array
|
Node features, either of shape |
required |
batch_indices
|
Optional[array]
|
Graph of each node, of shape |
None
|
mask
|
Optional[array]
|
Boolean mask of the real nodes, of shape |
None
|
max_num_nodes
|
Optional[int]
|
Number of nodes per graph of the dense batch built
from 2D |
None
|
batch_size
|
Optional[int]
|
Number of graphs, for 2D |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
array
|
|
Tuple[array, array]
|
assignments |
Tuple[array, array]
|
num_clusters]`. |
Source code in mlx_graphs/nn/pooling/mem_pool.py
from mlx_graphs.nn import MemPooling · mlx_graphs/nn/pooling/mem_pool.py