CrossEntropyLoss vs per-class-probabilities target

Documentation mentions that it is possible to pass per class probabilities as a target.

The target that this criterion expects should contain either:

Probabilities for each class;

Target: … If containing class probabilities, same shape as the input.

which also comes with example:

>>> # Example of target with class probabilities
>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5).softmax(dim=1)
>>> output = loss(input, target)
>>> output.backward()

Example fails with

  File "../torch/nn/functional.py", line 2824, in cross_entropy
    return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: 1D target tensor expected, multi-target not supported

Am I missing something?

Hi Sergey!

Support for “soft,” probabilistic targets for CrossEntropyLoss is new
as of (I believe) the current stable version, 1.10.0.

Your best bet will be to upgrade to the current stable release.

Best.

K. Frank