mindspore/docs/api/api_python/mint/mindspore.mint.nn.CosineEmb...

37 lines
2.0 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

mindspore.mint.nn.CosineEmbeddingLoss
=====================================
.. py:class:: mindspore.mint.nn.CosineEmbeddingLoss(margin=0.0, reduction="mean")
余弦相似度损失函数用于测量两个Tensor之间的相似性。
给定两个Tensor :math:`x1`:math:`x2` 以及一个Tensor标签 :math:`y` 正样本的值为1负样本的值为-1公式如下
.. math::
loss(x_1, x_2, y) = \begin{cases}
1-cos(x_1, x_2), & \text{if } y = 1\\
\max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\
\end{cases}
参数:
- **margin** (float可选) - 指定负样本运算中的调节因子,取值范围[-1.0, 1.0],范围外的取值不会报错,但无实际意义。默认值: ``0.0``
- **reduction** (str可选) - 指定应用于输出结果的规约计算方式,可选 ``"none"````"mean"````"sum"`` ,默认值: ``"mean"``
- ``"none"`` :不应用规约方法。
- ``"mean"`` :计算输出元素的平均值。
- ``"sum"`` :计算输出元素的总和。
输入:
- **input1** (Tensor) - 输入Tensorshape为 :math:`(N, D)`:math:`(D)` ,其中 :math:`N` 代表批量大小,:math:`D` 代表嵌入维度。
- **input2** (Tensor) - 输入Tensorshape为 :math:`(N, D)`:math:`(D)` 。数据类型与 `input1` 相同shape需与 `input1` 一致或满足广播规则。
- **target** (Tensor) - 标签Tensor输入值为1或-1。shape为 :math:`(N)`:math:`()`
输出:
Tensor或Scalar。如果 `reduction```"none"`` 返回一个shape与 `target` 相同的Tensor否则将返回一个Scalar。
异常:
- **ValueError** - `reduction` 不为 ``"none"````"mean"````"sum"``
- **ValueError** - `input1``input2` 的形状不匹配。
- **ValueError** - `target` 的形状和 `input1``input2` 的形状不匹配。