code: def test_masked_log_softmax_masked(self):
      # Tests replicated from test_softmax_masked - we test that exponentiated,
      # the log softmax contains the correct elements (masked elements should be == 1).
  
      # Testing the general masked 1D case.
      vector_1d = torch.FloatTensor([[1.0, 2.0, 5.0]])
      mask_1d = torch.FloatTensor([[1.0, 0.0, 1.0]])
      vector_1d_softmaxed = util.masked_log_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          numpy.exp(vector_1d_softmaxed),
          numpy.array([[0.01798621, 0.0, 0.98201382]]),
      )
  
      vector_1d = torch.FloatTensor([[0.0, 2.0, 3.0, 4.0]])
      mask_1d = torch.FloatTensor([[1.0, 0.0, 1.0, 1.0]])
      vector_1d_softmaxed = util.masked_log_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          numpy.exp(vector_1d_softmaxed),
          numpy.array([[0.01321289, 0.0, 0.26538793, 0.72139918]]),
      )
  
      # Testing the masked 1D case where the input is all 0s and the mask
      # is not all 0s.
      vector_1d = torch.FloatTensor([[0.0, 0.0, 0.0, 0.0]])
      mask_1d = torch.FloatTensor([[0.0, 0.0, 0.0, 1.0]])
      vector_1d_softmaxed = util.masked_log_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          numpy.exp(vector_1d_softmaxed),
          numpy.array([[0.0, 0.0, 0.0, 1.0]]),
      )
  
      # Testing the masked 1D case where the input is not all 0s
      # and the mask is all 0s.  The output here will be arbitrary, but it should not be nan.
      vector_1d = torch.FloatTensor([[0.0, 2.0, 3.0, 4.0]])
      mask_1d = torch.FloatTensor([[0.0, 0.0, 0.0, 0.0]])
      vector_1d_softmaxed = util.masked_log_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert not numpy.isnan(vector_1d_softmaxed).any()
  
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 8 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_masked_log_softmax_masked() without return types