class_docstr: 计算 准确率 的 metric 。
  
  :param backend: 目前支持五种类型的backend, ``['auto', 'torch', 'paddle', 'jittor', 'oneflow']``。其中 ``'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","\u51c6\u786e\u7387","\u7684","metric","\u3002",":","param","backend",":","\u76ee\u524d\u652f\u6301\u4e94\u79cd\u7c7b\u578b\u7684backend",",","``","[","'auto","'",",","'torch","'",",","'paddle","'",",","'jittor","'",",","'oneflow","'","]","``","\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\u4e00\u822c\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: 预测的 tensor, tensor 的形状可以是 ``[B,]`` 、``[B, n_classes]`` 、
          ``[B, max_len]`` 或 ``[B, max_len, n_classes]``
  :param target: 真实值的 tensor, tensor 的形状可以是 ``[B,]`` 、``[B,]`` 、``[B, max_len]``
      或 ``[B, max_len]``
  :param seq_len: 序列长度标记, 标记的形状可以是 ``None``,  或者 ``[B]`` 。
          如果 mask 也被传进来的话 ``seq_len`` 会被忽略
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","tensor",",","tensor","\u7684\u5f62\u72b6\u53ef\u4ee5\u662f","``","[","B",",","]","``","\u3001","``","[","B",",","n_classes","]","``","\u3001","``","[","B",",","max_len","]","``","\u6216","``","[","B",",","max_len",",","n_classes","]","``",":","param","target",":","\u771f\u5b9e\u503c\u7684","tensor",",","tensor","\u7684\u5f62\u72b6\u53ef\u4ee5\u662f","``","[","B",",","]","``","\u3001","``","[","B",",","]","``","\u3001","``","[","B",",","max_len","]","``","\u6216","``","[","B",",","max_len","]","``",":","param","seq_len",":","\u5e8f\u5217\u957f\u5ea6\u6807\u8bb0",",","\u6807\u8bb0\u7684\u5f62\u72b6\u53ef\u4ee5\u662f","``","None","``",",","\u6216\u8005","``","[","B","]","``","\u3002","\u5982\u679c","mask","\u4e5f\u88ab\u4f20\u8fdb\u6765\u7684\u8bdd","``","seq_len","``","\u4f1a\u88ab\u5ffd\u7565"]
code: def update(self, pred, target, seq_len=None):
      r"""
      :meth:`update` 函数将针对一个批次的预测结果做评价指标的累计。
  
      :param pred: 预测的 tensor, tensor 的形状可以是 ``[B,]`` 、``[B, n_classes]`` 、
              ``[B, max_len]`` 或 ``[B, max_len, n_classes]``
      :param target: 真实值的 tensor, tensor 的形状可以是 ``[B,]`` 、``[B,]`` 、``[B, max_len]``
          或 ``[B, max_len]``
      :param seq_len: 序列长度标记, 标记的形状可以是 ``None``,  或者 ``[B]`` 。
              如果 mask 也被传进来的话 ``seq_len`` 会被忽略
      """
      # 为了兼容不同框架，我们将输入变量全部转为numpy类型来进行计算。
      pred = self.tensor2numpy(pred)
      target = self.tensor2numpy(target)
      if seq_len is not None:
          seq_len = self.tensor2numpy(seq_len)
  
      if seq_len is not None and target.ndim > 1:
          max_len = target.shape[1]
          masks = seq_len_to_mask(seq_len, max_len)
      else:
          masks = None
  
      if pred.ndim == target.ndim:
          if np.prod(pred.shape) != np.prod(target.shape):
              raise RuntimeError(
                  f"when pred have same dimensions with target, they should have same element numbers."
                  f" while target have shape:{target.shape}, "
                  f"pred have shape: {pred.shape}"
              )
  
      elif pred.ndim == target.ndim + 1:
          pred = pred.argmax(axis=-1)
          if seq_len is None and target.ndim > 1:
              logger.warning(
                  "You are not passing `seq_len` to exclude pad when calculate accuracy."
              )
  
      else:
          raise RuntimeError(
              f"when pred have size:{pred.shape}, target should have size: {pred.shape} or "
              f"{pred.shape[:-1]}, got {target.shape}."
          )
  
      if masks is not None:
          self.total += masks.sum().item()
          self.correct += ((pred == target) * masks).sum().item()
      else:
          self.total += np.prod(list(pred.shape)).item()
          self.correct += (target == pred).sum().item()
  
file_path: fastNLP/fastNLP/core/metrics/accuracy.py
imports: from typing import Union
  import numpy
  from fastNLP.core.metrics.metric import Metric
  from fastNLP.core.metrics.backend import Backend
  from fastNLP.core.utils.seq_len_to_mask import seq_len_to_mask
  from fastNLP.core.log import logger
label_desc: Use image node_id 3 for calling the Accuracy obj's underlying member method code with example usage: obj.update(pred, target, seq_len) without return types