训练时使用nn.CrossEntropyLoss()作为损失函数,输入数据时报错
criterion = nn.CrossEntropyLoss()#损失函数
loss = criterion(output, target)#这里是输入
报错[debug] RuntimeError: “nll_loss_forward_reduce_cuda_kernel_2d_index“ not implemented for ‘float‘
看报错内容应该是类型问题,查阅pytorch官网CrossEntropyLoss — PyTorch 1.11.0 documentationhttps://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLossnn.CrossEntropyLoss()的介绍
官方示例中target的dtype用了long,而我程序中用的是float32
将类型改为long再测试
target=target.to(torch.long)
成功运行。