class_docstr: 条件随机场。提供 :meth:`forward` 以及 :meth:`viterbi_decode` 两个方法，分别用于 **训练** 与 **inference** 。
  
  :param num_tags: 标签的数量
  :param include_start_end_trans: 是否考虑各个 tag 作为开始以及结尾的分数。
  :param allowed_transitions: 内部的 ``Tuple[from_tag_id(int), to_tag_id(int)]`` 视为允许发生的跃迁，其他没
      有包含的跃迁认为是禁止跃迁，可以通过 :func:`allowed_transitions` 函数得到；如果为 ``None`` ，则所有跃迁均为合法。
class_docstr_tok: ["\u6761\u4ef6\u968f\u673a\u573a\u3002\u63d0\u4f9b",":","meth",":","`","forward","`","\u4ee5\u53ca",":","meth",":","`","viterbi_decode","`","\u4e24\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u7528\u4e8e","*","*","\u8bad\u7ec3","*","*","\u4e0e","*","*","inference","*","*","\u3002",":","param","num_tags",":","\u6807\u7b7e\u7684\u6570\u91cf",":","param","include_start_end_trans",":","\u662f\u5426\u8003\u8651\u5404\u4e2a","tag","\u4f5c\u4e3a\u5f00\u59cb\u4ee5\u53ca\u7ed3\u5c3e\u7684\u5206\u6570\u3002",":","param","allowed_transitions",":","\u5185\u90e8\u7684","``","Tuple","[","from_tag_id","(","int",")",",","to_tag_id","(","int",")","]","``","\u89c6\u4e3a\u5141\u8bb8\u53d1\u751f\u7684\u8dc3\u8fc1\uff0c\u5176\u4ed6\u6ca1","\u6709\u5305\u542b\u7684\u8dc3\u8fc1\u8ba4\u4e3a\u662f\u7981\u6b62\u8dc3\u8fc1\uff0c\u53ef\u4ee5\u901a\u8fc7",":","func",":","`","allowed_transitions","`","\u51fd\u6570\u5f97\u5230\uff1b\u5982\u679c\u4e3a","``","None","``","\uff0c\u5219\u6240\u6709\u8dc3\u8fc1\u5747\u4e3a\u5408\u6cd5\u3002"]
docstr: 用于计算 ``CRF`` 的前向 loss，返回值为一个形状为 ``[batch_size,]`` 的 :class:`torch.FloatTensor` ，可能需要 :func:`mean` 求得 loss 。
  
  :param feats: 特征矩阵，形状为 ``[batch_size, max_len, num_tags]``
  :param tags: 标签矩阵，形状为 ``[batch_size, max_len]``
  :param mask: 形状为 ``[batch_size, max_len]`` ，为 **0** 的位置认为是 padding。
  :return: ``[batch_size,]``
docstr_tok: ["\u7528\u4e8e\u8ba1\u7b97","``","CRF","``","\u7684\u524d\u5411","loss\uff0c\u8fd4\u56de\u503c\u4e3a\u4e00\u4e2a\u5f62\u72b6\u4e3a","``","[","batch_size",",","]","``","\u7684",":","class",":","`","torch.FloatTensor","`","\uff0c\u53ef\u80fd\u9700\u8981",":","func",":","`","mean","`","\u6c42\u5f97","loss","\u3002",":","param","feats",":","\u7279\u5f81\u77e9\u9635\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","max_len",",","num_tags","]","``",":","param","tags",":","\u6807\u7b7e\u77e9\u9635\uff0c\u5f62\u72b6\u4e3a","``","[","batch_size",",","max_len","]","``",":","param","mask",":","\u5f62\u72b6\u4e3a","``","[","batch_size",",","max_len","]","``","\uff0c\u4e3a","*","*","0","*","*","\u7684\u4f4d\u7f6e\u8ba4\u4e3a\u662f","padding\u3002",":","return",":","``","[","batch_size",",","]","``"]
code: def forward(
      self,
      feats: "torch.FloatTensor",
      tags: "torch.LongTensor",
      mask: "torch.ByteTensor",
  ) -> "torch.FloatTensor":
      r"""
      用于计算 ``CRF`` 的前向 loss，返回值为一个形状为 ``[batch_size,]`` 的 :class:`torch.FloatTensor` ，可能需要 :func:`mean` 求得 loss 。
  
      :param feats: 特征矩阵，形状为 ``[batch_size, max_len, num_tags]``
      :param tags: 标签矩阵，形状为 ``[batch_size, max_len]``
      :param mask: 形状为 ``[batch_size, max_len]`` ，为 **0** 的位置认为是 padding。
      :return: ``[batch_size,]``
      """
      feats = feats.transpose(0, 1)
      tags = tags.transpose(0, 1).long()
      mask = mask.transpose(0, 1).float()
      all_path_score = self._normalizer_likelihood(feats, mask)
      gold_path_score = self._gold_score(feats, tags, mask)
  
      return all_path_score - gold_path_score
  
file_path: fastNLP/fastNLP/modules/torch/decoder/crf.py
imports: from typing import Union, List, Tuple
  import torch
  from torch import nn
  from ....core.metrics.span_f1_pre_rec_metric import _get_encoding_type_from_tag_vocab, _check_tag_vocab_and_encoding_type
  from ....core.vocabulary import Vocabulary
label_desc: Use image node_id 4 for calling the ConditionalRandomField obj's underlying member method code with example usage: obj.forward(feats, tags, mask) and returns: unknown