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.Metric.rst 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. mindspore.nn.Metric
  2. ====================
  3. .. py:class:: mindspore.nn.Metric
  4. 用于计算评估指标的基类。
  5. 在计算评估指标时需要调用 `clear` 、 `update` 和 `eval` 三个方法,在继承该类自定义评估指标时,也需要实现这三个方法。其中,`update` 用于计算中间过程的内部结果,`eval` 用于计算最终评估结果,`clear` 用于重置中间结果。
  6. 请勿直接使用该类,需使用子类如 :class:`mindspore.nn.MAE` 、 :class:`mindspore.nn.Recall` 等。
  7. .. py:method:: clear()
  8. :abstractmethod:
  9. 描述了清除内部评估结果的行为。
  10. .. note::
  11. 所有子类都必须重写此接口。
  12. .. py:method:: eval()
  13. :abstractmethod:
  14. 描述了计算最终评估结果的行为。
  15. .. note::
  16. 所有子类都必须重写此接口。
  17. .. py:method:: indexes
  18. :property:
  19. 获取当前的 `indexes` 值。默认为None,调用 `set_indexes` 可修改 `indexes` 值。
  20. .. py:method:: set_indexes(indexes)
  21. 该接口用于重排 `update` 的输入。
  22. 给定(label0, label1, logits)作为 `update` 的输入,将 `indexes` 设置为[2, 1],则最终使用(logits, label1)作为 `update` 的真实输入。
  23. .. note::
  24. 在继承该类自定义评估函数时,需要用装饰器 `mindspore.nn.rearrange_inputs` 修饰 `update` 方法,否则配置的 `indexes` 值不生效。
  25. **参数:**
  26. - **indexes** (List(int)) - logits和标签的目标顺序。
  27. **输出:**
  28. :class:`Metric` ,类实例本身。
  29. **样例:**
  30. >>> import numpy as np
  31. >>> from mindspore import nn, Tensor
  32. >>>
  33. >>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  34. >>> y = Tensor(np.array([1, 0, 1]))
  35. >>> y2 = Tensor(np.array([0, 0, 1]))
  36. >>> metric = nn.Accuracy('classification').set_indexes([0, 2])
  37. >>> metric.clear()
  38. >>> # indexes为[0, 2],使用x作为预测值,y2作为真实标签
  39. >>> metric.update(x, y, y2)
  40. >>> accuracy = metric.eval()
  41. >>> print(accuracy)
  42. 0.3333333333333333
  43. .. py:method:: update(*inputs)
  44. :abstractmethod:
  45. 描述了更新内部评估结果的行为。
  46. .. note::
  47. 所有子类都必须重写此接口。
  48. **参数:**
  49. - **inputs** - 可变长度输入参数列表。通常是预测值和对应的真实标签。