mindspore/docs/api/api_python/nn/mindspore.nn.LossBase.rst

37 lines
1.4 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.nn.LossBase
======================
.. py:class:: mindspore.nn.LossBase(reduction='mean')
损失函数的基类。
自定义损失函数时应重写 `construct` ,并使用方法 `self.get_loss``reduction` 应用于loss计算。
参数:
- **reduction** (str可选) - 指定应用于输出结果的归约计算方式,可选 ``'none'````'mean'````'sum'`` ,默认值: ``'mean'``
- ``"none"``:不应用归约方法。
- ``"mean"``:计算输出元素的(加权)平均值。
- ``"sum"``:计算输出元素的总和。
异常:
- **ValueError** - `reduction` 不为 ``'none'````'mean'````'sum'``
.. py:method:: get_axis(x)
获取输入的轴范围。
参数:
- **x** (Tensor) - 任何shape的Tensor。
.. py:method:: get_loss(x, weights=1.0)
计算加权损失。
参数:
- **x** (Tensor) - shape为 :math:`(N, *)` 的输入Tensor其中 :math:`*` 表示任意数量的附加维度。
- **weights** (Union[float, Tensor], 可选) - 权重。当 `weights` 为Tensor时其rank必须为0或与输入相同并且必须可广播到输入所有维度必须为 `1` ,或与相应输入的维度相同)。默认值: ``1.0``
返回:
返回加权损失。