官方示例:
>>> loss = nn.CrossEntropyLoss() >>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.empty(3, dtype=torch.long).random_(5) >>> output = loss(input, target) >>> output.backward()
1.在loss中的輸入中,target為類別的index,而非one-hot編碼。
2.在輸入的target的index中,數據的范圍為[0, c-1],其中c為類別的總數,注意index的編碼從0開始。
