class_docstr: Entity representations enriched by R-GCN.
  
  The GCN employed by the entity encoder is adapted to include typed edges.
  The forward pass of the GCN is defined by:
  
   .. math::
  
      \textbf{e}_{i}^{l+1} = \sigma \left( \sum_{r \in \mathcal{R}}\sum_{j\in \mathcal{N}_{i}^{r}}
      \frac{1}{c_{i,r}} \textbf{W}_{r}^{l} \textbf{e}_{j}^{l} + \textbf{W}_{0}^{l} \textbf{e}_{i}^{l}\right)
  
  where $\mathcal{N}_{i}^{r}$ is the set of neighbors of node $i$ that are connected to
  $i$ by relation $r$, $c_{i,r}$ is a fixed normalization constant (but it can also be introduced as an additional
  parameter), and $\textbf{W}_{r}^{l} \in \mathbb{R}^{d^{(l)} \times d^{(l)}}$ and
  $\textbf{W}_{0}^{l} \in \mathbb{R}^{d^{(l)} \times d^{(l)}}$ are weight matrices of the `l`-th layer of the
  R-GCN.
  
  The encoder aggregates for each node $e_i$ the latent representations of its neighbors and its
  own latent representation $e_{i}^{l}$ into a new latent representation $e_{i}^{l+1}$.
  In contrast to standard GCN, R-GCN defines relation specific transformations
  $\textbf{W}_{r}^{l}$ which depend on the type and direction of an edge.
  
  Since having one matrix for each relation introduces a large number of additional parameters, the authors instead
  propose to use a decomposition, cf. :class:`pykeen.nn.message_passing.Decomposition`.
  ---
  name: R-GCN
  citation:
      author: Schlichtkrull
      year: 2018
      link: https://arxiv.org/pdf/1703.06103
      github: https://github.com/MichSchli/RelationPrediction
