class_docstr: Initialize nodes via random-walk positional encoding.
  
  The random walk positional encoding is given as
  
  .. math::
      \mathbf{x}_i = [\mathbf{R}_{i, i}, \mathbf{R}^{2}_{i, i}, \ldots, \mathbf{R}^{d}_{i, i}] \in \mathbb{R}^{d}
  
  where $\mathbf{R} := \mathbf{A}\mathbf{D}^{-1}$ is the random walk matrix, with
  $\mathbf{D} := \sum_i \mathbf{A}_{i, i}$.
  
  .. seealso::
      https://arxiv.org/abs/2110.07875
class_docstr_tok: ["Initialize","nodes","via","random-walk","positional","encoding",".","The","random","walk","positional","encoding","is","given","as","..","math",":",":","\\mathbf","{","x","}","_i","=","[","\\mathbf","{","R","}","_","{","i",",","i","}",",","\\mathbf","{","R","}","^","{","2","}","_","{","i",",","i","}",",","\\ldots",",","\\mathbf","{","R","}","^","{","d","}","_","{","i",",","i","}","]","\\in","\\mathbb","{","R","}","^","{","d","}","where","$","\\mathbf","{","R","}",":","=","\\mathbf","{","A","}","\\mathbf","{","D","}","^","{","-1","}","$","is","the","random","walk","matrix",",","with","$","\\mathbf","{","D","}",":","=","\\sum_i","\\mathbf","{","A","}","_","{","i",",","i","}","$",".","..","seealso",":",":","https",":","\/\/arxiv.org\/abs\/2110.07875"]
docstr: Initialize the positional encoding.
  
  One of `triples_factory`, `mapped_triples` or `edge_index` will be used.
  The preference order is:
  
  1. `triples_factory`
  2. `mapped_triples`
  3. `edge_index`
  
  :param triples_factory:
      the triples factory
  :param mapped_triples: shape: `(m, 3)`
      the mapped triples
  :param edge_index: shape: `(2, m)`
      the edge index
  :param dim:
      the dimensionality
  :param num_entities:
      the number of entities. If `None`, it will be inferred from `edge_index`
  :param space_dim:
      estimated dimensionality of the space. Used to
      correct the random-walk diagonal by a factor `k^(space_dim/2)`.
      In euclidean space, this correction means that the height of
      the gaussian distribution stays almost constant across the number of
      steps, if `space_dim` is the dimension of the euclidean space.
  :param skip_first_power:
      in most cases the adjacencies diagonal values will be zeros (since reflexive edges are not that common).
      This flag enables skipping the first matrix power.
docstr_tok: ["Initialize","the","positional","encoding",".","One","of","`","triples_factory","`",",","`","mapped_triples","`","or","`","edge_index","`","will","be","used",".","The","preference","order","is",":","1",".","`","triples_factory","`","2",".","`","mapped_triples","`","3",".","`","edge_index","`",":","param","triples_factory",":","the","triples","factory",":","param","mapped_triples",":","shape",":","`","(","m",",","3",")","`","the","mapped","triples",":","param","edge_index",":","shape",":","`","(","2",",","m",")","`","the","edge","index",":","param","dim",":","the","dimensionality",":","param","num_entities",":","the","number","of","entities",".","If","`","None","`",",","it","will","be","inferred","from","`","edge_index","`",":","param","space_dim",":","estimated","dimensionality","of","the","space",".","Used","to","correct","the","random-walk","diagonal","by","a","factor","`","k^","(","space_dim\/2",")","`",".","In","euclidean","space",",","this","correction","means","that","the","height","of","the","gaussian","distribution","stays","almost","constant","across","the","number","of","steps",",","if","`","space_dim","`","is","the","dimension","of","the","euclidean","space",".",":","param","skip_first_power",":","in","most","cases","the","adjacencies","diagonal","values","will","be","zeros","(","since","reflexive","edges","are","not","that","common",")",".","This","flag","enables","skipping","the","first","matrix","power","."]
code: def __init__(
      self,
      *,
      triples_factory: Optional[CoreTriplesFactory] = None,
      mapped_triples: Optional[MappedTriples] = None,
      edge_index: Optional[torch.Tensor] = None,
      dim: int,
      num_entities: Optional[int] = None,
      space_dim: int = 0,
      skip_first_power: bool = True,
  ) -> None:
      """
      Initialize the positional encoding.
  
      One of `triples_factory`, `mapped_triples` or `edge_index` will be used.
      The preference order is:
  
      1. `triples_factory`
      2. `mapped_triples`
      3. `edge_index`
  
      :param triples_factory:
          the triples factory
      :param mapped_triples: shape: `(m, 3)`
          the mapped triples
      :param edge_index: shape: `(2, m)`
          the edge index
      :param dim:
          the dimensionality
      :param num_entities:
          the number of entities. If `None`, it will be inferred from `edge_index`
      :param space_dim:
          estimated dimensionality of the space. Used to
          correct the random-walk diagonal by a factor `k^(space_dim/2)`.
          In euclidean space, this correction means that the height of
          the gaussian distribution stays almost constant across the number of
          steps, if `space_dim` is the dimension of the euclidean space.
      :param skip_first_power:
          in most cases the adjacencies diagonal values will be zeros (since reflexive edges are not that common).
          This flag enables skipping the first matrix power.
      """
      edge_index = get_edge_index(
          triples_factory=triples_factory,
          mapped_triples=mapped_triples,
          edge_index=edge_index,
      )
      # create random walk matrix
      rw = torch_ppr.utils.prepare_page_rank_adjacency(
          edge_index=edge_index, num_nodes=num_entities
      )
      # stack diagonal entries of powers of rw
      tensor = torch.stack(
          [
              (i ** (space_dim / 2.0)) * safe_diagonal(matrix=power)
              for i, power in enumerate(
                  iter_matrix_power(matrix=rw, max_iter=dim), start=1
              )
              if not skip_first_power or i > 1
          ],
          dim=-1,
      )
      super().__init__(tensor=tensor)
  
imports: import functools
  import logging
  import math
  from typing import Any, Optional, Sequence
  import numpy
  import torch
  import torch.nn
  import torch.nn.init
  import torch_ppr.utils
  from class_resolver import FunctionResolver, Hint, HintOrType, OptionalKwargs
  from more_itertools import last
  from torch.nn import functional
  from .text import TextEncoder, text_encoder_resolver
  from .utils import iter_matrix_power, safe_diagonal
  from ..triples import CoreTriplesFactory, TriplesFactory
  from ..typing import Initializer, MappedTriples, OneOrSequence
  from ..utils import compose, get_edge_index, iter_weisfeiler_lehman, upgrade_to_sequence
label_desc: Use image node_id 1 to create a new RandomWalkPositionalEncodingInitializer object from inherited base classes: PretrainedInitializer with example: obj = RandomWalkPositionalEncodingInitializer()