code: def test_masked_softmax_masked(self):
      # 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_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          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_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          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_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          vector_1d_softmaxed, numpy.array([[0, 0, 0, 1]])
      )
  
      # Testing the masked 1D case where the input is not all 0s
      # and the mask is all 0s.
      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_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          vector_1d_softmaxed, numpy.array([[0.0, 0.0, 0.0, 0.0]])
      )
  
      # Testing the masked 1D case where the input is all 0s and
      # the mask is all 0s.
      vector_1d = torch.FloatTensor([[0.0, 0.0, 0.0, 0.0]])
      mask_1d = torch.FloatTensor([[0.0, 0.0, 0.0, 0.0]])
      vector_1d_softmaxed = util.masked_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          vector_1d_softmaxed, numpy.array([[0.0, 0.0, 0.0, 0.0]])
      )
  
      # Testing the masked 1D case where there are large elements in the
      # padding.
      vector_1d = torch.FloatTensor([[1.0, 1.0, 1e5]])
      mask_1d = torch.FloatTensor([[1.0, 1.0, 0.0]])
      vector_1d_softmaxed = util.masked_softmax(
          vector_1d, mask_1d
      ).data.numpy()
      assert_array_almost_equal(
          vector_1d_softmaxed, numpy.array([[0.5, 0.5, 0]])
      )
  
      # Testing the general masked batched case.
      matrix = torch.FloatTensor([[1.0, 2.0, 5.0], [1.0, 2.0, 3.0]])
      mask = torch.FloatTensor([[1.0, 0.0, 1.0], [1.0, 1.0, 1.0]])
      masked_matrix_softmaxed = util.masked_softmax(
          matrix, mask
      ).data.numpy()
      assert_array_almost_equal(
          masked_matrix_softmaxed,
          numpy.array(
              [
                  [0.01798621, 0.0, 0.98201382],
                  [0.090031, 0.244728, 0.665241],
              ]
          ),
      )
  
      # Testing the masked batch case where one of the inputs is all 0s but
      # none of the masks are all 0.
      matrix = torch.FloatTensor([[0.0, 0.0, 0.0], [1.0, 2.0, 3.0]])
      mask = torch.FloatTensor([[1.0, 0.0, 1.0], [1.0, 1.0, 1.0]])
      masked_matrix_softmaxed = util.masked_softmax(
          matrix, mask
      ).data.numpy()
      assert_array_almost_equal(
          masked_matrix_softmaxed,
          numpy.array(
              [[0.5, 0.0, 0.5], [0.090031, 0.244728, 0.665241]]
          ),
      )
  
      # Testing the masked batch case where one of the inputs is all 0s and
      # one of the masks are all 0.
      matrix = torch.FloatTensor([[0.0, 0.0, 0.0], [1.0, 2.0, 3.0]])
      mask = torch.FloatTensor([[1.0, 0.0, 1.0], [0.0, 0.0, 0.0]])
      masked_matrix_softmaxed = util.masked_softmax(
          matrix, mask
      ).data.numpy()
      assert_array_almost_equal(
          masked_matrix_softmaxed,
          numpy.array([[0.5, 0.0, 0.5], [0.0, 0.0, 0.0]]),
      )
  
      matrix = torch.FloatTensor([[0.0, 0.0, 0.0], [1.0, 2.0, 3.0]])
      mask = torch.FloatTensor([[0.0, 0.0, 0.0], [1.0, 0.0, 1.0]])
      masked_matrix_softmaxed = util.masked_softmax(
          matrix, mask
      ).data.numpy()
      assert_array_almost_equal(
          masked_matrix_softmaxed,
          numpy.array([[0.0, 0.0, 0.0], [0.11920292, 0.0, 0.88079708]]),
      )
  
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 7 for calling the TestNnUtil obj's underlying member method code with example usage: obj.test_masked_softmax_masked() without return types