class_docstr: Test the custom loss functions.
docstr: Test the negative sampling self adversarial loss function.
docstr_tok: ["Test","the","negative","sampling","self","adversarial","loss","function","."]
code: def test_negative_sampling_self_adversarial_loss(self):
      """Test the negative sampling self adversarial loss function."""
      loss_fct = NSSALoss(margin=1.0, adversarial_temperature=1.0)
      self.assertIs(loss_fct._reduction_method, torch.mean)
  
      pos_scores = torch.tensor([0.0, 0.0, -0.5, -0.5])
      neg_scores = torch.tensor([0.0, 0.0, -1.0, -1.0])
  
      # ≈ result of softmax
      weights = torch.tensor([0.37, 0.37, 0.13, 0.13])
  
      # neg_distances - margin = [-1., -1., 0., 0.]
      # sigmoids ≈ [0.27, 0.27, 0.5, 0.5]
      log_sigmoids = torch.tensor([-1.31, -1.31, -0.69, -0.69])
      intermediate = weights * log_sigmoids
      # sum over the softmax dim as weights sum up to 1
      neg_loss = torch.sum(intermediate, dim=-1)
  
      # pos_distances = [0., 0., 0.5, 0.5]
      # margin - pos_distances = [1. 1., 0.5, 0.5]
      # ≈ result of sigmoid
      # sigmoids ≈ [0.73, 0.73, 0.62, 0.62]
      log_sigmoids = torch.tensor([-0.31, -0.31, -0.48, -0.48])
      pos_loss = torch.mean(log_sigmoids)
  
      # expected_loss ≈ 0.34
      expected_loss = (-pos_loss - neg_loss) / 2.0
  
      loss = loss_fct(pos_scores, neg_scores, weights).item()
  
      self.assertAlmostEqual(expected_loss, 0.77, delta=0.02)
      self.assertAlmostEqual(expected_loss, loss, delta=0.02)
  
imports: import unittest
  import numpy
  import torch
  import unittest_templates
  import pykeen.losses
  from pykeen.losses import Loss, NSSALoss, apply_label_smoothing
  from pykeen.pipeline import PipelineResult, pipeline
  from tests import cases
label_desc: Use image node_id 1 for calling the TestCustomLossFunctions obj's underlying member method code with example usage: obj.test_negative_sampling_self_adversarial_loss() without return types