code: def test_forward_works_with_mask(self):
      # Use a non-trivial mask
      mask = torch.tensor([[True, True, True], [True, True, False]])
  
      log_likelihood = self.crf(self.logits, self.tags, mask).item()
  
      # Now compute the log-likelihood manually
      manual_log_likelihood = 0.0
  
      # For each instance, manually compute the numerator
      #   (which is just the score for the logits and actual tags)
      # and the denominator
      #   (which is the log-sum-exp of the scores for the logits across all possible tags)
      for logits_i, tags_i, mask_i in zip(self.logits, self.tags, mask):
          # Find the sequence length for this input and only look at that much of each sequence.
          sequence_length = torch.sum(mask_i.detach())
          logits_i = logits_i.data[:sequence_length]
          tags_i = tags_i.data[:sequence_length]
  
          numerator = self.score(logits_i, tags_i)
          all_scores = [
              self.score(logits_i, tags_j)
              for tags_j in itertools.product(
                  range(5), repeat=sequence_length
              )
          ]
          denominator = math.log(
              sum(math.exp(score) for score in all_scores)
          )
          # And include them in the manual calculation.
          manual_log_likelihood += numerator - denominator
  
      # The manually computed log likelihood should equal the result of crf.forward.
      assert manual_log_likelihood.item() == approx(log_likelihood)
  
imports: import itertools
  import math
  from pytest import approx, raises
  import torch
  from numpy.testing import assert_allclose
  from allennlp.modules import ConditionalRandomField
  from allennlp.modules.conditional_random_field import ConditionalRandomFieldWeightEmission, ConditionalRandomFieldWeightTrans, ConditionalRandomFieldWeightLannoy
  from allennlp.modules.conditional_random_field.conditional_random_field import allowed_transitions
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.testing import AllenNlpTestCase
label_desc: Use image node_id 5 for calling the TestConditionalRandomField obj's underlying member method code with example usage: obj.test_forward_works_with_mask() without return types