code: def test_crf_forward(self, num_tags, seq_lens):
      crf_model = CRF(
          num_tags,
          ignore_index=Padding.WORD_LABEL_PAD_IDX,
          default_label_pad_index=Padding.DEFAULT_LABEL_PAD_IDX,
      )
  
      total_manual_loss = 0
  
      padded_inputs = []
      padded_targets = []
  
      max_num_words = max(seq_lens)
  
      for seq_len in seq_lens:
          target_tokens = np.random.randint(
              1, num_tags, size=(1, seq_len)
          )
          padded_targets.append(
              np.concatenate(
                  [
                      target_tokens,
                      np.zeros((1, max_num_words - seq_len)),
                  ],
                  axis=1,
              )
          )
  
          input_emission = np.random.rand(seq_len, num_tags)
          padded_inputs.append(
              np.concatenate(
                  [
                      input_emission,
                      np.zeros((max_num_words - seq_len, num_tags)),
                  ],
                  axis=0,
              )
          )
  
          manual_loss = self._compute_loss_manual(
              input_emission,
              num_tags,
              target_tokens.reshape(-1),
              crf_model.get_transitions().tolist(),
          )
          crf_loss = crf_model(
              torch.tensor(input_emission, dtype=torch.float).unsqueeze(
                  0
              ),
              torch.tensor(target_tokens),
          )
  
          # Loss returned by CRF model for each input should be equal to
          # manually calculated loss
          self.assertAlmostEqual(
              manual_loss, -1 * crf_loss.item(), places=4
          )
          total_manual_loss += manual_loss
  
      # Loss returned by CRF model for batched input should be equal to
      # average of manually calculated loss
      batched_crf_loss = crf_model(
          torch.tensor(padded_inputs, dtype=torch.float),
          torch.tensor(padded_targets, dtype=torch.long).squeeze(1),
      )
      self.assertAlmostEqual(
          total_manual_loss / len(seq_lens),
          -1 * batched_crf_loss.item(),
          places=4,
      )
  
file_path: pytext/pytext/models/test/crf_test.py
imports: import caffe2.python.hypothesis_test_util
  import hypothesis.strategies
  import numpy
  import torch
  from hypothesis import given, settings
  from pytext.common.constants import Padding
  from pytext.models.crf import CRF
  from scipy.special import logsumexp
label_desc: Use image node_id 1 for calling the CRFTest obj's underlying member method code with example usage: obj.test_crf_forward(num_tags, seq_lens) without return types