topk
¶
topk(
scores: array,
ratio: Optional[Union[float, int]],
batch_indices: array,
min_score: Optional[float] = None,
tol: float = 1e-07,
) -> array
Selects the nodes with the highest scores in each graph.
If min_score is None, keeps the \(k\) best nodes of each graph,
with \(k = \lceil \mathrm{ratio} \cdot N \rceil\) for a ratio smaller
than 1 and \(k = \mathrm{ratio}\) otherwise (all the nodes of graphs
with fewer than \(k\) nodes). Otherwise, keeps the nodes whose score is
larger than min_score, and the best node of each graph in all cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scores
|
array
|
Score of each node, of shape |
required |
ratio
|
Optional[Union[float, int]]
|
The pooling ratio, or the number of nodes to keep per graph.
Ignored if |
required |
batch_indices
|
array
|
Graph of each node, of shape |
required |
min_score
|
Optional[float]
|
Minimal score of the kept nodes. Default: |
None
|
tol
|
float
|
Tolerance on the best score of each graph, used with
|
1e-07
|
Returns:
| Type | Description |
|---|---|
array
|
The indices of the kept nodes. Without |
array
|
by graph, by decreasing score; with |
Example:
import mlx.core as mx
from mlx_graphs.nn import topk
scores = mx.array([0.1, 0.9, 0.5, 0.3, 0.7])
batch_indices = mx.array([0, 0, 0, 1, 1])
>>> topk(scores, 0.5, batch_indices)
array([1, 2, 4], dtype=int32)
Source code in mlx_graphs/nn/pooling/select.py
from mlx_graphs.nn import topk · mlx_graphs/nn/pooling/select.py