RGCNLayer
- class RGCNLayer(num_relations: int, input_dim: int = 32, output_dim: int | None = None, use_bias: bool = True, activation: str | Module | None = None, activation_kwargs: Mapping[str, Any] | None = None, self_loop_dropout: float = 0.2, decomposition: str | Decomposition | None = None, decomposition_kwargs: Mapping[str, Any] | None = None)[source]
Bases:
Module
An RGCN layer from [schlichtkrull2018] updated to match the official implementation.
This layer uses separate decompositions for forward and backward edges (i.e., “normal” and implicitly created inverse relations), as well as a separate transformation for self-loops.
Ignoring dropouts, decomposition and normalization, it can be written as
\[y_i = \sigma( W^s x_i + \sum_{(e_j, r, e_i) \in \mathcal{T}} W^f_r x_j + \sum_{(e_i, r, e_j) \in \mathcal{T}} W^b_r x_j + b )\]where \(b, W^s, W^f_r, W^b_r\) are trainable weights. \(W^f_r, W^b_r\) are relation-specific, and commonly enmploy a weight-sharing mechanism, cf. Decomposition. \(\sigma\) is an activation function. The individual terms in both sums are typically weighted. This is implemented by EdgeWeighting. Moreover, RGCN employs an edge-dropout, however, this needs to be done outside of an individual layer, since the same edges are dropped across all layers. In contrast, the self-loop dropout is layer-specific.
Initialize the layer.
- Parameters:
input_dim (int) – >0 The input dimension.
num_relations (int) – The number of relations.
output_dim (int | None) – >0 The output dimension. If none is given, use the input dimension.
use_bias (bool) – Whether to use a trainable bias.
activation (Hint[nn.Module]) – The activation function to use. Defaults to None, i.e., the identity function serves as activation.
activation_kwargs (Mapping[str, Any] | None) – Additional keyword-based arguments passed to the activation function for instantiation.
self_loop_dropout (float) – 0 <= self_loop_dropout <= 1 The dropout to use for self-loops.
decomposition (Hint[Decomposition]) – The decomposition to use.
decomposition_kwargs (Mapping[str, Any] | None) – The keyword-based arguments passed to the decomposition for instantiation.
Note
2 resolvers are used in this function.
The parameter pair
(activation, activation_kwargs)
is used forclass_resolver.contrib.torch.activation_resolver
The parameter pair
(decomposition, decomposition_kwargs)
is used forpykeen.nn.message_passing.decomposition_resolver
An explanation of resolvers and how to use them is given in https://class-resolver.readthedocs.io/en/latest/.
Methods Summary
forward
(x, source, target, edge_type[, ...])Calculate enriched entity representations.
Methods Documentation
- forward(x: Tensor, source: Tensor, target: Tensor, edge_type: Tensor, edge_weights: Tensor | None = None) Tensor [source]
Calculate enriched entity representations.
- Parameters:
x (Tensor) – shape:
(num_entities, input_dim)
The input entity representations.source (Tensor) – shape:
(num_triples,)
The indices of the source entity per triple.target (Tensor) – shape:
(num_triples,)
The indices of the target entity per triple.edge_type (Tensor) – shape:
(num_triples,)
The relation type per triple.edge_weights (Tensor | None) – shape:
(num_triples,)
Scalar edge weights per triple.
- Returns:
shape:
(num_entities, output_dim)
Enriched entity representations.- Return type: