class_docstr: In the state-of-the-art KGE models, loss functions were designed according to
  various pointwise, pairwise and multi-class approaches. Refers to
  `Loss Functions in Knowledge Graph Embedding Models
  <https://alammehwish.github.io/dl4kg-eswc/papers/paper%201.pdf>`__
  
  **Pointwise Loss Function**
  
  `MSELoss <https://pytorch.org/docs/master/generated/torch.nn.MSELoss.html>`__
  Creates a criterion that measures the mean squared error (squared L2 norm)
  between each element in the input :math:`x` and target :math:`y`.
  
  
  `SOFTMARGINLOSS <https://pytorch.org/docs/master/generated/torch.nn.SoftMarginLoss
  .html>`__ Creates a criterion that optimizes a two-class classification
  logistic loss between input tensor :math:`x` and target tensor :math:`y`
  (containing 1 or -1).
  Tips:   The number of positive and negative samples should be about the same,
          otherwise it's easy to overfit
  
  .. math::
      \text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{\text{x.nelement}()}
  
  
  **Pairwise Loss Function**
  
  `SoftplusLoss <https://github.com/thunlp/OpenKE/blob/OpenKE-PyTorch/openke/module/loss/
  SoftplusLoss.py>`__
  refers to the paper `OpenKE: An Open Toolkit for Knowledge Embedding
  <https://www.aclweb.org/anthology/D18-2024.pdf>`__
  
  `SigmoidLoss <https://github.com/thunlp/OpenKE/blob/OpenKE-PyTorch/openke/module/loss/
  SigmoidLoss.py>`__
  refers to the paper `OpenKE: An Open Toolkit for Knowledge Embedding
  <https://www.aclweb.org/anthology/D18-2024.pdf>`__
  
  **Multi-Class Loss Function**
  
  `Binary Cross Entropy Loss <https://pytorch.org/docs/master/generated/torch.nn.BCELoss.html>`__
  Creates a criterion that measures the Binary Cross Entropy between the target
  and the output. Note that the targets
  :math:`y` should be numbers between 0 and 1.
class_docstr_tok: ["In","the","state-of-the-art","KGE","models",",","loss","functions","were","designed","according","to","various","pointwise",",","pairwise","and","multi-class","approaches",".","Refers","to","`","Loss","Functions","in","Knowledge","Graph","Embedding","Models","<","https",":","\/\/alammehwish.github.io\/dl4kg-eswc\/papers\/paper","%","201.pdf",">","`","__","*","*","Pointwise","Loss","Function","*","*","`","MSELoss","<","https",":","\/\/pytorch.org\/docs\/master\/generated\/torch.nn.MSELoss.html",">","`","__","Creates","a","criterion","that","measures","the","mean","squared","error","(","squared","L2","norm",")","between","each","element","in","the","input",":","math",":","`","x","`","and","target",":","math",":","`","y","`",".","`","SOFTMARGINLOSS","<","https",":","\/\/pytorch.org\/docs\/master\/generated\/torch.nn.SoftMarginLoss",".html",">","`","__","Creates","a","criterion","that","optimizes","a","two-class","classification","logistic","loss","between","input","tensor",":","math",":","`","x","`","and","target","tensor",":","math",":","`","y","`","(","containing","1","or","-1",")",".","Tips",":","The","number","of","positive","and","negative","samples","should","be","about","the","same",",","otherwise","it","'s","easy","to","overfit","..","math",":",":","\\text","{","loss","}","(","x",",","y",")","=","\\sum_i","\\frac","{","\\log","(","1","+","\\exp","(","-y","[","i","]","*","x","[","i","]",")",")","}","{","\\text","{","x.nelement","}","(",")","}","*","*","Pairwise","Loss","Function","*","*","`","SoftplusLoss","<","https",":","\/\/github.com\/thunlp\/OpenKE\/blob\/OpenKE-PyTorch\/openke\/module\/loss\/","SoftplusLoss.py",">","`","__","refers","to","the","paper","`","OpenKE",":","An","Open","Toolkit","for","Knowledge","Embedding","<","https",":","\/\/www.aclweb.org\/anthology\/D18-2024.pdf",">","`","__","`","SigmoidLoss","<","https",":","\/\/github.com\/thunlp\/OpenKE\/blob\/OpenKE-PyTorch\/openke\/module\/loss\/","SigmoidLoss.py",">","`","__","refers","to","the","paper","`","OpenKE",":","An","Open","Toolkit","for","Knowledge","Embedding","<","https",":","\/\/www.aclweb.org\/anthology\/D18-2024.pdf",">","`","__","*","*","Multi-Class","Loss","Function","*","*","`","Binary","Cross","Entropy","Loss","<","https",":","\/\/pytorch.org\/docs\/master\/generated\/torch.nn.BCELoss.html",">","`","__","Creates","a","criterion","that","measures","the","Binary","Cross","Entropy","between","the","target","and","the","output",".","Note","that","the","targets",":","math",":","`","y","`","should","be","numbers","between","0","and","1","."]
docstr: Parameters
  ----------
  MSELoss
      input: Tensor.
          :math:`(N,*)` where :math:`*` means any number of additional dimensions
      target: Tensor.
          :math:`(N,*)`, same shape as the input
      output:
          If reduction is `'none'`, then same shape as the input
  
  SoftMarginLoss
      input: Tensor.
          :math:`(*)` where * means, any number of additional dimensions
      target: Tensor.
          same shape as the input
      output: scalar.
          If reduction is `'none'`, then same shape as the input
  
  SoftplusLoss
      p_score: Tensor.
          :math:`(*)` where * means, any number of additional dimensions
      n_score: Tensor.
          :math:`(*)` where * means, any number of additional dimensions.
          The dimension could be different from the `p_score` dimension.
      output: scalar.
  
  SigmoidLoss
      p_score: Tensor.
          :math:`(*)` where * means, any number of additional dimensions
      n_score: Tensor.
          :math:`(*)` where * means, any number of additional dimensions.
          The dimension could be different from the `p_score` dimension.
      output: scalar.
  
  BCELoss:
      Input: Tensor.
          :math:`(N, *)` where :math:`*` means, any number of additional dimensions
      Target: Tensor.
          :math:`(N, *)`, same shape as the input
      Output: scalar.
          If :attr:`reduction` is ``'none'``, then :math:`(N, *)`, same shape as input.
  
  Returns
  -------
