class_docstr: 在 **序列标注** 任务中评估抽取结果匹配度的 **Metric** 。
  
  :param tag_vocab: 标签的 :class:`~fastNLP.core.Vocabulary` 。支持的标签有 ``"B"`` (没有label)；或 ``"B-xxx"`` ( ``xxx`` 为某种 label ，比如 POS 中的 NN)，
      在解码时，会将相同 ``xxx`` 的认为是同一个 label ，比如 ['B-NN', 'E-NN'] 会被合并为一个 'NN' 。
  :param encoding_type: 目前支持 ``['bio', 'bmes', 'bmeso', 'bioes', None]`` 。默认为 ``None`` ，通过 ``tag_vocab`` 自动判断
  :param ignore_labels: 字符串组成的列表，这个列表中包含的内容不会被用于计算。例如在 *POS tagging* 时传入 ``['NN']`` ，则不会计算 ``'NN'`` 这个 ``label``
  :param only_gross: 是否只计算总的 ``f1``, ``precision`` , ``recall`` 的值。如果为 ``False`` ，不仅返回总的 ``f1`` , ``pre`` , ``rec`` , 还会返回每个 label 
      ``f1`` , ``pre`` , ``rec`` 
  :param f_type: ``'micro'`` 或 ``'macro'``。
  
      - *micro* -- 通过先计算总体的 ``TP``， ``FN`` 和 ``FP`` 的数量，再计算 ``f``, ``precision``, ``recall`` ；
      - *macro* -- 分别计算每个类别的 ``f`` , ``precision`` , ``recall`` ，然后做平均（各类别 ``f`` 的权重相同）；
  
  :param beta: **f_beta** 分数中的 ``beta`` 值。 常用为 ``beta=0.5, 1, 2`` 若为 0.5 则 **精确率** 的权重高于 **召回率** ；若为1，则两者平等；若为2，则
      **召回率** 权重高于 **精确率** 。**f_beta** 分数的计算公式为：
      
      .. math::
  
          f_{beta} = \\frac{(1 + {beta}^{2})*(pre*rec)}{({beta}^{2}*pre + rec)}
  
  :param backend: 目前支持五种类型的 backend, ``['torch', 'paddle', 'jittor', 'oneflow', 'auto']``。其中 ``'auto'`` 表示根据实际调用 :meth:`update`
      函数时传入的参数决定具体的 backend ，大部分情况下直接使用 ``'auto'`` 即可。
  :param aggregate_when_get_metric: 在计算 metric 的时候是否自动将各个进程上的相同的 element 的数字聚合后再得到 metric，
      当 backend 不支持分布式时，该参数无意义。如果为 ``None`` ，将在 :class:`~fastNLP.core.controllers.Evaluator` 中根据
      sampler 是否使用分布式进行自动设置。