class_docstr_tok: ["Entity","representations","enriched","by","R-GCN",".","The","GCN","employed","by","the","entity","encoder","is","adapted","to","include","typed","edges",".","The","forward","pass","of","the","GCN","is","defined","by",":","..","math",":",":","\\textbf","{","e","}","_","{","i","}","^","{","l+1","}","=","\\sigma","\\left","(","\\sum_","{","r","\\in","\\mathcal","{","R","}","}","\\sum_","{","j\\in","\\mathcal","{","N","}","_","{","i","}","^","{","r","}","}","\\frac","{","1","}","{","c_","{","i",",","r","}","}","\\textbf","{","W","}","_","{","r","}","^","{","l","}","\\textbf","{","e","}","_","{","j","}","^","{","l","}","+","\\textbf","{","W","}","_","{","0","}","^","{","l","}","\\textbf","{","e","}","_","{","i","}","^","{","l","}","\\right",")","where","$","\\mathcal","{","N","}","_","{","i","}","^","{","r","}","$","is","the","set","of","neighbors","of","node","$","i","$","that","are","connected","to","$","i","$","by","relation","$","r","$",",","$","c_","{","i",",","r","}","$","is","a","fixed","normalization","constant","(","but","it","can","also","be","introduced","as","an","additional","parameter",")",",","and","$","\\textbf","{","W","}","_","{","r","}","^","{","l","}","\\in","\\mathbb","{","R","}","^","{","d^","{","(","l",")","}","\\times","d^","{","(","l",")","}","}","$","and","$","\\textbf","{","W","}","_","{","0","}","^","{","l","}","\\in","\\mathbb","{","R","}","^","{","d^","{","(","l",")","}","\\times","d^","{","(","l",")","}","}","$","are","weight","matrices","of","the","`","l","`","-th","layer","of","the","R-GCN",".","The","encoder","aggregates","for","each","node","$","e_i","$","the","latent","representations","of","its","neighbors","and","its","own","latent","representation","$","e_","{","i","}","^","{","l","}","$","into","a","new","latent","representation","$","e_","{","i","}","^","{","l+1","}","$",".","In","contrast","to","standard","GCN",",","R-GCN","defines","relation","specific","transformations","$","\\textbf","{","W","}","_","{","r","}","^","{","l","}","$","which","depend","on","the","type","and","direction","of","an","edge",".","Since","having","one","matrix","for","each","relation","introduces","a","large","number","of","additional","parameters",",","the","authors","instead","propose","to","use","a","decomposition",",","cf",".",":","class",":","`","pykeen.nn.message_passing.Decomposition","`",".","--","-","name",":","R-GCN","citation",":","author",":","Schlichtkrull","year",":","2018","link",":","https",":","\/\/arxiv.org\/pdf\/1703.06103","github",":","https",":","\/\/github.com\/MichSchli\/RelationPrediction"]
docstr: Instantiate the R-GCN encoder.
  
  :param triples_factory:
      The triples factory holding the training triples used for message passing.
  :param max_id:
      The maximum number of IDs. could either be None (the default), or match the triples factory's number of
      entities.
  :param shape:
      the shape information. If None, will propagate the shape information of the base entity representations.
  :param entity_representations:
      the base entity representations (or a hint for them)
  :param entity_representations_kwargs:
      additional keyword-based parameters for the base entity representations
  :param num_layers:
      The number of layers.
  :param use_bias:
      Whether to use a bias.
  :param activation:
      The activation.
  :param activation_kwargs:
      Additional keyword based arguments passed if the activation is not pre-instantiated. Ignored otherwise.
  :param edge_dropout:
      The edge dropout to use. Does not apply to self-loops.
  :param self_loop_dropout:
      The self-loop dropout to use.
  :param edge_weighting:
      The edge weighting mechanism.
  :param decomposition:
      The decomposition, cf. :class:`pykeen.nn.message_passing.Decomposition`.
  :param decomposition_kwargs:
      Additional keyword based arguments passed to the decomposition upon instantiation.
  :param kwargs:
      additional keyword-based parameters passed to :meth:`Representation.__init__`
  :param cache:
      whether to cache representations
  
  :raises ValueError: If the triples factory creates inverse triples.
