'nn.CorssEntropyLoss documentation example is not correct?

Tested the documentation example of nn.CrossEntropyLoss. It doesn't seem to work.

loss = nn.CrossEntropyLoss()

input = torch.randn(3, 5, requires_grad=True)
target = torch.randn(3, 5).softmax(dim=1)
output = loss(input, target)
print(output)

error:

RuntimeError: 1D target tensor expected, multi-target not supported


Solution 1:[1]

You might need to upgrade your torch version.

pip install torch --upgrade

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joe32140