解决RuntimeError: 0D or 1D target tensor expected, multi-target not supported

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

报错截图如下:
解决RuntimeError: 0D or 1D target tensor expected, multi-target not supported_第1张图片出错的相关代码:

loss = nn.CrossEntropyLoss()(output.narrow(0, 0, data_source.size(0)), label_source)

出错原因:第二个参数应该降维
更正:(在label_source后方添加函数.squeeze())

loss = nn.CrossEntropyLoss()(output.narrow(0, 0, data_source.size(0)), label_source.squeeze(dim=1))

本段代码更正后报错截图:
在这里插入图片描述
那就在此段代码前面加上:

label_source =label_source.to(torch.int64)

成功运行

你可能感兴趣的:(python,深度学习,pytorch)