code: def test_sequence_cross_entropy_with_logits_averages_batch_correctly(
      self,
  ):
      # test batch average is the same as dividing the batch averaged
      # loss by the number of batches containing any non-padded tokens.
      tensor = torch.rand([5, 7, 4])
      tensor[0, 3:, :] = 0
      tensor[1, 4:, :] = 0
      tensor[2, 2:, :] = 0
      tensor[3, :, :] = 0
      weights = (tensor != 0.0)[:, :, 0].long().squeeze(-1)
      targets = torch.LongTensor(numpy.random.randint(0, 3, [5, 7]))
      targets *= weights
  
      loss = util.sequence_cross_entropy_with_logits(
          tensor, targets, weights
      )
  
      vector_loss = util.sequence_cross_entropy_with_logits(
          tensor, targets, weights, batch_average=False
      )
      # Batch has one completely padded row, so divide by 4.
      assert loss.data.numpy() == vector_loss.data.sum() / 4
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import numpy
  from numpy.testing import assert_array_almost_equal, assert_almost_equal
  import torch
  import pytest
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase
  from allennlp.nn import util
label_desc: Use image node_id 24 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_sequence_cross_entropy_with_logits_averages_batch_correctly() without return types