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.8 kB

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