class_docstr_tok: ["\u5728","*","*","\u5e8f\u5217\u6807\u6ce8","*","*","\u4efb\u52a1\u4e2d\u8bc4\u4f30\u62bd\u53d6\u7ed3\u679c\u5339\u914d\u5ea6\u7684","*","*","Metric","*","*","\u3002",":","param","tag_vocab",":","\u6807\u7b7e\u7684",":","class",":","`","~fastNLP.core.Vocabulary","`","\u3002\u652f\u6301\u7684\u6807\u7b7e\u6709","``","``","B","''","``","(","\u6ca1\u6709label",")","\uff1b\u6216","``","``","B-xxx","''","``","(","``","xxx","``","\u4e3a\u67d0\u79cd","label","\uff0c\u6bd4\u5982","POS","\u4e2d\u7684","NN",")","\uff0c","\u5728\u89e3\u7801\u65f6\uff0c\u4f1a\u5c06\u76f8\u540c","``","xxx","``","\u7684\u8ba4\u4e3a\u662f\u540c\u4e00\u4e2a","label","\uff0c\u6bd4\u5982","[","'","B-NN","'",",","'","E-NN","'","]","\u4f1a\u88ab\u5408\u5e76\u4e3a\u4e00\u4e2a","'NN","'","\u3002",":","param","encoding_type",":","\u76ee\u524d\u652f\u6301","``","[","'bio","'",",","'bmes","'",",","'bmeso","'",",","'bioes","'",",","None","]","``","\u3002\u9ed8\u8ba4\u4e3a","``","None","``","\uff0c\u901a\u8fc7","``","tag_vocab","``","\u81ea\u52a8\u5224\u65ad",":","param","ignore_labels",":","\u5b57\u7b26\u4e32\u7ec4\u6210\u7684\u5217\u8868\uff0c\u8fd9\u4e2a\u5217\u8868\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u4e0d\u4f1a\u88ab\u7528\u4e8e\u8ba1\u7b97\u3002\u4f8b\u5982\u5728","*","POS","tagging","*","\u65f6\u4f20\u5165","``","[","'NN","'","]","``","\uff0c\u5219\u4e0d\u4f1a\u8ba1\u7b97","``","'NN","'","``","\u8fd9\u4e2a","``","label","``",":","param","only_gross",":","\u662f\u5426\u53ea\u8ba1\u7b97\u603b\u7684","``","f1","``",",","``","precision","``",",","``","recall","``","\u7684\u503c\u3002\u5982\u679c\u4e3a","``","False","``","\uff0c\u4e0d\u4ec5\u8fd4\u56de\u603b\u7684","``","f1","``",",","``","pre","``",",","``","rec","``",",","\u8fd8\u4f1a\u8fd4\u56de\u6bcf\u4e2a","label","``","f1","``",",","``","pre","``",",","``","rec","``",":","param","f_type",":","``","'micro","'","``","\u6216","``","'macro","'","``","\u3002","-","*","micro","*","--","\u901a\u8fc7\u5148\u8ba1\u7b97\u603b\u4f53\u7684","``","TP","``","\uff0c","``","FN","``","\u548c","``","FP","``","\u7684\u6570\u91cf\uff0c\u518d\u8ba1\u7b97","``","f","``",",","``","precision","``",",","``","recall","``","\uff1b","-","*","macro","*","--","\u5206\u522b\u8ba1\u7b97\u6bcf\u4e2a\u7c7b\u522b\u7684","``","f","``",",","``","precision","``",",","``","recall","``","\uff0c\u7136\u540e\u505a\u5e73\u5747\uff08\u5404\u7c7b\u522b","``","f","``","\u7684\u6743\u91cd\u76f8\u540c\uff09\uff1b",":","param","beta",":","*","*","f_beta","*","*","\u5206\u6570\u4e2d\u7684","``","beta","``","\u503c\u3002","\u5e38\u7528\u4e3a","``","beta=0.5",",","1",",","2","``","\u82e5\u4e3a","0.5","\u5219","*","*","\u7cbe\u786e\u7387","*","*","\u7684\u6743\u91cd\u9ad8\u4e8e","*","*","\u53ec\u56de\u7387","*","*","\uff1b\u82e5\u4e3a1\uff0c\u5219\u4e24\u8005\u5e73\u7b49\uff1b\u82e5\u4e3a2\uff0c\u5219","*","*","\u53ec\u56de\u7387","*","*","\u6743\u91cd\u9ad8\u4e8e","*","*","\u7cbe\u786e\u7387","*","*","\u3002","*","*","f_beta","*","*","\u5206\u6570\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a","..","math",":",":","f_","{","beta","}","=","\\\\frac","{","(","1","+","{","beta","}","^","{","2","}",")","*","(","pre","*","rec",")","}","{","(","{","beta","}","^","{","2","}","*","pre","+","rec",")","}",":","param","backend",":","\u76ee\u524d\u652f\u6301\u4e94\u79cd\u7c7b\u578b\u7684","backend",",","``","[","'torch","'",",","'paddle","'",",","'jittor","'",",","'oneflow","'",",","'auto","'","]","``","\u3002\u5176\u4e2d","``","'auto","'","``","\u8868\u793a\u6839\u636e\u5b9e\u9645\u8c03\u7528",":","meth",":","`","update","`","\u51fd\u6570\u65f6\u4f20\u5165\u7684\u53c2\u6570\u51b3\u5b9a\u5177\u4f53\u7684","backend","\uff0c\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u76f4\u63a5\u4f7f\u7528","``","'auto","'","``","\u5373\u53ef\u3002",":","param","aggregate_when_get_metric",":","\u5728\u8ba1\u7b97","metric","\u7684\u65f6\u5019\u662f\u5426\u81ea\u52a8\u5c06\u5404\u4e2a\u8fdb\u7a0b\u4e0a\u7684\u76f8\u540c\u7684","element","\u7684\u6570\u5b57\u805a\u5408\u540e\u518d\u5f97\u5230","metric\uff0c","\u5f53","backend","\u4e0d\u652f\u6301\u5206\u5e03\u5f0f\u65f6\uff0c\u8be5\u53c2\u6570\u65e0\u610f\u4e49\u3002\u5982\u679c\u4e3a","``","None","``","\uff0c\u5c06\u5728",":","class",":","`","~fastNLP.core.controllers.Evaluator","`","\u4e2d\u6839\u636e","sampler","\u662f\u5426\u4f7f\u7528\u5206\u5e03\u5f0f\u8fdb\u884c\u81ea\u52a8\u8bbe\u7f6e\u3002"]
docstr: :meth:`update` 函数将针对一个批次的预测结果做评价指标的累计。
  
  :param pred: 预测的结果，大小为 ``[batch, seq_len]`` 或者 ``[batch, seq_len, len(tag_vocab)]``
  :param target: 真实值，大小为 ``[batch, seq_len]``
  :param seq_len: 文本长度标记，大小为 ``[batch]``
  :return:
