r"""Computes the multi-class cross entropy loss (using logits by default). When using label smoothing, the label distribution is as follows: .. math:: y^{LS}_{k}=y_{k}\left(1-\alpha\right)+\alpha/K where :math:`y^{LS}` and :math:`y` are new label distribution and origin label distribu
(
pred: Tensor,
label: Tensor,
axis: int = 1,
with_logits: bool = True,
label_smooth: float = 0,
reduction: str = "mean",
)
| 137 | |
| 138 | @_reduce_output |
| 139 | def cross_entropy( |
| 140 | pred: Tensor, |
| 141 | label: Tensor, |
| 142 | axis: int = 1, |
| 143 | with_logits: bool = True, |
| 144 | label_smooth: float = 0, |
| 145 | reduction: str = "mean", |
| 146 | ) -> Tensor: |
| 147 | r"""Computes the multi-class cross entropy loss (using logits by default). |
| 148 | |
| 149 | When using label smoothing, the label distribution is as follows: |
| 150 | |
| 151 | .. math:: y^{LS}_{k}=y_{k}\left(1-\alpha\right)+\alpha/K |
| 152 | |
| 153 | where :math:`y^{LS}` and :math:`y` are new label distribution and origin label distribution respectively. |
| 154 | k is the index of label distribution. :math:`\alpha` is ``label_smooth`` and :math:`K` is the number of classes. |
| 155 | |
| 156 | Args: |
| 157 | pred: input tensor representing the predicted value. |
| 158 | label: input tensor representing the classification label. |
| 159 | axis: an axis along which softmax will be applied. Default: 1 |
| 160 | with_logits: whether to apply softmax first. Default: True |
| 161 | label_smooth: a label smoothing of parameter that can re-distribute target distribution. Default: 0 |
| 162 | reduction: the reduction to apply to the output: 'none' | 'mean' | 'sum'. |
| 163 | |
| 164 | Returns: |
| 165 | loss value. |
| 166 | |
| 167 | Examples: |
| 168 | |
| 169 | By default(``with_logitis`` is True), ``pred`` is assumed to be logits, |
| 170 | class probabilities are given by softmax. |
| 171 | It has better numerical stability compared with sequential calls to |
| 172 | :func:`~.softmax` and :func:`~.cross_entropy`. |
| 173 | |
| 174 | >>> pred = Tensor([[0., 1.], [0.3, 0.7], [0.7, 0.3]]) |
| 175 | >>> label = Tensor([1., 1., 1.]) |
| 176 | >>> F.nn.cross_entropy(pred, label) # doctest: +SKIP |
| 177 | Tensor(0.57976407, device=xpux:0) |
| 178 | >>> F.nn.cross_entropy(pred, label, reduction="none") |
| 179 | Tensor([0.3133 0.513 0.913 ], device=xpux:0) |
| 180 | |
| 181 | If the ``pred`` value has been probabilities, set ``with_logits`` to False: |
| 182 | |
| 183 | >>> pred = Tensor([[0., 1.], [0.3, 0.7], [0.7, 0.3]]) |
| 184 | >>> label = Tensor([1., 1., 1.]) |
| 185 | >>> F.nn.cross_entropy(pred, label, with_logits=False) # doctest: +SKIP |
| 186 | Tensor(0.5202159, device=xpux:0) |
| 187 | >>> F.nn.cross_entropy(pred, label, with_logits=False, reduction="none") |
| 188 | Tensor([0. 0.3567 1.204 ], device=xpux:0) |
| 189 | |
| 190 | """ |
| 191 | n0 = pred.ndim |
| 192 | n1 = label.ndim |
| 193 | assert n0 == n1 + 1, ( |
| 194 | "target ndim must be one less than input ndim; input_ndim={} " |
| 195 | "target_ndim={}".format(n0, n1) |
| 196 | ) |
nothing calls this directly
no test coverage detected