!48955 [bugfix][api] Fix I6FCIG

Merge pull request !48955 from shaojunsong/fix/I6FCIG
This commit is contained in:
i-robot 2023-02-18 02:38:49 +00:00 committed by Gitee
commit 978cfb6392
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 34 additions and 22 deletions

View File

@ -24,7 +24,7 @@ mindspore.nn.HingeEmbeddingLoss
其中 :math:`L = \{l_1,\dots,l_N\}^\top` 其中 :math:`L = \{l_1,\dots,l_N\}^\top`
参数: 参数:
- **margin** (float) - Hinge Embedding Loss公式定义的阈值 :math:`margin`。公式中表示为 :math:`\Delta`。默认值1.0。 - **margin** (float, int) - Hinge Embedding Loss公式定义的阈值 :math:`margin`。公式中表示为 :math:`\Delta`。默认值1.0。
- **reduction** (str) - 指定应用于输出结果的计算方式,'none'、'mean'、'sum',默认值:'mean'。 - **reduction** (str) - 指定应用于输出结果的计算方式,'none'、'mean'、'sum',默认值:'mean'。
输入: 输入:
@ -35,8 +35,8 @@ mindspore.nn.HingeEmbeddingLoss
Tensor或Tensor scalar根据 :math:`reduction` 计算的loss。 Tensor或Tensor scalar根据 :math:`reduction` 计算的loss。
异常: 异常:
- **TypeError** - `logits` 不是Tensor。 - **TypeError** - `logits` 不是数据类型为float的Tensor。
- **TypeError** - `labels` 不是Tensor。 - **TypeError** - `labels` 不是数据类型为float的Tensor。
- **TypeError** - `margin` 不是float。 - **TypeError** - `margin` 不是float或int
- **ValueError** - `labels``logits` shape不一致。 - **ValueError** - `labels``logits` shape不一致。
- **ValueError** - `reduction` 不是"none"、"mean"或者"sum"。 - **ValueError** - `reduction` 不是"none"、"mean"或者"sum"。

View File

@ -26,15 +26,15 @@ mindspore.ops.hinge_embedding_loss
参数: 参数:
- **inputs** (Tensor) - 预测值,公式中表示为 :math:`x`shape为 :math:`(*)``*` 代表着任意数量的维度。 - **inputs** (Tensor) - 预测值,公式中表示为 :math:`x`shape为 :math:`(*)``*` 代表着任意数量的维度。
- **targets** (Tensor) - 标签值,公式中表示为 :math:`y`,和 `logits` 具有相同shape包含1或-1。 - **targets** (Tensor) - 标签值,公式中表示为 :math:`y`,和 `logits` 具有相同shape包含1或-1。
- **margin** (float) - Hinge Embedding Loss公式定义的阈值 :math:`margin`。公式中表示为 :math:`\Delta`。默认值1.0。 - **margin** (float, int) - Hinge Embedding Loss公式定义的阈值 :math:`margin`。公式中表示为 :math:`\Delta`。默认值1.0。
- **reduction** (str) - 指定应用于输出结果的计算方式,'none'、'mean'、'sum',默认值:'mean'。 - **reduction** (str) - 指定应用于输出结果的计算方式,'none'、'mean'、'sum',默认值:'mean'。
返回: 返回:
Tensor或Tensor scalar根据 :math:`reduction` 计算的loss。 Tensor或Tensor scalar根据 :math:`reduction` 计算的loss。
异常: 异常:
- **TypeError** - `inputs` 不是Tensor。 - **TypeError** - `inputs` 不是数据类型为float的Tensor。
- **TypeError** - `targets` 不是Tensor。 - **TypeError** - `targets` 不是数据类型为float的Tensor。
- **TypeError** - `margin` 不是float。 - **TypeError** - `margin` 不是float或者int
- **ValueError** - `inputs``targets` shape不一致。 - **ValueError** - `inputs``targets` shape不一致。
- **ValueError** - `reduction` 不是"none"、"mean"或者"sum"。 - **ValueError** - `reduction` 不是"none"、"mean"或者"sum"。

View File

@ -2492,7 +2492,7 @@ class HingeEmbeddingLoss(LossBase):
where :math:`L = \{l_1,\dots,l_N\}^\top`. where :math:`L = \{l_1,\dots,l_N\}^\top`.
Args: Args:
margin (float): Threshold defined by Hinge Embedding Loss :math:`margin`. margin (float, int): Threshold defined by Hinge Embedding Loss :math:`margin`.
Represented as :math:`\Delta` in the formula. Default: 1.0. Represented as :math:`\Delta` in the formula. Default: 1.0.
reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'. reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'.
Default: 'mean'. Default: 'mean'.
@ -2505,9 +2505,9 @@ class HingeEmbeddingLoss(LossBase):
Tensor or Tensor scalar, the computed loss depending on `reduction`. Tensor or Tensor scalar, the computed loss depending on `reduction`.
Raises: Raises:
TypeError: If `logits` is not a Tensor. TypeError: If `logits` is not a Tensor of floats.
TypeError: If `labels` is not a Tensor. TypeError: If `labels` is not a Tensor of floats.
TypeError: If `margin` is not a float. TypeError: If `margin` is not a float or int.
ValueError: If `labels` does not have the same shape as `logits`. ValueError: If `labels` does not have the same shape as `logits`.
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'. ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
@ -2531,7 +2531,7 @@ class HingeEmbeddingLoss(LossBase):
def __init__(self, margin=1.0, reduction='mean'): def __init__(self, margin=1.0, reduction='mean'):
super(HingeEmbeddingLoss, self).__init__() super(HingeEmbeddingLoss, self).__init__()
validator.check_value_type('margin', margin, [float], self.cls_name) validator.check_value_type('margin', margin, [float, int], self.cls_name)
validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', self.cls_name) validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', self.cls_name)
self.margin = margin self.margin = margin
self.reduction = reduction self.reduction = reduction

