torch.nn.functional.linear_cross_entropy
- torch.nn.functional.linear_cross_entropy(input, linear_weight, target, *, linear_bias=None, weight=None, reduction='mean', ignore_index, ,
Compute the cross entropy loss between inputs, transformed linearly, and target.
The statement:
loss = linear_cross_entropy(input, linear_weight, target, **kwargs)
is equivalent to the following reference implementation of linear_cross_entropy:
logits = linear(input, linear_weight)
loss = cross_entropy(logits, target, **kwargs)
provided that ignore_index is not explicitly set to None
in kwargs (since cross_entropy() does not accept None
for ignore_index).
See Linear and CrossEntropyLoss for details.
input (Tensor) – input samples.
linear_weight (Tensor) – linear weight.
target (Tensor) – Ground truth class indices or class probabilities.
With options != None, class probabilities use the chunked
path for reduction 'mean' / 'sum' when the target
dtype matches the dtype and the target does not
require grad; other probability-target configurations fall
back to the reference implementation with a warning
(gradients w.r.t. the target are only available on the
reference path).
Input: or .
linear_bias (Tensor, optional) – bias added to the linear
projection (shape (C,) or (C, d_1, ..., d_K) for
K-dimensional loss, matching linear_weight).
With options != None, K-dimensional bias
(out_features != ()) falls back to the reference
implementation with a warning; the chunked path supports
only (C,)-shaped bias. Default: None.
weight (Tensor, optional) – a manual rescaling weight given to each class.
reduction (str, optional) – Specifies the reduction to apply to
the output: 'none' | 'mean' |
'sum'. 'none': no reduction will be applied,
'mean': the sum of the output will be divided by the
number of elements in the output, 'sum': the output
will be summed.
Default: 'mean'.
ignore_index (int, optional) – Specifies a target value that is
ignored and does not contribute to the input
gradient. Note that ignore_index is only
applicable when the target contains class indices.
Default: None. When target contains class indices, the
default value is mapped to -100. Note: the default
ignore_index in
cross_entropy is -100 for both
target types.
label_smoothing (float, optional) – A float in [0.0, 1.0]. Specifies the amount of smoothing when computing the loss, where 0.0 means no smoothing. The targets become a mixture of the original ground truth and a uniform distribution as described in Rethinking the Inception Architecture for Computer Vision. Default: .
options (LinearCrossEntropyOptions, optional) – Specify
chunking strategy options, see
LinearCrossEntropyOptions
for more details. Enabling chunking will decrease the
memory usage. To enable reference implementation of
linear_cross_entropy, use options=None. Default:
None. See the autograd / compile note below for
which higher-level APIs (torch.compile,
torch.func.grad, torch.func.vmap(grad(...)),
higher-order or forward-mode AD) only work on the
options=None reference path.
output.backward()torch.compile falls back to eager at the chunked op;
allow_retain_graph=True is forced internally to keep
double-backward correct (with a warning).
torch.jit.trace falls back to the reference path with a
warning.
LinearCrossEntropyOptions is not TorchScript-scriptable.
The reference path (options=None) supports all of the above.
Linear weight: or with in the case of K-dimensional loss. Note: multi-dimensional weights (K > 0) require batched input .
Target: If containing class indices, ,
, or when
, where each value should be between
. The target data type is required to be long
when using class indices.
If containing class probabilities, the target must have
shape , , or when , and each value should
be between . This means the target data type
is required to be float when using class probabilities. Note
that PyTorch does not strictly enforce probability
constraints on the class probabilities and that it is the
user’s responsibility to ensure target contains valid
probability distributions.
Weight: .
Output: If reduction is ‘none’, shape , or with in the case of K-dimensional loss, depending on the shape of the input. Otherwise, scalar.
where is batch size and is number of classes.