CompGCNLayer

class CompGCNLayer(input_dim: int, output_dim: int | None = None, *, dropout: float = 0.0, use_bias: bool = True, use_relation_bias: bool = False, composition: str | ~pykeen.nn.compositions.CompositionModule | None = None, composition_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None, attention_heads: int = 4, attention_dropout: float = 0.1, activation: str | ~torch.nn.modules.module.Module | type[~torch.nn.modules.module.Module] | None = <class 'torch.nn.modules.linear.Identity'>, activation_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None, edge_weighting: str | type[~pykeen.nn.weighting.EdgeWeighting] | None = <class 'pykeen.nn.weighting.SymmetricEdgeWeighting'>, edge_weighting_kwargs: ~collections.abc.Mapping[str, ~typing.Any] | None = None)[source]

Bases: Module

A single CompGCN layer.

Initialize the module.

Parameters:
  • input_dim (int) – The input dimension.

  • output_dim (int | None) – The output dimension. If None, equals the input dimension.

  • dropout (float) – The dropout to use for forward and backward edges.

  • use_bias (bool) – # TODO: do we really need this? it comes before a mandatory batch norm layer Whether to use bias.

  • use_relation_bias (bool) – Whether to use a bias for the relation transformation.

  • composition (Hint[CompositionModule]) – The composition function.

  • composition_kwargs (OptionalKwargs) – Additional keyword based arguments passed to the composition.

  • attention_heads (int) – Number of attention heads when using the attention weighting

  • attention_dropout (float) – Dropout for the attention message weighting

  • activation (HintOrType[nn.Module]) – The activation to use.

  • activation_kwargs (Mapping[str, Any] | None) – Additional key-word based arguments passed to the activation.

  • edge_weighting (HintType[EdgeWeighting]) – A pre-instantiated EdgeWeighting, a class, or name to look up with class_resolver.

  • edge_weighting_kwargs (OptionalKwargs) –

    Additional keyword based arguments passed to the edge weighting. Note that the following keyword arguments for CompGCNLayer are automatically shuttled in here:

    • output_dim (or input_dim, if output dimension is not given) is passed to message_dim

    • attention_dropout is passed to dropout

    • attention_heads is passed to num_heads

Note

3 resolvers are used in this function.

An explanation of resolvers and how to use them is given in https://class-resolver.readthedocs.io/en/latest/.

Methods Summary

forward(x_e, x_r, edge_index, edge_type)

Update entity and relation representations.

message(x_e, x_r, edge_index, edge_type, weight)

Perform message passing.

reset_parameters()

Reset the model's parameters.

Methods Documentation

forward(x_e: Tensor, x_r: Tensor, edge_index: Tensor, edge_type: Tensor) tuple[Tensor, Tensor][source]

Update entity and relation representations.

\[X_E'[e] = \frac{1}{3} \left( X_E W_s + \left( \sum_{h,r,e \in T} \alpha(h, e) \phi(X_E[h], X_R[r]) W_f \right) + \left( \sum_{e,r,t \in T} \alpha(e, t) \phi(X_E[t], X_R[r^{-1}]) W_b \right) \right)\]
Parameters:
  • x_e (Tensor) – shape: (num_entities, input_dim) The entity representations.

  • x_r (Tensor) – shape: (2 * num_relations, input_dim) The relation representations (including inverse relations).

  • edge_index (Tensor) – shape: (2, num_edges) The edge index, pairs of source and target entity for each triple.

  • edge_type (Tensor) – shape (num_edges,) The edge type, i.e., relation ID, for each triple.

Returns:

shape: (num_entities, output_dim) / (2 * num_relations, output_dim) The updated entity and relation representations.

Return type:

tuple[Tensor, Tensor]

message(x_e: Tensor, x_r: Tensor, edge_index: Tensor, edge_type: Tensor, weight: Parameter) Tensor[source]

Perform message passing.

Parameters:
  • x_e (Tensor) – shape: (num_entities, input_dim) The entity representations.

  • x_r (Tensor) – shape: (2 * num_relations, input_dim) The relation representations (including inverse relations).

  • edge_index (Tensor) – shape: (2, num_edges) The edge index, pairs of source and target entity for each triple.

  • edge_type (Tensor) – shape (num_edges,) The edge type, i.e., relation ID, for each triple.

  • weight (Parameter) – The transformation weight.

Returns:

The updated entity representations.

Return type:

Tensor

reset_parameters()[source]

Reset the model’s parameters.