Aggregation
¶
Bases: Module
Base class of the aggregation operators, mirroring
torch_geometric.nn.aggr.Aggregation.
An aggregation reduces a set of elements (e.g. the messages received by
each node, or the nodes of each graph) to a single vector per set. The sets
are given by an index array mapping every element of x to its set,
and the reduction is always done along the first axis of x:
x = mx.random.normal((10, 64))
index = mx.array([0, 0, 1, 0, 2, 0, 2, 1, 0, 2])
out = aggr(x, index) # shape [3, 64]
index does not need to be sorted. Unlike PyG, compressed ptr
inputs and a dim argument are not supported: elements are always
stacked along axis 0, which is the mlx-graphs convention.
Aggregations can be used on their own (e.g. as a global readout with
index = batch_indices) or passed as aggr to any
MessagePassing layer.
Subclasses implement aggregate(), which receives a validated
index and dim_size.
Methods:
| Name | Description |
|---|---|
__call__ |
Aggregates the elements of |
aggregate |
Computes the aggregation. Implemented by subclasses. |
reduce |
Reduces |
__call__
¶
__call__(
x: array,
index: Optional[array] = None,
dim_size: Optional[int] = None,
**kwargs: Any,
) -> array
Aggregates the elements of x according to index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
Array of shape |
required |
index
|
Optional[array]
|
Array of shape |
None
|
dim_size
|
Optional[int]
|
Number of sets, i.e. size of the first axis of the
output. Defaults to |
None
|
**kwargs
|
Any
|
Additional arguments of the aggregation. |
{}
|
Returns:
| Type | Description |
|---|---|
array
|
An array of shape |
Source code in mlx_graphs/nn/aggr/base.py
aggregate
¶
Computes the aggregation. Implemented by subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
Array of shape |
required |
index
|
array
|
Array of shape |
required |
dim_size
|
int
|
Number of sets. |
required |
Source code in mlx_graphs/nn/aggr/base.py
reduce
¶
reduce(
x: array, index: array, dim_size: int, reduce: ScatterAggregations = "add"
) -> array
Reduces x with scatter().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
Array of shape |
required |
index
|
array
|
Array of shape |
required |
dim_size
|
int
|
Number of sets. |
required |
reduce
|
ScatterAggregations
|
Any reduction supported by
|
'add'
|
Source code in mlx_graphs/nn/aggr/base.py
from mlx_graphs.nn.aggr import Aggregation · mlx_graphs/nn/aggr/base.py