1.函数原型:loss_func = nn.CrossEntropyLoss()
loss = loss_func(pre_label, label)
2.值得注意的点,这里的label不需要赋值one-hot编码类型,因为函数内部会自动将label变换为one-hot类型,如果这里赋值为one-hot编码,则会产生类似如下报错:
①:RuntimeError: multi-target not supported at /opt/conda/conda-bld/pytorch_1549635019666/work/aten/src
解决办法:使用数值标签,而非one-hot编码
②:expected type torch.cuda.DoubleTensor but got torch.cuda.FloatTensor
解决办法:针对这样的错误,我们之前提到过,基本原因就是函数所需要的参数类型和我们赋值类型不同,这里介绍一种改变torch中tensor的类型的函数:
data = data.type(torch.FloatTensor)
data = data.type(torch.LongTensor)
data = data.type(torch.FloatTensor)