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.TopKCategoricalAccuracy.rst 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. mindspore.nn.TopKCategoricalAccuracy
  2. ====================================
  3. .. py:class:: mindspore.nn.TopKCategoricalAccuracy(k)
  4. 计算top-k分类正确率。
  5. .. note::
  6. `update` 方法需要接收满足 :math:`(y_{pred}, y)` 格式的输入。如果某些样本具有相同的正确率,则将选择第一个样本。
  7. **参数:**
  8. - **k (int)** - 指定要计算的top-k分类正确率。
  9. **异常:**
  10. - **TypeError** - `k` 不是int。
  11. - **ValueError** - `k` 小于1。
  12. **样例:**
  13. >>> import numpy as np
  14. >>> from mindspore import nn, Tensor
  15. >>>
  16. >>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
  17. ... [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
  18. >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
  19. >>> topk = nn.TopKCategoricalAccuracy(3)
  20. >>> topk.clear()
  21. >>> topk.update(x, y)
  22. >>> output = topk.eval()
  23. >>> print(output)
  24. 0.6666666666666666
  25. .. py:method:: clear()
  26. 内部评估结果清零。
  27. .. py:method:: eval()
  28. 计算top-k分类正确率。
  29. **返回:**
  30. numpy.float64,计算结果。
  31. .. py:method:: update(*inputs)
  32. 使用预测值 `y_pred` 和真实标签 `y` 更新局部变量。
  33. **参数:**
  34. - **inputs** - 输入 `y_pred` 和 `y`。`y_pred` 和 `y` 支持Tensor、list或numpy.ndarray类型。
  35. `y_pred` 在大多数情况下由范围 :math:`[0, 1]` 中的浮点数组成,shape为 :math:`(N, C)` ,其中 :math:`N` 是样本数, :math:`C` 是类别数。
  36. `y` 由整数值组成。如果使用one-hot编码,则shape为 :math:`(N, C)` ;如果使用类别索引,shape是 :math:`(N,)` 。