From 19edbac71fe3b7d8670f381463f9344af4e24824 Mon Sep 17 00:00:00 2001 From: Jiaqi Date: Tue, 30 Mar 2021 16:56:15 +0800 Subject: [PATCH] fix api bugs --- mindspore/nn/loss/loss.py | 24 +++++++++++-------- mindspore/nn/metrics/bleu_score.py | 1 + mindspore/nn/metrics/confusion_matrix.py | 4 ++-- mindspore/nn/metrics/occlusion_sensitivity.py | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/mindspore/nn/loss/loss.py b/mindspore/nn/loss/loss.py index 5c471a126c..6caf65eca4 100644 --- a/mindspore/nn/loss/loss.py +++ b/mindspore/nn/loss/loss.py @@ -436,7 +436,7 @@ class DiceLoss(_Loss): >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32) >>> output = loss(y_pred, y) >>> print(output) - [0.38596618] + 0.38596618 """ def __init__(self, smooth=1e-5): super(DiceLoss, self).__init__() @@ -452,7 +452,7 @@ class DiceLoss(_Loss): single_dice_coeff = (2 * intersection) / (unionset + self.smooth) dice_loss = 1 - single_dice_coeff - return dice_loss.mean() + return dice_loss @constexpr @@ -1037,7 +1037,11 @@ class FocalLoss(_Loss): r""" The loss function proposed by Kaiming team in their paper ``Focal Loss for Dense Object Detection`` improves the effect of image object detection. It is a loss function to solve the imbalance of categories and the difference of - classification difficulty. + classification difficulty. If you want to learn more, please refer to the paper. + `https://arxiv.org/pdf/1708.02002.pdf`. The function is shown as follows: + + .. math:: + FL(p_t) = -(1-p_t)^\gamma log(p_t) Args: gamma (float): Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0. @@ -1048,19 +1052,19 @@ class FocalLoss(_Loss): Inputs: - **predict** (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). Where C is the number - of classes. Its value is greater than 1. If the shape is (B, C, H, W) or (B, C, H), the H or product of H - and W should be the same as target. + of classes. Its value is greater than 1. If the shape is (B, C, H, W) or (B, C, H), the H or product of H + and W should be the same as target. - **target** (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). The value of C is 1 or - it needs to be the same as predict's C. If C is not 1, the shape of target should be the same as that of - predict, where C is the number of classes. If the shape is (B, C, H, W) or (B, C, H), the H or product of H - and W should be the same as predict. + it needs to be the same as predict's C. If C is not 1, the shape of target should be the same as that of + predict, where C is the number of classes. If the shape is (B, C, H, W) or (B, C, H), the H or product of H + and W should be the same as predict. Outputs: Tensor, it's a tensor with the same shape and type as input `predict`. Raises: TypeError: If the data type of ``gamma`` is not float.. - TypeError: If ``weight`` is not a Parameter. + TypeError: If ``weight`` is not a Tensor. ValueError: If ``target`` dim different from ``predict``. ValueError: If ``target`` channel is not 1 and ``target`` shape is different from ``predict``. ValueError: If ``reduction`` is not one of 'none', 'mean', 'sum'. @@ -1074,7 +1078,7 @@ class FocalLoss(_Loss): >>> focalloss = nn.FocalLoss(weight=Tensor([1, 2]), gamma=2.0, reduction='mean') >>> output = focalloss(predict, target) >>> print(output) - 1.6610543 + 0.12516622 """ def __init__(self, weight=None, gamma=2.0, reduction='mean'): diff --git a/mindspore/nn/metrics/bleu_score.py b/mindspore/nn/metrics/bleu_score.py index 812fd96bf8..2d9f3f9d1c 100644 --- a/mindspore/nn/metrics/bleu_score.py +++ b/mindspore/nn/metrics/bleu_score.py @@ -35,6 +35,7 @@ class BleuScore(Metric): >>> metric.clear() >>> metric.update(candidate_corpus, reference_corpus) >>> bleu_score = metric.eval() + >>> print(bleu_score) 0.5946035575013605 """ def __init__(self, n_gram=4, smooth=False): diff --git a/mindspore/nn/metrics/confusion_matrix.py b/mindspore/nn/metrics/confusion_matrix.py index 6a1cb40752..7ec0d1ef4e 100644 --- a/mindspore/nn/metrics/confusion_matrix.py +++ b/mindspore/nn/metrics/confusion_matrix.py @@ -167,7 +167,7 @@ class ConfusionMatrixMetric(Metric): Examples: >>> metric = ConfusionMatrixMetric(skip_channel=True, metric_name="tpr", - >>> calculation_method=False, decrease="mean") + ... calculation_method=False, decrease="mean") >>> metric.clear() >>> x = Tensor(np.array([[[0], [1]], [[1], [0]]])) >>> y = Tensor(np.array([[[0], [1]], [[0], [1]]])) @@ -176,7 +176,7 @@ class ConfusionMatrixMetric(Metric): >>> y = Tensor(np.array([[[0], [1]], [[1], [0]]])) >>> avg_output = metric.eval() >>> print(avg_output) - [0.75] + [0.5] """ def __init__(self, skip_channel=True, diff --git a/mindspore/nn/metrics/occlusion_sensitivity.py b/mindspore/nn/metrics/occlusion_sensitivity.py index 24f991a861..a0afb71ac5 100644 --- a/mindspore/nn/metrics/occlusion_sensitivity.py +++ b/mindspore/nn/metrics/occlusion_sensitivity.py @@ -49,7 +49,7 @@ class OcclusionSensitivity(Metric): Example: >>> class DenseNet(nn.Cell): >>> def __init__(self): - >>> super(DenseNet, self).init() + >>> super(DenseNet, self).__init__() >>> w = np.array([[0.1, 0.8, 0.1, 0.1],[1, 1, 1, 1]]).astype(np.float32) >>> b = np.array([0.3, 0.6]).astype(np.float32) >>> self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b))