class_docstr: 计算分类结果 **F值** 的 **Metric** 。
  
  :param tag_vocab: 标签的 :class:`~fastNLP.core.Vocabulary` 。 默认值为 ``None``。若为 ``None`` 则使用数字来作为标签内容，
      否则使用 vocab 来作为标签内容
  :param ignore_labels: :class:`str` 组成的 :class:`list`. 这个 :class:`list` 中的 class 不会被用于计算。例如在 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: ["\u8ba1\u7b97\u5206\u7c7b\u7ed3\u679c","*","*","F\u503c","*","*","\u7684","*","*","Metric","*","*","\u3002",":","param","tag_vocab",":","\u6807\u7b7e\u7684",":","class",":","`","~fastNLP.core.Vocabulary","`","\u3002","\u9ed8\u8ba4\u503c\u4e3a","``","None","``","\u3002\u82e5\u4e3a","``","None","``","\u5219\u4f7f\u7528\u6570\u5b57\u6765\u4f5c\u4e3a\u6807\u7b7e\u5185\u5bb9\uff0c","\u5426\u5219\u4f7f\u7528","vocab","\u6765\u4f5c\u4e3a\u6807\u7b7e\u5185\u5bb9",":","param","ignore_labels",":",":","class",":","`","str","`","\u7ec4\u6210\u7684",":","class",":","`","list","`",".","\u8fd9\u4e2a",":","class",":","`","list","`","\u4e2d\u7684","class","\u4e0d\u4f1a\u88ab\u7528\u4e8e\u8ba1\u7b97\u3002\u4f8b\u5982\u5728","POS","tagging","\u65f6\u4f20\u5165","``","[","'NN","'","]","``","\uff0c","\u5219\u4e0d\u4f1a\u8ba1\u7b97","'NN","'","\u4e2a","label",":","param","only_gross",":","\u662f\u5426\u53ea\u8ba1\u7b97\u603b\u7684","``","f1","``",",","``","precision","``",",","``","recall","``","\u7684\u503c\uff1b\u5982\u679c\u4e3a","``","False","``","\uff0c\u4e0d\u4ec5\u8fd4\u56de\u603b\u7684","``","f1","``",",","``","pre","``",",","``","rec","``",",","\u8fd8\u4f1a\u8fd4\u56de\u6bcf\u4e2a","label","\u7684","``","f1","``",",","``","pre","``",",","``","rec","``",":","param","f_type",":","`","micro","`","\u6216","`","macro","`","\u3002","*","`","micro","`",":","\u901a\u8fc7\u5148\u8ba1\u7b97\u603b\u4f53\u7684","TP\uff0cFN","\u548c","FP","\u7684\u6570\u91cf\uff0c\u518d\u8ba1\u7b97","f",",","precision",",","recall",";","*","`","macro","`",":","\u5206\u5e03\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",":","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:`get_metric` 函数将根据 :meth:`update` 函数累计的评价指标统计量来计算最终的评价结果。
  
  :return: 包含以下内容的字典：``{"acc": float}``
docstr_tok: [":","meth",":","`","get_metric","`","\u51fd\u6570\u5c06\u6839\u636e",":","meth",":","`","update","`","\u51fd\u6570\u7d2f\u8ba1\u7684\u8bc4\u4ef7\u6307\u6807\u7edf\u8ba1\u91cf\u6765\u8ba1\u7b97\u6700\u7ec8\u7684\u8bc4\u4ef7\u7ed3\u679c\u3002",":","return",":","\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\u7684\u5b57\u5178\uff1a","``","{","``","acc","''",":","float","}","``"]
code: def get_metric(self) -> dict:
      r"""
      :meth:`get_metric` 函数将根据 :meth:`update` 函数累计的评价指标统计量来计算最终的评价结果。
  
      :return: 包含以下内容的字典：``{"acc": float}``
      """
      evaluate_result = {}
  
      # 通过 all_gather_object 将各个卡上的结果收集过来，并加和。
      ls = self.all_gather_object([self._tp, self._fp, self._fn])
      tps, fps, fns = zip(*ls)
      _tp, _fp, _fn = Counter(), Counter(), Counter()
      for c, cs in zip([_tp, _fp, _fn], [tps, fps, fns]):
          for _c in cs:
              c.update(_c)
  
      if not self.only_gross or self.f_type == "macro":
          tags = set(_fn.keys())
          tags.update(set(_fp.keys()))
          tags.update(set(_tp.keys()))
          f_sum = 0
          pre_sum = 0
          rec_sum = 0
          for tag in tags:
              if self.tag_vocab is not None:
                  tag_name = self.tag_vocab.to_word(tag)
              else:
                  tag_name = int(tag)
              tp = _tp[tag]
              fn = _fn[tag]
              fp = _fp[tag]
              if tp == fn == fp == 0:
                  continue
              f, pre, rec = _compute_f_pre_rec(
                  self.beta_square, tp, fn, fp
              )
              f_sum += f
              pre_sum += pre
              rec_sum += rec
              if not self.only_gross and tag != "":  # tag!=''防止无tag的情况
                  f_key = "f-{}".format(tag_name)
                  pre_key = "pre-{}".format(tag_name)
                  rec_key = "rec-{}".format(tag_name)
                  evaluate_result[f_key] = f
                  evaluate_result[pre_key] = pre
                  evaluate_result[rec_key] = rec
  
          if self.f_type == "macro":
              evaluate_result["f"] = f_sum / len(tags)
              evaluate_result["pre"] = pre_sum / len(tags)
              evaluate_result["rec"] = rec_sum / len(tags)
  
      if self.f_type == "micro":
          f, pre, rec = _compute_f_pre_rec(
              self.beta_square,
              sum(_tp.values()),
              sum(_fn.values()),
              sum(_fp.values()),
          )
          evaluate_result["f"] = f
          evaluate_result["pre"] = pre
          evaluate_result["rec"] = rec
  
      for key, value in evaluate_result.items():
          evaluate_result[key] = round(value, 6)
  
      return evaluate_result
  
file_path: fastNLP/fastNLP/core/metrics/classify_f1_pre_rec_metric.py
imports: from typing import Union, List
  from collections import Counter
  import numpy
  from .metric import Metric
  from .backend import Backend
  from fastNLP.core.vocabulary import Vocabulary
  from fastNLP.core.utils.seq_len_to_mask import seq_len_to_mask
  from .utils import _compute_f_pre_rec
  from fastNLP.core.log import logger
label_desc: Use image node_id 3 for calling the ClassifyFPreRecMetric obj's underlying member method code with example usage: obj.get_metric() and returns: evaluate_result