MessagePassing
¶
MessagePassing(
aggr: Union[ScatterAggregations, List[Union[str, Any]], Any] = "add",
aggr_kwargs: Optional[Dict[str, Any]] = None,
)
Bases: Module
Base class for creating Message Passing Neural Networks (MPNNs) [1].
Inherit this class to build arbitrary GNN models based on the message passing paradigm. This implementation is inspired from PyTorch Geometric [2].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggr
|
Union[ScatterAggregations, List[Union[str, Any]], Any]
|
Aggregation strategy used to aggregate messages. Either:
Default: |
'add'
|
aggr_kwargs
|
Optional[Dict[str, Any]]
|
Arguments used to build the aggregation module from a
name or a list (e.g. |
None
|
Methods:
| Name | Description |
|---|---|
propagate |
Computes messages from neighbors, aggregates them and updates |
message_and_aggregate |
Fused |
message |
Computes messages between connected nodes. |
aggregate |
Aggregates the messages using the |
update_nodes |
Updates the final embeddings given the aggregated messages. |
Source code in mlx_graphs/nn/message_passing.py
propagate
¶
propagate(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
message_kwargs: Optional[Dict] = None,
aggregate_kwargs: Optional[Dict] = None,
update_kwargs: Optional[Dict] = None,
) -> array
Computes messages from neighbors, aggregates them and updates the final node embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Graph representation of shape |
required |
node_features
|
Union[array, Tuple[array, array]]
|
Input node features/embeddings.
Can be either an array or a tuple of arrays, for distinct src and dst
node features. With a tuple |
required |
message_kwargs
|
Optional[Dict]
|
Arguments to pass to the |
None
|
aggregate_kwargs
|
Optional[Dict]
|
Arguments to pass to the |
None
|
update_kwargs
|
Optional[Dict]
|
Arguments to pass to the |
None
|
Source code in mlx_graphs/nn/message_passing.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
message_and_aggregate
¶
message_and_aggregate(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
edge_weights: Optional[array] = None,
**kwargs,
) -> array
Fused message + aggregate as one sparse x dense matmul.
Computes, for every node i,
aggr_{j -> i} edge_weights[j -> i] * x_j (aggr = add, mean,
max or min, per feature; 0 for nodes without neighbours), the same
result as the
default message() followed by aggregate(), without
building the per-edge message array. Used by layers decorated with
fused_message_passing(); override it to provide a custom fused
implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Graph representation of shape |
required |
node_features
|
Union[array, Tuple[array, array]]
|
Node features, or a |
required |
edge_weights
|
Optional[array]
|
Optional weights: one scalar per edge, or, for
multi-head features of shape |
None
|
Source code in mlx_graphs/nn/message_passing.py
message
¶
Computes messages between connected nodes.
By default, returns the features of source nodes.
Optional edge_weights can be directly integrated in kwargs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_features
|
array
|
Source node embeddings |
required |
dst_features
|
array
|
Destination node embeddings |
required |
edge_weights
|
Array of scalars with shape (num_edges,) or (num_edges, 1)
used to weigh neighbor features during aggregation. For
multi-head source features of shape (num_edges, heads, channels),
an array of shape (num_edges, heads) weighs each head separately.
Default: |
required | |
**kwargs
|
Optional args to compute messages |
{}
|
Source code in mlx_graphs/nn/message_passing.py
aggregate
¶
Aggregates the messages using the self.aggr strategy, or
self.aggr_module when aggr is a list or an aggregation module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
array
|
Computed messages |
required |
indices
|
array
|
Indices representing the nodes that receive messages |
required |
**kwargs
|
Optional args to aggregate messages |
{}
|
Source code in mlx_graphs/nn/message_passing.py
update_nodes
¶
Updates the final embeddings given the aggregated messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregated
|
array
|
aggregated messages |
required |
**kwargs
|
optional args to update messages |
{}
|
Source code in mlx_graphs/nn/message_passing.py
from mlx_graphs.nn import MessagePassing · mlx_graphs/nn/message_passing.py