code: def test_cross_entropy_loss():
      b = sockeye.loss.CrossEntropyLoss(
          ignore_label=C.PAD_ID, label_smoothing=0.0
      )
      assert b.ignore_label == C.PAD_ID
      assert b.name == C.CROSS_ENTROPY
      assert b.weight == 1.0
      assert b._dtype == C.DTYPE_FP32
      assert b.output_name == C.LOGITS_NAME
      assert b.label_name == C.TARGET_LABEL_NAME
      assert b._alpha == 0.0
  
      logits = pt.tensor(
          [[1, 1, 1, 1], [4, 2, 2, 2], [1, 1, 1, 1], [1, 1, 1, 1]],
          dtype=pt.float32,
          requires_grad=True,
      )
      labels = pt.tensor([1, 0, 2, 3])
  
      loss_value, loss_samples = b(
          {C.LOGITS_NAME: logits, "other_stuff": None},
          {C.TARGET_LABEL_NAME: labels, "other_stuff": None},
      )
      loss_value.backward()
      assert loss_samples.item() == 1  # this loss returns always 1
  
      expected_logits_grad = pt.tensor(
          [
              [0.08333334, -0.25, 0.08333334, 0.08333334],
              [0.0, 0.0, 0.0, 0.0],
              [0.08333334, 0.08333334, -0.25, 0.08333334],
              [0.08333334, 0.08333334, 0.08333334, -0.25],
          ]
      )
      num_valid = (C.PAD_ID != labels).sum().item()
      expected_loss_value = pt.tensor(
          -(math.log(1 / 4) * 3) / num_valid
      )  # 3 valid rows, all uniform, divided by num_valid
  
      pt.testing.assert_close(loss_value, expected_loss_value)
      pt.testing.assert_close(logits.grad, expected_logits_grad)
  
file_path: sockeye/test/unit/test_loss.py
imports: import math
  import numpy
  import pytest
  import torch
  import sockeye.constants
  import sockeye.loss
  import sockeye.model
  import sockeye.utils
label_desc: Use image node_id 3 for calling a global function with example usage: test_cross_entropy_loss() without return types