torch.nn.CrossEntropyLoss()

本文介绍了在PyTorch中如何使用`torch.nn.CrossEntropyLoss`损失函数。示例展示了当目标是类别索引和概率分布时的正确用法。当目标是概率分布时,计算会报错,因为CrossEntropyLoss期望1D的目标张量,而非多目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考: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=<NllLossBackward>)
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 <module>
    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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值