code: def test_case2(self):
      # 测试CRF是否正常work。
      import json
      import torch
      from fastNLP import seq_len_to_mask
  
      folder = os.path.dirname(os.path.abspath(__file__))
      path = os.path.join(
          folder, "../../../", "helpers/data/modules/decoder/crf.json"
      )
  
      with open(os.path.abspath(path), "r") as f:
          data = json.load(f)
  
      bio_logits = torch.FloatTensor(data["bio_logits"])
      bio_scores = data["bio_scores"]
      bio_path = data["bio_path"]
      bio_trans_m = torch.FloatTensor(data["bio_trans_m"])
      bio_seq_lens = torch.LongTensor(data["bio_seq_lens"])
  
      bmes_logits = torch.FloatTensor(data["bmes_logits"])
      bmes_scores = data["bmes_scores"]
      bmes_path = data["bmes_path"]
      bmes_trans_m = torch.FloatTensor(data["bmes_trans_m"])
      bmes_seq_lens = torch.LongTensor(data["bmes_seq_lens"])
  
      labels = ["O"]
      for label in ["X", "Y"]:
          for tag in "BI":
              labels.append("{}-{}".format(tag, label))
      id2label = {idx: label for idx, label in enumerate(labels)}
      num_tags = len(id2label)
  
      mask = seq_len_to_mask(bio_seq_lens)
  
      from fastNLP.modules.torch.decoder.crf import (
          ConditionalRandomField,
          allowed_transitions,
      )
  
      fast_CRF = ConditionalRandomField(
          num_tags=num_tags,
          allowed_transitions=allowed_transitions(
              id2label, include_start_end=True
          ),
      )
      fast_CRF.trans_m.data = bio_trans_m
      fast_res = fast_CRF.viterbi_decode(bio_logits, mask, unpad=True)
      # score equal
      assert bio_scores == [round(s, 4) for s in fast_res[1].tolist()]
      # seq equal
      assert bio_path == fast_res[0]
  
      labels = []
      for label in ["X", "Y"]:
          for tag in "BMES":
              labels.append("{}-{}".format(tag, label))
      id2label = {idx: label for idx, label in enumerate(labels)}
      num_tags = len(id2label)
  
      mask = seq_len_to_mask(bmes_seq_lens)
  
      from fastNLP.modules.torch.decoder.crf import (
          ConditionalRandomField,
          allowed_transitions,
      )
  
      fast_CRF = ConditionalRandomField(
          num_tags=num_tags,
          allowed_transitions=allowed_transitions(
              id2label, encoding_type="BMES", include_start_end=True
          ),
      )
      fast_CRF.trans_m.data = bmes_trans_m
      fast_res = fast_CRF.viterbi_decode(bmes_logits, mask, unpad=True)
      # score equal
      assert bmes_scores == [round(s, 4) for s in fast_res[1].tolist()]
      # seq equal
      assert bmes_path == fast_res[0]
  
file_path: fastNLP/tests/modules/torch/decoder/test_CRF.py
imports: import pytest
  import os
  from fastNLP import Vocabulary
label_desc: Use image node_id 4 for calling the TestCRF obj's underlying member method code with example usage: obj.test_case2() without return types