mindspore/docs/api/api_python/nn/mindspore.nn.HingeEmbedding...

43 lines
1.7 KiB
ReStructuredText
Raw Normal View History

2022-10-22 14:26:45 +08:00
mindspore.nn.HingeEmbeddingLoss
===============================
.. py:class:: mindspore.nn.HingeEmbeddingLoss(margin=1.0, reduction="mean")
Hinge Embedding 损失函数。按输入元素计算输出。衡量输入张量x和标签y包含1或-1之间的损失值。通常被用来衡量两个输入之间的相似度。
mini-batch中的第n个样例的损失函数为
.. math::
l_n = \begin{cases}
x_n, & \text{if}\; y_n = 1,\\
\max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1,
\end{cases}
总损失值为:
.. math::
\ell(x, y) = \begin{cases}
2022-10-25 17:02:26 +08:00
\operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
\operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
2022-10-22 14:26:45 +08:00
\end{cases}
其中 :math:`L = \{l_1,\dots,l_N\}^\top`
参数:
- **margin** (float) - Hinge Embedding Loss公式定义的阈值 :math:`margin`。公式中表示为 :math:`\Delta`。默认值1.0。
- **reduction** (str) - 指定应用于输出结果的计算方式,'none'、'mean'、'sum',默认值:'mean'。
输入:
- **logits** (Tensor) - 预测值,公式中表示为 :math:`x`shape为:math:`(*)``*` 代表着任意数量的维度。
- **labels** (Tensor) - 标签值,公式中表示为 :math:`y`,和 `logits` 具有相同shape包含1或-1。
返回:
Tensor或Tensor scalar根据 :math:`reduction` 计算的loss。
异常:
- **TypeError** - `logits` 不是Tensor。
- **TypeError** - `labels` 不是Tensor。
- **TypeError** - `margin` 不是float。
- **ValueError** - `labels``logits` shape不一致。
- **ValueError** - `reduction` 不是"none"、"mean"或者"sum"。