code: def test_case3(self):
      # 测试crf的loss不会出现负数
      import torch
      from fastNLP.modules.torch.decoder.crf import (
          ConditionalRandomField,
      )
      from fastNLP.core.utils import seq_len_to_mask
      from torch import optim
      from torch import nn
  
      num_tags, include_start_end_trans = 4, True
      num_samples = 4
      lengths = torch.randint(3, 50, size=(num_samples,)).long()
      max_len = lengths.max()
      tags = torch.randint(num_tags, size=(num_samples, max_len))
      masks = seq_len_to_mask(lengths)
      feats = nn.Parameter(torch.randn(num_samples, max_len, num_tags))
      crf = ConditionalRandomField(num_tags, include_start_end_trans)
      optimizer = optim.SGD(
          [param for param in crf.parameters() if param.requires_grad]
          + [feats],
          lr=0.1,
      )
      for _ in range(10):
          loss = crf(feats, tags, masks).mean()
          optimizer.zero_grad()
          loss.backward()
          optimizer.step()
          if _ % 1000 == 0:
              print(loss)
          assert loss.item() > 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 5 for calling the TestCRF obj's underlying member method code with example usage: obj.test_case3() without return types