class_docstr: This class tests the crypten.nn package.
class_docstr_tok: ["This","class","tests","the","crypten.nn","package","."]
docstr: Tests all Losses implemented in crypten.nn.
code: def test_losses(self):
      """
      Tests all Losses implemented in crypten.nn.
      """
  
      # create test tensor:
      input = (
          get_random_test_tensor(max_value=0.999, is_float=True).abs()
          + 0.001
      )
      target = (
          get_random_test_tensor(max_value=0.999, is_float=True).abs()
          + 0.001
      )
      encrypted_input = crypten.cryptensor(input)
      encrypted_target = crypten.cryptensor(target)
  
      losses = [
          "BCELoss",
          "BCEWithLogitsLoss",
          "L1Loss",
          "MSELoss",
      ]
  
      # test forward() function of all simple losses:
      for loss_name in losses:
          for skip_forward in [False, True]:
              enc_loss_object = getattr(crypten.nn, loss_name)(
                  skip_forward=skip_forward
              )
              self.assertEqual(
                  enc_loss_object.reduction,
                  "mean",
                  "Reduction used is not 'mean'",
              )
  
              input.requires_grad = True
              input.grad = None
  
              loss = getattr(torch.nn, loss_name)()(input, target)
              encrypted_loss = enc_loss_object(
                  encrypted_input, encrypted_target
              )
              if not skip_forward:
                  self._check(
                      encrypted_loss,
                      loss,
                      f"{loss_name} failed forward w/o grad",
                  )
  
              encrypted_input.requires_grad = True
              encrypted_input.grad = None
  
              encrypted_loss = enc_loss_object(
                  encrypted_input, encrypted_target
              )
              if not skip_forward:
                  self._check(
                      encrypted_loss,
                      loss,
                      f"{loss_name} failed forward with grad",
                  )
  
              # Check backward
              loss.backward()
              encrypted_loss.backward()
              self._check(
                  encrypted_input.grad,
                  input.grad,
                  f"{loss_name} grad failed",
              )
  
      # test forward() function of cross-entropy loss:
      batch_size, num_targets = 16, 5
      input = get_random_test_tensor(
          size=(batch_size, num_targets), is_float=True
      )
      target = get_random_test_tensor(
          size=(batch_size,), max_value=num_targets - 1
      ).abs()
      encrypted_input = crypten.cryptensor(input)
      encrypted_target = crypten.cryptensor(
          onehot(target, num_targets=num_targets)
      )
      enc_loss_object = crypten.nn.CrossEntropyLoss()
      self.assertEqual(
          enc_loss_object.reduction,
          "mean",
          "Reduction used is not 'mean'",
      )
  
      loss = torch.nn.CrossEntropyLoss()(input, target)
      encrypted_loss = crypten.nn.CrossEntropyLoss()(
          encrypted_input, encrypted_target
      )
      self._check(encrypted_loss, loss, "cross-entropy loss failed")
      encrypted_input.requires_grad = True
      encrypted_target.requires_grad = True
      encrypted_loss = crypten.nn.CrossEntropyLoss()(
          encrypted_input, encrypted_target
      )
      self._check(encrypted_loss, loss, "cross-entropy loss failed")
  
imports: import copy
  import itertools
  import logging
  import unittest
  import crypten
  import crypten.communicator
  import torch
  import torch.nn.functional
  from crypten.common.rng import generate_random_ring_element
  from crypten.common.tensor_types import is_float_tensor
  from crypten.config import cfg
  from crypten.encoder import FixedPointEncoder
  from test.multiprocess_test_case import get_random_linear, get_random_test_tensor, MultiProcessTestCase, onehot
label_desc: Use image node_id 16 for calling the TestNN obj's underlying member method code with example usage: obj.test_losses() without return types