code: def test_sequence_cross_entropy_with_logits_masks_loss_correctly(
      self,
  ):
      # test weight masking by checking that a tensor with non-zero values in
      # masked positions returns the same loss as a tensor with zeros in those
      # positions.
      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)
      tensor2 = tensor.clone()
      tensor2[0, 3:, :] = 2
      tensor2[1, 4:, :] = 13
      tensor2[2, 2:, :] = 234
      tensor2[3, :, :] = 65
      targets = torch.LongTensor(numpy.random.randint(0, 3, [5, 7]))
      targets *= weights
  
      loss = util.sequence_cross_entropy_with_logits(
          tensor, targets, weights
      )
      loss2 = util.sequence_cross_entropy_with_logits(
          tensor2, targets, weights
      )
      assert loss.data.numpy() == loss2.data.numpy()
  
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 22 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_sequence_cross_entropy_with_logits_masks_loss_correctly() without return types