class_docstr: Relation Classifier to predict the relation between two entities.
  
  Task
  ----
  Relation Classification (RC) is the task of identifying the semantic relation between two entities in a text.
  In contrast to (end-to-end) Relation Extraction (RE), RC requires pre-labelled entities.
  
  Example:
  --------
  For the `founded_by` relation from `ORG` (head) to `PER` (tail) and the sentence
  "Larry Page and Sergey Brin founded Google .", we extract the relations
  - founded_by(head='Google', tail='Larry Page') and
  - founded_by(head='Google', tail='Sergey Brin').
  
  Architecture
  ------------
  The Relation Classifier Model builds upon a text classifier.
  The model generates an encoded sentence for each entity pair
  in the cross product of all entities in the original sentence.
  In the encoded representation, the entities in the current entity pair are masked/marked with control tokens.
  (For an example, see the docstrings of different encoding strategies, e.g. :class:`TypedEntityMarker`.)
  Then, for each encoded sentence, the model takes its document embedding and puts the resulting
  text representation(s) through a linear layer to get the class relation label.
  
  The implemented encoding strategies are taken from this paper by Zhou et al.: https://arxiv.org/abs/2102.01373
  
  .. warning::
      Currently, the model has no multi-label support.
class_docstr_tok: ["Relation","Classifier","to","predict","the","relation","between","two","entities",".","Task","--","--","Relation","Classification","(","RC",")","is","the","task","of","identifying","the","semantic","relation","between","two","entities","in","a","text",".","In","contrast","to","(","end-to-end",")","Relation","Extraction","(","RE",")",",","RC","requires","pre-labelled","entities",".","Example",":","--","--","--","--","For","the","`","founded_by","`","relation","from","`","ORG","`","(","head",")","to","`","PER","`","(","tail",")","and","the","sentence","''","Larry","Page","and","Sergey","Brin","founded","Google",".","``",",","we","extract","the","relations","-","founded_by","(","head='Google","'",",","tail='Larry","Page","'",")","and","-","founded_by","(","head='Google","'",",","tail='Sergey","Brin","'",")",".","Architecture","--","--","--","--","--","--","The","Relation","Classifier","Model","builds","upon","a","text","classifier",".","The","model","generates","an","encoded","sentence","for","each","entity","pair","in","the","cross","product","of","all","entities","in","the","original","sentence",".","In","the","encoded","representation",",","the","entities","in","the","current","entity","pair","are","masked\/marked","with","control","tokens",".","(","For","an","example",",","see","the","docstrings","of","different","encoding","strategies",",","e.g",".",":","class",":","`","TypedEntityMarker","`",".",")","Then",",","for","each","encoded","sentence",",","the","model","takes","its","document","embedding","and","puts","the","resulting","text","representation","(","s",")","through","a","linear","layer","to","get","the","class","relation","label",".","The","implemented","encoding","strategies","are","taken","from","this","paper","by","Zhou","et","al",".",":","https",":","\/\/arxiv.org\/abs\/2102.01373","..","warning",":",":","Currently",",","the","model","has","no","multi-label","support","."]
docstr: Predicts the class labels for the given sentence(s).
  
  Standard `Sentence` objects and `EncodedSentences` specific to the `RelationClassifier` are allowed as input.
  The (relation) labels are directly added to the sentences.
  
  Args:
      sentences: A list of (encoded) sentences.
      mini_batch_size: The mini batch size to use
      return_probabilities_for_all_classes: Return probabilities for all classes instead of only best predicted
      verbose: Set to display a progress bar
      return_loss: Set to return loss
      label_name: Set to change the predicted label type name
      embedding_storage_mode: The default is 'none', which is always best. Only set to 'cpu' or 'gpu' if you wish to predict and keep the generated embeddings in CPU or GPU memory, respectively.
  
  Returns: The loss and the total number of classes, if `return_loss` is set
