code: def test_correct_sequence_elements_are_embedded_with_a_masked_sequence(
      self,
  ):
      sequence_tensor = torch.randn([2, 5, 8])
      # concatentate start and end points together to form our representation
      # for both the forward and backward directions.
      extractor = BidirectionalEndpointSpanExtractor(
          input_dim=8,
          forward_combination="x,y",
          backward_combination="x,y",
      )
      indices = torch.LongTensor(
          [
              [[1, 3], [2, 4]],
              # This span has an end index at the
              # end of the padded sequence.
              [[0, 2], [0, 1]],
          ]
      )
      sequence_mask = torch.tensor(
          [
              [True, True, True, True, True],
              [True, True, True, False, False],
          ]
      )
  
      span_representations = extractor(
          sequence_tensor, indices, sequence_mask=sequence_mask
      )
  
      # We just concatenated the start and end embeddings together, so
      # we can check they match the original indices if we split them apart.
      (
          forward_start_embeddings,
          forward_end_embeddings,
          backward_start_embeddings,
          backward_end_embeddings,
      ) = span_representations.split(4, -1)
  
      (
          forward_sequence_tensor,
          backward_sequence_tensor,
      ) = sequence_tensor.split(4, -1)
  
      # Forward direction => subtract 1 from start indices to make them exlusive.
      correct_forward_start_indices = torch.LongTensor(
          [[0, 1], [-1, -1]]
      )
      # These indices should be -1, so they'll be replaced with a sentinel. Here,
      # we'll set them to a value other than -1 so we can index select the indices and
      # replace them later.
      correct_forward_start_indices[1, 0] = 1
      correct_forward_start_indices[1, 1] = 1
  
      # Forward direction => end indices are the same.
      correct_forward_end_indices = torch.LongTensor([[3, 4], [2, 1]])
  
      # Backward direction => start indices are exclusive, so add 1 to the end indices.
      correct_backward_start_indices = torch.LongTensor(
          [[4, 5], [3, 2]]
      )
      # These exclusive backward start indices are outside the tensor, so will be replaced
      # with the end sentinel. Here we replace them with ones so we can index select using
      # these indices without torch complaining.
      correct_backward_start_indices[0, 1] = 1
  
      # Backward direction => end indices are inclusive and equal to the forward start indices.
      correct_backward_end_indices = torch.LongTensor([[1, 2], [0, 0]])
  
      correct_forward_start_embeddings = batched_index_select(
          forward_sequence_tensor.contiguous(),
          correct_forward_start_indices,
      )
      # This element had sequence_tensor index of 0, so it's exclusive index is the start sentinel.
      correct_forward_start_embeddings[
          1, 0
      ] = extractor._start_sentinel.data
      correct_forward_start_embeddings[
          1, 1
      ] = extractor._start_sentinel.data
      numpy.testing.assert_array_equal(
          forward_start_embeddings.data.numpy(),
          correct_forward_start_embeddings.data.numpy(),
      )
  
      correct_forward_end_embeddings = batched_index_select(
          forward_sequence_tensor.contiguous(),
          correct_forward_end_indices,
      )
      numpy.testing.assert_array_equal(
          forward_end_embeddings.data.numpy(),
          correct_forward_end_embeddings.data.numpy(),
      )
  
      correct_backward_end_embeddings = batched_index_select(
          backward_sequence_tensor.contiguous(),
          correct_backward_end_indices,
      )
      numpy.testing.assert_array_equal(
          backward_end_embeddings.data.numpy(),
          correct_backward_end_embeddings.data.numpy(),
      )
  
      correct_backward_start_embeddings = batched_index_select(
          backward_sequence_tensor.contiguous(),
          correct_backward_start_indices,
      )
      # This element had sequence_tensor index == sequence_tensor.size(1),
      # so it's exclusive index is the end sentinel.
      correct_backward_start_embeddings[
          0, 1
      ] = extractor._end_sentinel.data
      # This element has sequence_tensor index == the masked length of the batch element,
      # so it should be the end_sentinel even though it isn't greater than sequence_tensor.size(1).
      correct_backward_start_embeddings[
          1, 0
      ] = extractor._end_sentinel.data
  
      numpy.testing.assert_array_equal(
          backward_start_embeddings.data.numpy(),
          correct_backward_start_embeddings.data.numpy(),
      )
  
file_path: allennlp/tests/modules/span_extractors/bidirectional_endpoint_span_extractor_test.py
imports: import numpy
  import pytest
  import torch
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.params import Params
  from allennlp.modules.span_extractors import BidirectionalEndpointSpanExtractor, SpanExtractor
  from allennlp.nn.util import batched_index_select
label_desc: Use image node_id 4 for calling the TestBidirectonalEndpointSpanExtractor obj's underlying member method code with example usage: obj.test_correct_sequence_elements_are_embedded_with_a_masked_sequence() without return types