class_docstr: Self Adversarial Sampling loss.
  
  Introduced in :cite:`sun2018rotate`.
  
  .. math::
  
      \mathcal{L} = -log \left( \sigma(\gamma + f_{model} (\mathbf{s},\mathbf{o})) \right)
      - \sum_{i=1}^{n} p(h'_{i}, r, t'_{i} ) \cdot log
      \left( \sigma(-f_{model}(\mathbf{s}'_{i},\mathbf{o}'_{i}) - \gamma) \right)
  
  where :math:`\mathbf{s}, \mathbf{o} \in \mathcal{R}^k` are the embeddings of the subject
  and object of a triple :math:`t=(s,r,o)`, :math:`\gamma \in \mathbb{R}` is the margin, :math:`\sigma` the sigmoid
  function, and :math:`p(s'_{i}, r, o'_{i})` is the negatives sampling distribution which is defined as:
  
  .. math::
  
      p(s'_j, r, o'_j | \{(s_i, r_i, o_i)\}) = \frac{\exp \left( \alpha \, f_{model}(\mathbf{s'_j}, \mathbf{o'_j}) \right)}
      {\sum_i \exp \left( \alpha \, f_{model}(\mathbf{s'_i}, \mathbf{o'_i}) \right)}
  
  where :math:`\alpha` is the temperature of sampling and :math:`f_{model}` is the scoring function of
  the desired embedding model.
  
  Example
  -------
  >>> import ampligraph.latent_features.loss_functions as lfs
  >>> loss = lfs.SelfAdversarialLoss({'margin': 1, 'alpha': 0.1, 'reduction': 'mean'})
  >>> isinstance(loss, lfs.SelfAdversarialLoss)
  True
  
  >>> loss = lfs.get('self_adversarial')
  >>> isinstance(loss, lfs.SelfAdversarialLoss)
  True
class_docstr_tok: ["Self","Adversarial","Sampling","loss",".","Introduced","in",":","cite",":","`","sun2018rotate","`",".","..","math",":",":","\\mathcal","{","L","}","=","-log","\\left","(","\\sigma","(","\\gamma","+","f_","{","model","}","(","\\mathbf","{","s","}",",","\\mathbf","{","o","}",")",")","\\right",")","-","\\sum_","{","i=1","}","^","{","n","}","p","(","h","'","_","{","i","}",",","r",",","t","'","_","{","i","}",")","\\cdot","log","\\left","(","\\sigma","(","-f_","{","model","}","(","\\mathbf","{","s","}","'","_","{","i","}",",","\\mathbf","{","o","}","'","_","{","i","}",")","-","\\gamma",")","\\right",")","where",":","math",":","`","\\mathbf","{","s","}",",","\\mathbf","{","o","}","\\in","\\mathcal","{","R","}","^k","`","are","the","embeddings","of","the","subject","and","object","of","a","triple",":","math",":","`","t=","(","s",",","r",",","o",")","`",",",":","math",":","`","\\gamma","\\in","\\mathbb","{","R","}","`","is","the","margin",",",":","math",":","`","\\sigma","`","the","sigmoid","function",",","and",":","math",":","`","p","(","s","'","_","{","i","}",",","r",",","o","'","_","{","i","}",")","`","is","the","negatives","sampling","distribution","which","is","defined","as",":","..","math",":",":","p","(","s'_j",",","r",",","o'_j","|","\\","{","(","s_i",",","r_i",",","o_i",")","\\","}",")","=","\\frac","{","\\exp","\\left","(","\\alpha","\\",",","f_","{","model","}","(","\\mathbf","{","s'_j","}",",","\\mathbf","{","o'_j","}",")","\\right",")","}","{","\\sum_i","\\exp","\\left","(","\\alpha","\\",",","f_","{","model","}","(","\\mathbf","{","s'_i","}",",","\\mathbf","{","o'_i","}",")","\\right",")","}","where",":","math",":","`","\\alpha","`","is","the","temperature","of","sampling","and",":","math",":","`","f_","{","model","}","`","is","the","scoring","function","of","the","desired","embedding","model",".","Example","--","--","--","-",">",">",">","import","ampligraph.latent_features.loss_functions","as","lfs",">",">",">","loss","=","lfs.SelfAdversarialLoss","(","{","'margin","'",":","1",",","'alpha","'",":","0.1",",","'reduction","'",":","'mean","'","}",")",">",">",">","isinstance","(","loss",",","lfs.SelfAdversarialLoss",")","True",">",">",">","loss","=","lfs.get","(","'self_adversarial","'",")",">",">",">","isinstance","(","loss",",","lfs.SelfAdversarialLoss",")","True"]
docstr: Apply the loss function.
  
  Parameters
  ----------
  scores_pos : tf.Tensor, shape (n, 1)
      A tensor of scores assigned to positive statements.
  scores_neg : tf.Tensor, shape (eta, n)
      A tensor of scores assigned to negative statements.
  
  Returns
  -------
  loss : tf.Tensor
      The loss value that must be minimized.
docstr_tok: ["Apply","the","loss","function",".","Parameters","--","--","--","--","--","scores_pos",":","tf.Tensor",",","shape","(","n",",","1",")","A","tensor","of","scores","assigned","to","positive","statements",".","scores_neg",":","tf.Tensor",",","shape","(","eta",",","n",")","A","tensor","of","scores","assigned","to","negative","statements",".","Returns","--","--","--","-","loss",":","tf.Tensor","The","loss","value","that","must","be","minimized","."]
code: def _apply_loss(self, scores_pos, scores_neg):
      """Apply the loss function.
  
      Parameters
      ----------
      scores_pos : tf.Tensor, shape (n, 1)
          A tensor of scores assigned to positive statements.
      scores_neg : tf.Tensor, shape (eta, n)
          A tensor of scores assigned to negative statements.
  
      Returns
      -------
      loss : tf.Tensor
          The loss value that must be minimized.
  
      """
      margin = tf.constant(
          self._loss_parameters["margin"],
          dtype=tf.float32,
          name="margin",
      )
      alpha = tf.constant(
          self._loss_parameters["alpha"], dtype=tf.float32, name="alpha"
      )
  
      p_neg = tf.nn.softmax(alpha * scores_neg, axis=0)
  
      # Compute Loss based on eg 5
      loss = -tf.math.log_sigmoid(
          margin - tf.negative(scores_pos)
      ) - self._reduce_sample_loss(
          tf.multiply(
              p_neg,
              tf.math.log_sigmoid(tf.negative(scores_neg) - margin),
          )
      )
  
      return loss
  
imports: import abc
  import logging
  import six
  import tensorflow
  from tensorflow.python.keras import metrics
  from tensorflow.python.keras.utils import losses_utils
  from tensorflow.python.ops import math_ops
label_desc: Use image node_id 3 for calling the SelfAdversarialLoss obj's underlying member method code with example usage: obj._apply_loss(scores_pos, scores_neg) and returns: loss