class_docstr: A rank-based evaluator using sampled negatives instead of all negatives.
  
  See also [teru2020]_.
  
  Notice that this evaluator yields optimistic estimations of the metrics evaluated on all entities,
  cf. https://arxiv.org/abs/2106.06935.
class_docstr_tok: ["A","rank-based","evaluator","using","sampled","negatives","instead","of","all","negatives",".","See","also","[","teru2020","]","_",".","Notice","that","this","evaluator","yields","optimistic","estimations","of","the","metrics","evaluated","on","all","entities",",","cf",".","https",":","\/\/arxiv.org\/abs\/2106.06935","."]
docstr: Initialize the evaluator.
  
  :param evaluation_factory:
      the factory with evaluation triples
  :param additional_filter_triples:
      additional true triples to use for filtering; only relevant if not explicit negatives are given.
      cf. :func:`pykeen.evaluation.rank_based_evaluator.sample_negatives`
  :param num_negatives:
      the number of negatives to sample; only relevant if not explicit negatives are given.
      cf. :func:`pykeen.evaluation.rank_based_evaluator.sample_negatives`
  :param head_negatives: shape: (num_triples, num_negatives)
      the entity IDs of negative samples for head prediction for each evaluation triple
  :param tail_negatives: shape: (num_triples, num_negatives)
      the entity IDs of negative samples for tail prediction for each evaluation triple
  :param kwargs:
      additional keyword-based arguments passed to
      :meth:`pykeen.evaluation.rank_based_evaluator.RankBasedEvaluator.__init__`
  
  :raises ValueError:
      if only a single side's negatives are given, or the negatives are in wrong shape
code: def __init__(
      self,
      evaluation_factory: CoreTriplesFactory,
      *,
      additional_filter_triples: Union[
          None, MappedTriples, List[MappedTriples]
      ] = None,
      num_negatives: Optional[int] = None,
      head_negatives: Optional[torch.LongTensor] = None,
      tail_negatives: Optional[torch.LongTensor] = None,
      **kwargs,
  ):
      """
      Initialize the evaluator.
  
      :param evaluation_factory:
          the factory with evaluation triples
      :param additional_filter_triples:
          additional true triples to use for filtering; only relevant if not explicit negatives are given.
          cf. :func:`pykeen.evaluation.rank_based_evaluator.sample_negatives`
      :param num_negatives:
          the number of negatives to sample; only relevant if not explicit negatives are given.
          cf. :func:`pykeen.evaluation.rank_based_evaluator.sample_negatives`
      :param head_negatives: shape: (num_triples, num_negatives)
          the entity IDs of negative samples for head prediction for each evaluation triple
      :param tail_negatives: shape: (num_triples, num_negatives)
          the entity IDs of negative samples for tail prediction for each evaluation triple
      :param kwargs:
          additional keyword-based arguments passed to
          :meth:`pykeen.evaluation.rank_based_evaluator.RankBasedEvaluator.__init__`
  
      :raises ValueError:
          if only a single side's negatives are given, or the negatives are in wrong shape
      """
      super().__init__(**kwargs)
      if head_negatives is None and tail_negatives is None:
          # default for inductive LP by [teru2020]
          num_negatives = num_negatives or 50
          logger.info(
              f"Sampling {num_negatives} negatives for each of the "
              f"{evaluation_factory.num_triples} evaluation triples.",
          )
          if num_negatives > evaluation_factory.num_entities:
              raise ValueError(
                  "Cannot use more negative samples than there are entities."
              )
          negatives = sample_negatives(
              evaluation_triples=evaluation_factory.mapped_triples,
              additional_filter_triples=additional_filter_triples,
              num_entities=evaluation_factory.num_entities,
              num_samples=num_negatives,
          )
      elif head_negatives is None or tail_negatives is None:
          raise ValueError(
              "Either both, head and tail negatives must be provided, or none."
          )
      else:
          negatives = {
              LABEL_HEAD: head_negatives,
              LABEL_TAIL: tail_negatives,
          }
          if additional_filter_triples is not None:
              logger.warning(
                  f"Ignoring parameter additional_filter_triples={additional_filter_triples}"
              )
  
      # verify input
      for side, side_negatives in negatives.items():
          if side_negatives.shape[0] != evaluation_factory.num_triples:
              raise ValueError(
                  f"Negatives for {side} are in wrong shape: {side_negatives.shape}"
              )
      self.triple_to_index = {
          (h, r, t): i
          for i, (h, r, t) in enumerate(
              evaluation_factory.mapped_triples.tolist()
          )
      }
      self.negative_samples = negatives
      self.num_entities = evaluation_factory.num_entities
  
imports: from __future__ import annotations
  import functools
  import itertools
  import logging
  import math
  import random
  import re
  from collections import defaultdict
  from typing import Callable, DefaultDict, Iterable, List, Mapping, MutableMapping, NamedTuple, Optional, Sequence, Tuple, Type, TypeVar, Union, cast
  import numpy
  import numpy.random
  import pandas
  import torch
  from class_resolver import HintOrType, OptionalKwargs
  from .evaluator import Evaluator, MetricResults, prepare_filter_triples
  from .ranks import Ranks
  from ..constants import COLUMN_LABELS, TARGET_TO_KEY_LABELS, TARGET_TO_KEYS
  from ..metrics.ranking import HITS_METRICS, RankBasedMetric, rank_based_metric_resolver
  from ..metrics.utils import Metric
  from ..triples.triples_factory import CoreTriplesFactory
  from ..typing import LABEL_HEAD, LABEL_TAIL, RANK_REALISTIC, RANK_TYPE_SYNONYMS, RANK_TYPES, SIDE_BOTH, SIDES, ExtendedTarget, MappedTriples, RankType, Target, normalize_rank_type, normalize_target
label_desc: Use image node_id 1 to create a new SampledRankBasedEvaluator object from inherited base classes: RankBasedEvaluator with example: obj = SampledRankBasedEvaluator(evaluation_factory)