torch.nn.CrossEntropyLoss()

参考:torch.nn.functional 和 torch.nn 去使用softmax,logsoftmax,crossentropy等的区别_LUQC638的博客-CSDN博客参考:softmax + log = logsoftmax, logsoftmax+ nllloss= crossentropy_LUQC638的博客-CSDN博客import torchimport torch.nn as nnimport torch.nn.functional as F# Example of target with class indicesinput = torch.randn(3, 5)print(f"Input is {input}")t = torch.tensor([1..https://blog.csdn.net/weixin_61445075/article/details/122251742

torch.nn.CrossEntropyLoss() 的输入需要注意:官网说,可以是预测值和类别标量值,或者是预测值和softmax的真实概率值,但实际运行当target是softmax的概率的时候,报错。

CrossEntropyLoss — PyTorch 1.12 documentationicon-default.png?t=M276https://pytorch.org/docs/master/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss


import torch
import torch.nn as nn
import torch.nn.functional as F

if __name__ == '__main__':
    # Example of target with class indices
    # Example of target with class indices
    loss = nn.CrossEntropyLoss()
    input = torch.randn(3, 5, requires_grad=True)
    target = torch.empty(3, dtype=torch.long).random_(5)
    output = loss(input, target)
    print(input,target,output)
    output.backward()
    # Example of target with class probabilities
    input = torch.randn(3, 5, requires_grad=True)
    target = torch.randn(3, 5).softmax(dim=1)
    print(input,target)
    output = loss(input, target)
    print(input, target, output)
    output.backward()

输出:

tensor([[ 0.3674, -0.5932, -0.7216, -1.3490,  0.0218],
        [ 1.9170,  0.4528, -0.1410,  0.2581, -0.5870],
        [ 1.8812, -0.2069, -0.0421, -0.2248,  0.1507]], requires_grad=True) tensor([4, 4, 2]) tensor(2.2236, grad_fn=)
tensor([[-2.4329, -2.4403,  0.1269,  0.7363,  0.1317],
        [-0.4405,  1.5062, -0.9750, -2.3200, -0.3775],
        [-0.2398,  0.8891, -0.5336,  0.3223,  0.8040]], requires_grad=True) tensor([[0.2735, 0.1542, 0.4316, 0.0837, 0.0570],
        [0.0465, 0.3717, 0.4131, 0.0301, 0.1387],
        [0.2184, 0.1615, 0.1662, 0.0928, 0.3611]])
Traceback (most recent call last):
  File "/home/luzhengyu/PycharmProjects/test.py", line 19, in 
    output = loss(input, target)
  File "/home/luzhengyu/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1051, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/luzhengyu/anaconda3/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 1120, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "/home/luzhengyu/anaconda3/lib/python3.8/site-packages/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

你可能感兴趣的:(pytorch,pytorch)