VirtualNode
¶
Bases: BaseTransform
Appends a virtual node connected to all the other nodes, as described
in "Neural Message Passing for Quantum Chemistry", as PyG's VirtualNode.
The virtual node gets the id num_nodes. An edge from every node to the
virtual node and an edge from the virtual node to every node are
appended, in this order. The type of each edge is stored in
edge_type: existing edges keep their edge_type (0 if there was
none), and with \(t\) the largest existing type, edges to the virtual
node get type \(t + 1\) and edges from it type \(t + 2\).
Every per-node attribute (e.g. node_features) gets a row of zeros for
the virtual node, and every per-edge attribute (e.g. edge_features)
gets rows of zeros for the new edges, except edge_weights which is
extended with ones.
Example:
graph = GraphData(
edge_index=mx.array([[0, 1], [1, 2]]),
node_features=mx.ones((3, 2)),
)
graph = VirtualNode()(graph)
graph.edge_index
# mx.array([[0, 1, 0, 1, 2, 3, 3, 3], [1, 2, 3, 3, 3, 0, 1, 2]])
graph.edge_type
# mx.array([0, 0, 1, 1, 1, 2, 2, 2])
graph.node_features.shape
# (4, 2)
Methods:
| Name | Description |
|---|---|
process |
Adds the virtual node to |
process
¶
Adds the virtual node to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GraphData
|
The graph to transform. |
required |
Returns:
| Type | Description |
|---|---|
GraphData
|
The graph with the virtual node. |
Source code in mlx_graphs/transforms/topology.py
from mlx_graphs.transforms import VirtualNode · mlx_graphs/transforms/topology.py