docstr_tok: ["Predicts","the","class","labels","for","the","given","sentence","(","s",")",".","Standard","`","Sentence","`","objects","and","`","EncodedSentences","`","specific","to","the","`","RelationClassifier","`","are","allowed","as","input",".","The","(","relation",")","labels","are","directly","added","to","the","sentences",".","Args",":","sentences",":","A","list","of","(","encoded",")","sentences",".","mini_batch_size",":","The","mini","batch","size","to","use","return_probabilities_for_all_classes",":","Return","probabilities","for","all","classes","instead","of","only","best","predicted","verbose",":","Set","to","display","a","progress","bar","return_loss",":","Set","to","return","loss","label_name",":","Set","to","change","the","predicted","label","type","name","embedding_storage_mode",":","The","default","is","'none","'",",","which","is","always","best",".","Only","set","to","'cpu","'","or","'gpu","'","if","you","wish","to","predict","and","keep","the","generated","embeddings","in","CPU","or","GPU","memory",",","respectively",".","Returns",":","The","loss","and","the","total","number","of","classes",",","if","`","return_loss","`","is","set"]
code: def predict(
      self,
      sentences: Union[
          List[Sentence],
          List[EncodedSentence],
          Sentence,
          EncodedSentence,
      ],
      mini_batch_size: int = 32,
      return_probabilities_for_all_classes: bool = False,
      verbose: bool = False,
      label_name: Optional[str] = None,
      return_loss: bool = False,
      embedding_storage_mode: str = "none",
  ) -> Optional[Tuple[torch.Tensor, int]]:
      """Predicts the class labels for the given sentence(s).
  
      Standard `Sentence` objects and `EncodedSentences` specific to the `RelationClassifier` are allowed as input.
      The (relation) labels are directly added to the sentences.
  
      Args:
          sentences: A list of (encoded) sentences.
          mini_batch_size: The mini batch size to use
          return_probabilities_for_all_classes: Return probabilities for all classes instead of only best predicted
          verbose: Set to display a progress bar
          return_loss: Set to return loss
          label_name: Set to change the predicted label type name
          embedding_storage_mode: The default is 'none', which is always best. Only set to 'cpu' or 'gpu' if you wish to predict and keep the generated embeddings in CPU or GPU memory, respectively.
  
      Returns: The loss and the total number of classes, if `return_loss` is set
      """
      prediction_label_type: str = (
          self.label_type if label_name is None else label_name
      )
  
      if not isinstance(sentences, list):
          sentences = [sentences]
  
      loss: Optional[Tuple[torch.Tensor, int]]
      encoded_sentences: List[EncodedSentence]
  
      if all(
          isinstance(sentence, EncodedSentence)
          for sentence in sentences
      ):
          # Deal with the case where all sentences are encoded sentences
  
          # mypy does not infer the type of "sentences" restricted by the if statement
          encoded_sentences = cast(List[EncodedSentence], sentences)
          loss = super().predict(
              encoded_sentences,
              mini_batch_size=mini_batch_size,
              return_probabilities_for_all_classes=return_probabilities_for_all_classes,
              verbose=verbose,
              label_name=prediction_label_type,
              return_loss=return_loss,
              embedding_storage_mode=embedding_storage_mode,
          )
  
      elif all(
          not isinstance(sentence, EncodedSentence)
          for sentence in sentences
      ):
          # Deal with the case where all sentences are standard (non-encoded) sentences
          Sentence.set_context_for_sentences(
              cast(List[Sentence], sentences)
          )
          sentences_with_relation_reference: List[
              Tuple[EncodedSentence, Relation]
          ] = list(
              itertools.chain.from_iterable(
                  self._encode_sentence_for_inference(sentence)
                  for sentence in sentences
              )
          )
  
          encoded_sentences = [
              x[0] for x in sentences_with_relation_reference
          ]
          loss = super().predict(
              encoded_sentences,
              mini_batch_size=mini_batch_size,
              return_probabilities_for_all_classes=return_probabilities_for_all_classes,
              verbose=verbose,
              label_name=prediction_label_type,
              return_loss=return_loss,
              embedding_storage_mode=embedding_storage_mode,
          )
  
          # For each encoded sentence, transfer its prediction onto the original relation
          for (
              encoded_sentence,
              original_relation,
          ) in sentences_with_relation_reference:
              for label in encoded_sentence.get_labels(
                  prediction_label_type
              ):
                  original_relation.add_label(
                      prediction_label_type,
                      value=label.value,
                      score=label.score,
                  )
  
      else:
          raise ValueError(
              "All passed sentences must be either uniformly encoded or not."
          )
  
      return loss if return_loss else None
  
imports: import itertools
  import logging
  import typing
  from abc import ABC, abstractmethod
  from pathlib import Path
  from typing import Any, Dict, Iterator, List, NamedTuple, Optional, Sequence, Set, Tuple, Union, cast
  import torch
  from torch.utils.data.dataset import Dataset
  import flair
  from flair.data import Corpus, Dictionary, Label, Relation, Sentence, Span, Token, _iter_dataset
  from flair.datasets import DataLoader, FlairDatapointDataset
  from flair.embeddings import DocumentEmbeddings, TransformerDocumentEmbeddings
  from flair.tokenization import SpaceTokenizer
label_desc: Use image node_id 12 for calling the RelationClassifier obj's underlying member method code with example usage: obj.predict(sentences, mini_batch_size, return_probabilities_for_all_classes, verbose, label_name, return_loss, embedding_storage_mode) and returns: unknown