mindspore2022/docs/api/api_python/nn/mindspore.nn.SoftmaxCrossEn...

40 lines
1.8 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.SoftmaxCrossEntropyWithLogits
===========================================
.. py:class:: mindspore.nn.SoftmaxCrossEntropyWithLogits(sparse=False, reduction='none')
计算预测值与真实值之间的交叉熵。
使用交叉熵损失函数计算出输入概率使用softmax函数计算和真实值之间的误差。
对于每个实例 :math:`x_i` i的范围为0到N-1则可得损失为
.. math::
\ell(x_i, c) = - \log\left(\frac{\exp(x_i[c])}{\sum_j \exp(x_i[j])}\right)
= -x_i[c] + \log\left(\sum_j \exp(x_i[j])\right)
其中 :math:`x_i` 是一维的Tensor :math:`c` 为one-hot中等于1的位置。
.. note::
虽然目标值是互斥的,即目标值中只有一个为正,但预测的概率不为互斥。只要求输入的预测概率分布有效。
**参数:**
- **sparse** (bool) - 指定目标值是否使用稀疏格式。默认值False。
- **reduction** (str) - 指定应用于输出结果的计算方式。取值为"mean""sum",或"none"。取值为"none"则不执行reduction。默认值"none"。
**输入:**
- **logits** (Tensor) - shape (N, C)的Tensor。数据类型为float16或float32。
- **labels** (Tensor) - shape (N, )的Tensor。如果 `sparse` 为True`labels` 的类型为int32或int64。否则`labels` 的类型与 `logits` 的类型相同。
**输出:**
Tensor一个shape和数据类型与logits相同的Tensor。
**异常:**
- **TypeError** - `sparse` 不是bool。
- **TypeError** - `sparse` 为True并且 `labels` 的dtype既不是int32也不是int64。
- **TypeError** - `sparse` 为False并且 `labels` 的dtype既不是float16也不是float32。
- **ValueError** - `reduction` 不为"mean"、"sum",或"none"。