!14392 Solve the example problem in comments

From: @lijiaqi0612
Reviewed-by: @kisnwang,@zh_qh,@zh_qh,@zhunaipan
Signed-off-by:
This commit is contained in:
mindspore-ci-bot 2021-04-01 14:44:04 +08:00 committed by Gitee
commit 9580016d46
4 changed files with 18 additions and 13 deletions

View File

@ -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'):

View File

@ -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):

View File

@ -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,

View File

@ -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))