View File

@ -1731,7 +1731,7 @@ def signbit(x):
def sgn(x): def sgn(x):
r""" r"""
Extension of :func:`mindspore.ops.sign` in the plural. Extension of :func:`mindspore.ops.sign` in complex domain.
For real number input, this function is the same as :func:`mindspore.ops.sign`. For real number input, this function is the same as :func:`mindspore.ops.sign`.
For complex input, this function is calculated according to the following formula. For complex input, this function is calculated according to the following formula.
@ -7185,7 +7185,7 @@ def norm(A, ord=None, dim=None, keepdim=False, *, dtype=None):
====================== ================================ ========================================== ====================== ================================ ==========================================
.. note:: .. note::
Currently, plural is not supported. Currently, complex numbers are not supported.
Args: Args:
A (Tensor): Tensor of shape :math:`(*, n)` or :math:`(*, m, n)` where * is zero or more batch dimensions. A (Tensor): Tensor of shape :math:`(*, n)` or :math:`(*, m, n)` where * is zero or more batch dimensions.

View File

@ -4090,7 +4090,7 @@ def hinge_embedding_loss(inputs, targets, margin=1.0, reduction='mean'):
Args: Args:
inputs (Tensor): Tensor of shape :math:`(*)` where :math:`*` means any number of dimensions. inputs (Tensor): Tensor of shape :math:`(*)` where :math:`*` means any number of dimensions.
targets (Tensor): Same shape as the logits, contains -1 or 1. targets (Tensor): Same shape as the logits, contains -1 or 1.
margin (float): Threshold defined by Hinge Embedding Loss :math:`margin`. margin (float, int): Threshold defined by Hinge Embedding Loss :math:`margin`.
Represented as :math:`\Delta` in the formula. Default: 1.0. Represented as :math:`\Delta` in the formula. Default: 1.0.
reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'. reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'.
Default: 'mean'. Default: 'mean'.
@ -4099,9 +4099,9 @@ def hinge_embedding_loss(inputs, targets, margin=1.0, reduction='mean'):
Tensor or Tensor scalar, the computed loss depending on `reduction`. Tensor or Tensor scalar, the computed loss depending on `reduction`.
Raises: Raises:
TypeError: If `inputs` is not a Tensor. TypeError: If `inputs` is not a Tensor of floats.
TypeError: If `targets` is not a Tensor. TypeError: If `targets` is not a Tensor of floats.
TypeError: If `margin` is not a float. TypeError: If `margin` is not a float or int.
ValueError: If `targets` does not have the same shape as `inputs`. ValueError: If `targets` does not have the same shape as `inputs`.
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'. ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
@ -4121,13 +4121,25 @@ def hinge_embedding_loss(inputs, targets, margin=1.0, reduction='mean'):
>>> print(loss) >>> print(loss)
0.16666666 0.16666666
""" """
_dtype = inputs.dtype if not isinstance(margin, (float, int)):
min_val = Tensor(0, _dtype) raise TypeError(f"For 'HingeEmbeddingLoss', 'margin' must be a float or int, but got {type(margin)}.")
if reduction not in ['none', 'mean', 'sum']:
raise ValueError(f"For 'HingeEmbeddingLoss', 'reduction' must be one of 'none', 'mean', 'sum',"
f"but got {reduction}.")
inputs_dtype = inputs.dtype
targets_dtype = targets.dtype
if inputs_dtype not in mstype.float_type:
raise TypeError(f"For 'HingeEmbeddingLoss', the dtype of the first input must be float, but got "
f"{inputs_dtype}.")
if targets_dtype not in mstype.float_type:
raise TypeError(f"For 'HingeEmbeddingLoss', the dtype of the second input must be float, but got "
f"{targets_dtype}.")
min_val = Tensor(0, inputs_dtype)
pos_index = targets > 0 pos_index = targets > 0
neg_index = targets < 0 neg_index = targets < 0
pos = pos_index * inputs pos = pos_index * inputs
neg = neg_index * inputs neg = neg_index * inputs
m = ops.cast(margin, _dtype) m = ops.cast(margin, inputs_dtype)
margin_matrix = m * neg_index margin_matrix = m * neg_index
neg = margin_matrix - neg neg = margin_matrix - neg
neg = ops.clip_by_value(neg, min_val) neg = ops.clip_by_value(neg, min_val)