docstr_tok: ["Parameters","--","--","--","--","--","MSELoss","input",":","Tensor",".",":","math",":","`","(","N",",","*",")","`","where",":","math",":","`","*","`","means","any","number","of","additional","dimensions","target",":","Tensor",".",":","math",":","`","(","N",",","*",")","`",",","same","shape","as","the","input","output",":","If","reduction","is","`","'none","'","`",",","then","same","shape","as","the","input","SoftMarginLoss","input",":","Tensor",".",":","math",":","`","(","*",")","`","where","*","means",",","any","number","of","additional","dimensions","target",":","Tensor",".","same","shape","as","the","input","output",":","scalar",".","If","reduction","is","`","'none","'","`",",","then","same","shape","as","the","input","SoftplusLoss","p_score",":","Tensor",".",":","math",":","`","(","*",")","`","where","*","means",",","any","number","of","additional","dimensions","n_score",":","Tensor",".",":","math",":","`","(","*",")","`","where","*","means",",","any","number","of","additional","dimensions",".","The","dimension","could","be","different","from","the","`","p_score","`","dimension",".","output",":","scalar",".","SigmoidLoss","p_score",":","Tensor",".",":","math",":","`","(","*",")","`","where","*","means",",","any","number","of","additional","dimensions","n_score",":","Tensor",".",":","math",":","`","(","*",")","`","where","*","means",",","any","number","of","additional","dimensions",".","The","dimension","could","be","different","from","the","`","p_score","`","dimension",".","output",":","scalar",".","BCELoss",":","Input",":","Tensor",".",":","math",":","`","(","N",",","*",")","`","where",":","math",":","`","*","`","means",",","any","number","of","additional","dimensions","Target",":","Tensor",".",":","math",":","`","(","N",",","*",")","`",",","same","shape","as","the","input","Output",":","scalar",".","If",":","attr",":","`","reduction","`","is","``","'none","'","``",",","then",":","math",":","`","(","N",",","*",")","`",",","same","shape","as","input",".","Returns","--","--","--","-"]
code: def forward(
      self, input=None, target=None, p_score=None, n_score=None
  ):
      """
  
      Parameters
      ----------
      MSELoss
          input: Tensor.
              :math:`(N,*)` where :math:`*` means any number of additional dimensions
          target: Tensor.
              :math:`(N,*)`, same shape as the input
          output:
              If reduction is `'none'`, then same shape as the input
  
      SoftMarginLoss
          input: Tensor.
              :math:`(*)` where * means, any number of additional dimensions
          target: Tensor.
              same shape as the input
          output: scalar.
              If reduction is `'none'`, then same shape as the input
  
      SoftplusLoss
          p_score: Tensor.
              :math:`(*)` where * means, any number of additional dimensions
          n_score: Tensor.
              :math:`(*)` where * means, any number of additional dimensions.
              The dimension could be different from the `p_score` dimension.
          output: scalar.
  
      SigmoidLoss
          p_score: Tensor.
              :math:`(*)` where * means, any number of additional dimensions
          n_score: Tensor.
              :math:`(*)` where * means, any number of additional dimensions.
              The dimension could be different from the `p_score` dimension.
          output: scalar.
  
      BCELoss:
          Input: Tensor.
              :math:`(N, *)` where :math:`*` means, any number of additional dimensions
          Target: Tensor.
              :math:`(N, *)`, same shape as the input
          Output: scalar.
              If :attr:`reduction` is ``'none'``, then :math:`(N, *)`, same shape as input.
  
      Returns
      -------
  
      """
      if self.loss_type in ["SoftplusLoss", "SigmoidLoss"]:
          return self.loss_function(p_score, n_score)
      else:
          return self.loss_function(input, target)
  
file_path: graph4nlp/graph4nlp/pytorch/modules/loss/kg_loss.py
imports: import torch
  import torch.nn
  from .base import KGLossBase
label_desc: Use image node_id 2 for calling the KGLoss obj's underlying member method code with example usage: obj.forward(input, target, p_score, n_score) and returns: self, self