Lliutongtongcode check for master # Conflicts: # mindspore/common/initializer.py # mindspore/nn/cell.py # # 似乎您正在做一个拣选提交。如果不对,请删除文件 # .git/CHERRY_PICK_HEAD # 然后重试。 # 请为您的变更输入提交说明。以 '#' 开始的行将被忽略,而一个空的提交 # 说明将会终止提交。 # # 日期: Fri Aug 13 18:40:19 2021 +0800 # # 位于分支 code_review_r1.3 # 您的分支与上游分支 'ma/r1.3' 一致。 # # 您在执行拣选提交 ffda6be35c 的操作。 # # 要提交的变更: # 修改: mindspore/common/__init__.py # 修改: mindspore/common/_register_for_tensor.py # 修改: mindspore/common/api.py # 修改: mindspore/common/dtype.py # 修改: mindspore/common/initializer.py # 修改: mindspore/common/monad.py # 修改: mindspore/common/parameter.py # 修改: mindspore/common/seed.py # 修改: mindspore/common/tensor.py # 修改: mindspore/nn/cell.py # 修改: mindspore/nn/metrics/__init__.py # 修改: mindspore/nn/metrics/confusion_matrix.py # 修改: mindspore/nn/metrics/error.py # 修改: mindspore/nn/metrics/fbeta.py # 修改: mindspore/nn/metrics/loss.py # 修改: mindspore/nn/metrics/metric.py # 修改: mindspore/nn/metrics/precision.py # 修改: mindspore/nn/metrics/recall.py # 修改: mindspore/nn/metrics/topk.py # 修改: mindspore/train/callback/_checkpoint.py # 修改: mindspore/train/model.py # 修改: mindspore/train/serialization.py # # Conflicts: # mindspore/common/api.py # mindspore/common/initializer.py # mindspore/nn/metrics/confusion_matrix.py # # 似乎您正在做一个拣选提交。如果不对,请删除文件 # .git/CHERRY_PICK_HEAD # 然后重试。 # 请为您的变更输入提交说明。以 '#' 开始的行将被忽略,而一个空的提交 # 说明将会终止提交。 # # 日期: Fri Aug 13 18:40:19 2021 +0800 # # 位于分支 code_review_master # 您的分支与上游分支 'ma/master' 一致。 # # 您在执行拣选提交 743f9fbff3 的操作。 # # 要提交的变更: # 修改: mindspore/common/__init__.py # 修改: mindspore/common/_monad.py # 修改: mindspore/common/_register_for_tensor.py # 修改: mindspore/common/api.py # 修改: mindspore/common/dtype.py # 修改: mindspore/common/initializer.py # 修改: mindspore/common/parameter.py # 修改: mindspore/common/seed.py # 修改: mindspore/common/tensor.py # 修改: mindspore/nn/cell.py # 修改: mindspore/nn/metrics/__init__.py # 修改: mindspore/nn/metrics/confusion_matrix.py # 修改: mindspore/nn/metrics/error.py # 修改: mindspore/nn/metrics/fbeta.py # 修改: mindspore/nn/metrics/loss.py # 修改: mindspore/nn/metrics/metric.py # 修改: mindspore/nn/metrics/precision.py # 修改: mindspore/nn/metrics/recall.py # 修改: mindspore/nn/metrics/topk.py # 修改: mindspore/train/callback/_checkpoint.py # 修改: mindspore/train/model.py # 修改: mindspore/train/serialization.py #
077f10b7创建于 2021年8月30日历史提交
# Copyright 2020-2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Topk."""
import numpy as np
from .metric import Metric, rearrange_inputs


class TopKCategoricalAccuracy(Metric):
    """
    Calculates the top-k categorical accuracy.

    Note:
        The method `update` must receive input of the form :math:`(y_{pred}, y)`. If some samples have
        the same accuracy, the first sample will be chosen.

    Args:
        k (int): Specifies the top-k categorical accuracy to compute.

    Raises:
        TypeError: If `k` is not int.
        ValueError: If `k` is less than 1.

    Examples:
        >>> import numpy as np
        >>> from mindspore import nn, Tensor
        >>>
        >>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
        ...         [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
        >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
        >>> topk = nn.TopKCategoricalAccuracy(3)
        >>> topk.clear()
        >>> topk.update(x, y)
        >>> output = topk.eval()
        >>> print(output)
        0.6666666666666666
    """
    def __init__(self, k):
        super(TopKCategoricalAccuracy, self).__init__()
        if not isinstance(k, int):
            raise TypeError('k should be integer type, but got {}'.format(type(k)))
        if k < 1:
            raise ValueError('k must be at least 1, but got {}'.format(k))
        self.k = k
        self.clear()

    def clear(self):
        """Clear the internal evaluation result."""
        self._correct_num = 0
        self._samples_num = 0

    @rearrange_inputs
    def update(self, *inputs):
        """
        Updates the internal evaluation result y_pred and y.

        Args:
            inputs: Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray.
                y_pred is in most cases (not strictly) a list of floating numbers in range :math:`[0, 1]`
                and the shape is :math:`(N, C)`, where :math:`N` is the number of cases and :math:`C`
                is the number of categories. y contains values of integers. The shape is :math:`(N, C)`
                if one-hot encoding is used. Shape can also be :math:`(N,)` if category index is used.
        """
        if len(inputs) != 2:
            raise ValueError('The topk needs 2 inputs (y_pred, y), but got {}'.format(len(inputs)))

        y_pred = self._convert_data(inputs[0])
        y = self._convert_data(inputs[1])
        if y_pred.ndim == y.ndim and self._check_onehot_data(y):
            y = y.argmax(axis=1)
        indices = np.argsort(-y_pred, axis=1)[:, :self.k]
        repeated_y = y.reshape(-1, 1).repeat(self.k, axis=1)
        correct = np.equal(indices, repeated_y).sum(axis=1)
        self._correct_num += correct.sum()
        self._samples_num += repeated_y.shape[0]

    def eval(self):
        """
        Computes the top-k categorical accuracy.

        Returns:
            Float, computed result.
        """
        if self._samples_num == 0:
            raise RuntimeError('The total number of samples must not be 0.')
        return self._correct_num / self._samples_num


class Top1CategoricalAccuracy(TopKCategoricalAccuracy):
    """
    Calculates the top-1 categorical accuracy. This class is a specialized class for TopKCategoricalAccuracy.
    Refer to :class:`TopKCategoricalAccuracy` for more details.

    Examples:
        >>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
        ...         [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
        >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
        >>> topk = nn.Top1CategoricalAccuracy()
        >>> topk.clear()
        >>> topk.update(x, y)
        >>> output = topk.eval()
        >>> print(output)
        0.0
    """
    def __init__(self):
        super(Top1CategoricalAccuracy, self).__init__(1)


class Top5CategoricalAccuracy(TopKCategoricalAccuracy):
    """
    Calculates the top-5 categorical accuracy. This class is a specialized class for TopKCategoricalAccuracy.
    Refer to :class:`TopKCategoricalAccuracy` for more details.

    Examples:
        >>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
        ...            [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
        >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
        >>> topk = nn.Top5CategoricalAccuracy()
        >>> topk.clear()
        >>> topk.update(x, y)
        >>> output = topk.eval()
        >>> print(output)
        1.0
    """
    def __init__(self):
        super(Top5CategoricalAccuracy, self).__init__(5)