code: def __init__(
      self,
      triples_factory: CoreTriplesFactory,
      max_id: Optional[int] = None,
      shape: Optional[Sequence[int]] = None,
      entity_representations: HintOrType[Representation] = None,
      entity_representations_kwargs: OptionalKwargs = None,
      num_layers: int = 2,
      use_bias: bool = True,
      activation: Hint[nn.Module] = None,
      activation_kwargs: Optional[Mapping[str, Any]] = None,
      edge_dropout: float = 0.4,
      self_loop_dropout: float = 0.2,
      edge_weighting: Hint[EdgeWeighting] = None,
      decomposition: Hint[Decomposition] = None,
      decomposition_kwargs: Optional[Mapping[str, Any]] = None,
      cache: bool = True,
      **kwargs,
  ):
      """Instantiate the R-GCN encoder.
  
      :param triples_factory:
          The triples factory holding the training triples used for message passing.
      :param max_id:
          The maximum number of IDs. could either be None (the default), or match the triples factory's number of
          entities.
      :param shape:
          the shape information. If None, will propagate the shape information of the base entity representations.
      :param entity_representations:
          the base entity representations (or a hint for them)
      :param entity_representations_kwargs:
          additional keyword-based parameters for the base entity representations
      :param num_layers:
          The number of layers.
      :param use_bias:
          Whether to use a bias.
      :param activation:
          The activation.
      :param activation_kwargs:
          Additional keyword based arguments passed if the activation is not pre-instantiated. Ignored otherwise.
      :param edge_dropout:
          The edge dropout to use. Does not apply to self-loops.
      :param self_loop_dropout:
          The self-loop dropout to use.
      :param edge_weighting:
          The edge weighting mechanism.
      :param decomposition:
          The decomposition, cf. :class:`pykeen.nn.message_passing.Decomposition`.
      :param decomposition_kwargs:
          Additional keyword based arguments passed to the decomposition upon instantiation.
      :param kwargs:
          additional keyword-based parameters passed to :meth:`Representation.__init__`
      :param cache:
          whether to cache representations
  
      :raises ValueError: If the triples factory creates inverse triples.
      """
      # input validation
      if max_id and max_id != triples_factory.num_entities:
          raise ValueError(
              f"max_id={max_id} differs from triples_factory.num_entities={triples_factory.num_entities}"
          )
      if triples_factory.create_inverse_triples:
          raise ValueError(
              "RGCN internally creates inverse triples. It thus expects a triples factory without them.",
          )
  
      # has to be imported now to avoid cyclic imports
      from . import representation_resolver
  
      base = representation_resolver.make(
          entity_representations,
          max_id=triples_factory.num_entities,
          pos_kwargs=entity_representations_kwargs,
      )
      if len(base.shape) > 1:
          raise ValueError(
              f"{self.__class__.__name__} requires vector base entity representations."
          )
      max_id = max_id or triples_factory.num_entities
      if max_id != base.max_id:
          raise ValueError(
              f"Inconsistent max_id={max_id} vs. base.max_id={base.max_id}"
          )
      shape = ShapeError.verify(shape=base.shape, reference=shape)
      super().__init__(max_id=max_id, shape=shape, **kwargs)
  
      # has to be assigned after call to nn.Module init
      self.entity_embeddings = base
  
      # Resolve edge weighting
      self.edge_weighting = edge_weight_resolver.make(
          query=edge_weighting
      )
  
      # dropout
      self.edge_dropout = edge_dropout
      self_loop_dropout = self_loop_dropout or edge_dropout
  
      # Save graph using buffers, such that the tensors are moved together with the model
      h, r, t = triples_factory.mapped_triples.t()
      self.register_buffer("sources", h)
      self.register_buffer("targets", t)
      self.register_buffer("edge_types", r)
  
      dim = base.shape[0]
      self.layers = nn.ModuleList(
          RGCNLayer(
              input_dim=dim,
              num_relations=triples_factory.num_relations,
              output_dim=dim,
              use_bias=use_bias,
              # no activation on last layer
              # cf. https://github.com/MichSchli/RelationPrediction/blob/c77b094fe5c17685ed138dae9ae49b304e0d8d89/code/common/model_builder.py#L275  # noqa: E501
              activation=activation if i < num_layers - 1 else None,
              activation_kwargs=activation_kwargs,
              self_loop_dropout=self_loop_dropout,
              decomposition=decomposition,
              decomposition_kwargs=decomposition_kwargs,
          )
          for i in range(num_layers)
      )
  
      # buffering of enriched representations
      self.enriched_embeddings = None
      self.cache = cache
  
imports: import logging
  import math
  from abc import ABC, abstractmethod
  from typing import Any, Iterable, Mapping, Optional, Sequence
  import torch
  from class_resolver import ClassResolver, Hint, HintOrType, OptionalKwargs
  from class_resolver.contrib.torch import activation_resolver
  from docdata import parse_docdata
  from torch import nn
  from .init import uniform_norm_p1_, xavier_normal_
  from .representation import LowRankRepresentation, Representation
  from .utils import ShapeError, adjacency_tensor_to_stacked_matrix, use_horizontal_stacking
  from .weighting import EdgeWeighting, edge_weight_resolver
  from ..triples import CoreTriplesFactory
  from ..utils import ExtraReprMixin, einsum
label_desc: Use image node_id 1 to create a new RGCNRepresentation object from inherited base classes: Representation with example: obj = RGCNRepresentation(triples_factory, max_id, shape, entity_representations, entity_representations_kwargs, num_layers, use_bias, activation, activation_kwargs, edge_dropout, _loop_dropout, edge_weighting, decomposition, decomposition_kwargs, cache)