GeneralizedRelationalConv
¶
GeneralizedRelationalConv(
in_features_dim: int,
out_features_dim: int,
num_relations: int,
message_func: MessageFunctions = "distmult",
aggregate_func: AggregationFunctions = "add",
layer_norm: bool = True,
activation: str = "relu",
dependent: bool = False,
node_dim: int = 0,
**kwargs,
)
Bases: MessagePassing
Generalized relational convolution layer from Neural Bellman-Ford Networks: A General Graph Neural Network Framework for Link Prediction paper.
Adopted from the PyG version from here.
Part of the Neural Bellman-Ford networks (NBFNet) holding
state-of-the-art in KG completion.
Works with multi-relational graphs where edge types are stored in edge_labels.
The message function composes node and relation vectors in three possible ways.
The expected behavior is to work with "labeling trick" graphs where one node
in the graph is labeled with a query vector, while rest are zeros.
Message passing is then done separately for each data point in the batch.
The input shape is expected to be [batch_size, num_nodes, input_dim]
Alternatively, the layer can work as a standard relational conv with shapes [num_nodes, input_dim].
Note that this implementation materializes all edge messages and is O(E).
The complexity can be further reduced by adopting the O(V) rspmm C++ kernel
from the NBFNet-PyG repo to the MLX framework (not implemented here).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features_dim
|
int
|
input feature dimension (same for node and edge features) |
required |
out_features_dim
|
int
|
output node feature dimension |
required |
num_relations
|
int
|
number of unique relations in the graph |
required |
message_func
|
MessageFunctions
|
"transe" (sum), "distmult" (mult),
"rotate" (complex rotation). Default: |
'distmult'
|
aggregate_func
|
AggregationFunctions
|
"add", "mean", or "pna". Default: |
'add'
|
layer_norm
|
bool
|
whether to use layer norm
(often crucial to the performance). Default: |
True
|
activation
|
str
|
non-linearity. Default: |
'relu'
|
dependent
|
bool
|
whether to use separate relation embedding matrix
|
False
|
node_dim
|
int
|
for 3D batches, specified which dimension contains all nodes.
Default: |
0
|
Example:
import mlx.core as mx
from mlx_graphs.nn import GeneralizedRelationalConv
input_dim = 16
output_dim = 16
num_relations = 3
conv = GeneralizedRelationalConv(input_dim, output_dim, num_relations)
batch_size = 2
edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
edge_types = mx.array([0, 0, 1, 1, 2])
boundary = mx.random.uniform(0, 1, shape=(batch_size, 5, 16))
size = (boundary.shape[1], boundary.shape[1])
layer_input = boundary # initial node features which will be updated
h = conv(edge_index, layer_input, edge_types, boundary, size=size)
# optional: residual connection if input dim == output dim
h = h + layer_input
layer_input = h
# another conv type where relations are obtained from the additional
# query tensor
query = mx.random.uniform(0, 1, shape=(batch_size, 16))
conv2 = GeneralizedRelationalConv(
input_dim, output_dim, num_relations, dependent=True)
h = conv2(edge_index, layer_input, edge_types, boundary, query, size=size)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the forward pass of GeneralizedRelationalConv. |
Source code in mlx_graphs/nn/conv/rel_conv.py
__call__
¶
__call__(
edge_index: array,
node_features: array,
edge_type: array,
boundary: array,
query: Optional[array] = None,
size: Optional[tuple[int, int]] = None,
edge_weights: Optional[array] = None,
**kwargs,
) -> array
Computes the forward pass of GeneralizedRelationalConv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
Input edge index of shape |
required |
node_features
|
array
|
Input node features,
shape |
required |
edge_type
|
array
|
Input edge types of shape |
required |
boundary
|
array
|
Initial node feats |
required |
query
|
Optional[array]
|
Optional input node queries, shape |
None
|
size
|
Optional[tuple[int, int]]
|
a tuple encoding the size of the graph eg |
None
|
edge_weights
|
Optional[array]
|
Edge weights leveraged in message passing. Default: |
None
|
Returns:
| Type | Description |
|---|---|
array
|
The computed node embeddings |
Source code in mlx_graphs/nn/conv/rel_conv.py
from mlx_graphs.nn import GeneralizedRelationalConv · mlx_graphs/nn/conv/rel_conv.py