You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mindspore.nn.Accuracy.rst 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. mindspore.nn.Accuracy
  2. =====================
  3. .. py:class:: mindspore.nn.Accuracy(eval_type='classification')
  4. 计算'classification'单标签数据分类和'multilabel'多标签数据分类的正确率。
  5. 此类创建两个局部变量,预测正确的样本数和总样本数,用于计算预测值 `y_pred` 和真实标签 `y` 的匹配频率。
  6. 此频率最终作为正确率返回:是一个将预测正确的数目除以总数的幂等操作。
  7. .. math::
  8. \text{accuracy} =\frac{\text{true_positive} + \text{true_negative}}
  9. {\text{true_positive} + \text{true_negative} + \text{false_positive} + \text{false_negative}}
  10. **参数:**
  11. - **eval_type** (str) - 评估的数据集的类型,支持'classification'和'multilabel'。'classification'为单标签分类场景,'multilabel'为多标签分类场景。
  12. 默认值:'classification'。
  13. **示例:**
  14. >>> import numpy as np
  15. >>> from mindspore import nn, Tensor
  16. >>>
  17. >>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mindspore.float32)
  18. >>> y = Tensor(np.array([1, 0, 1]), mindspore.float32)
  19. >>> metric = nn.Accuracy('classification')
  20. >>> metric.clear()
  21. >>> metric.update(x, y)
  22. >>> accuracy = metric.eval()
  23. >>> print(accuracy)
  24. 0.6666666666666666
  25. .. py:method:: clear()
  26. 内部评估结果清零。
  27. .. py:method:: eval()
  28. 计算正确率。
  29. **返回:**
  30. Float,计算的结果。
  31. **异常:**
  32. - **RuntimeError** - 样本量为0。
  33. .. py:method:: update(*inputs)
  34. 更新局部变量。计算预测值y_pred和标签y的匹配频率。
  35. 对于'classification',如果预测的最大值的索引匹配真实的标签,预测正确;对于'multilabel',如果预测值与真实标签匹配,预测正确。
  36. **参数:**
  37. - **inputs** - 预测值 `y_pred` 和真实标签 `y` ,`y_pred` 和 `y` 支持Tensor、list或numpy.ndarray类型。
  38. 对于'classification'情况,`y_pred` 在大多数情况下由范围 :math:`[0, 1]` 中的浮点数组成,shape为 :math:`(N, C)` ,其中 :math:`N` 是样本数, :math:`C` 是类别数。
  39. `y` 由整数值组成,如果是one_hot编码格式,shape是 :math:`(N,C)` ;如果是类别索引,shape是 :math:`(N,)` 。
  40. 对于'multilabel'情况,`y_pred` 和 `y` 只能是值为0或1的one-hot编码格式,其中值为1的索引表示正类别。 `y_pred` 和 `y` 的shape都是 :math:`(N,C)` 。
  41. **异常:**
  42. - **ValueError** - inputs的数量不等于2。