docstr_tok: [":","meth",":","`","update","`","\u51fd\u6570\u5c06\u9488\u5bf9\u4e00\u4e2a\u6279\u6b21\u7684\u9884\u6d4b\u7ed3\u679c\u505a\u8bc4\u4ef7\u6307\u6807\u7684\u7d2f\u8ba1\u3002",":","param","pred",":","\u9884\u6d4b\u7684\u7ed3\u679c\uff0c\u5927\u5c0f\u4e3a","``","[","batch",",","seq_len","]","``","\u6216\u8005","``","[","batch",",","seq_len",",","len","(","tag_vocab",")","]","``",":","param","target",":","\u771f\u5b9e\u503c\uff0c\u5927\u5c0f\u4e3a","``","[","batch",",","seq_len","]","``",":","param","seq_len",":","\u6587\u672c\u957f\u5ea6\u6807\u8bb0\uff0c\u5927\u5c0f\u4e3a","``","[","batch","]","``",":","return",":"]
code: def update(self, pred, target, seq_len) -> None:
      r"""
      :meth:`update` 函数将针对一个批次的预测结果做评价指标的累计。
  
      :param pred: 预测的结果，大小为 ``[batch, seq_len]`` 或者 ``[batch, seq_len, len(tag_vocab)]``
      :param target: 真实值，大小为 ``[batch, seq_len]``
      :param seq_len: 文本长度标记，大小为 ``[batch]``
      :return:
      """
      pred = self.tensor2numpy(pred)
      target = self.tensor2numpy(target)
  
      if pred.ndim == target.ndim and target.ndim == 2:
          pass
  
      elif pred.ndim == target.ndim + 1 and target.ndim == 2:
          num_classes = pred.shape[-1]
          pred = pred.argmax(axis=-1)
          if (target >= num_classes).any():
              raise ValueError(
                  "A gold label passed to SpanBasedF1Metric contains an "
                  "id >= {}, the number of classes.".format(num_classes)
              )
      else:
          raise RuntimeError(
              f"when pred have size:{pred.ndim}, target should have size: {pred.ndim} or "
              f"{pred.shape[:-1]}, got {target.ndim}."
          )
  
      batch_size = pred.shape[0]
      pred = pred.tolist()
      target = target.tolist()
      for i in range(batch_size):
          pred_tags = pred[i][: int(seq_len[i])]
          gold_tags = target[i][: int(seq_len[i])]
  
          pred_str_tags = [
              self.tag_vocab.to_word(tag) for tag in pred_tags
          ]
          gold_str_tags = [
              self.tag_vocab.to_word(tag) for tag in gold_tags
          ]
  
          pred_spans = self.tag_to_span_func(
              pred_str_tags, ignore_labels=self.ignore_labels
          )
          gold_spans = self.tag_to_span_func(
              gold_str_tags, ignore_labels=self.ignore_labels
          )
  
          for span in pred_spans:
              if span in gold_spans:
                  self._tp[span[0]] += 1
                  gold_spans.remove(span)
              else:
                  self._fp[span[0]] += 1
          for span in gold_spans:
              self._fn[span[0]] += 1
  
file_path: fastNLP/fastNLP/core/metrics/span_f1_pre_rec_metric.py
imports: from typing import Union, List, Optional
  from collections import Counter
  from fastNLP.core.metrics.backend import Backend
  from fastNLP.core.metrics.metric import Metric
  from fastNLP.core.vocabulary import Vocabulary
  from fastNLP.core.log import logger
  from .utils import _compute_f_pre_rec
label_desc: Use image node_id 4 for calling the SpanFPreRecMetric obj's underlying member method code with example usage: obj.update(pred, target, seq_len) without return types