我正在学习 pytorch,并参加基于 tensorflow 的 anpr项目 (https://github.com/matthewearl/deep-anpr,http://matthewearl.github.io/2016/05/06/cnn-anpr / ) 作为练习,移植到pytorch平台。
有一个问题,我使用 nn.CrossEntropyLoss() 作为损失函数:
criterion=nn.CrossEntropyLoss()
模型的 output.data 是:
- 1.00000e-02 *
- 2.5552 2.7582 2.5368 ... 5.6184 1.2288 -0.0076
- 0.7033 1.3167 -1.0966 ... 4.7249 1.3217 1.8367
- 0.7592 1.4777 1.8095 ... 0.8733 1.2417 1.1521
- 0.1040 -0.7054 -3.4862 ... 4.7703 2.9595 1.4263
- [torch.FloatTensor of size 4x253]
和targets.data是:
- 1 0 0 ... 0 0 0
- 1 0 0 ... 0 0 0
- 1 0 0 ... 0 0 0
- 1 0 0 ... 0 0 0
- [torch.DoubleTensor of size 4x253]
当我打电话时:
loss=criterion(output,targets)
发生错误,信息是:
TypeError: FloatClassNLLCriterion_updateOutput received an invalid combination of arguments - got (int, torch.FloatTensor, **torch.DoubleTensor**, torch.FloatTensor, bool, NoneType, torch.FloatTensor), but expected (int state, torch.FloatTensor input, **torch.LongTensor** target, torch.FloatTensor output, bool sizeAverage, [torch.FloatTensor weights or None], torch.FloatTensor total_weight)
'expected torch.LongTensor'......'got torch.DoubleTensor',but if i convert the targets into LongTensor:
torch.LongTensor(numpy.array(targets.data.numpy(),numpy.long))
调用 loss=criterion(output,targets),错误是:
RuntimeError: multi-target not supported at /data/users/soumith/miniconda2/conda-bld/pytorch-0.1.10_1488752595704/work/torch/lib/THNN/generic/ClassNLLCriterion.c:20
我最后一个练习是mnist,一个pytorch的例子,我做了一点修改,batch_size是4,损失函数:
loss = F.nll_loss(outputs, labels)
输出数据:
- -2.3220 -2.1229 -2.3395 -2.3391 -2.5270 -2.3269 -2.1055 -2.2321 -2.4943 -2.2996
-2.3653 -2.2034 -2.4437 -2.2708 -2.5114 -2.3286 -2.1921 -2.1771 -2.3343 -2.2533
-2.2809 -2.2119 -2.3872 -2.2190 -2.4610 -2.2946 -2.2053 -2.3192 -2.3674 -2.3100
-2.3715 -2.1455 -2.4199 -2.4177 -2.4565 -2.2812 -2.2467 -2.1144 -2.3321 -2.3009
[torch.FloatTensor of size 4x10]
标签.数据:
- 8
- 6
- 0
- 1
- [torch.LongTensor of size 4]
标签,对于输入图像,必须是单个元素,在上例中,有 253 个数字,而在 'mnist' 中,只有一个数字,输出的形状与标签不同。
我查看了 tensorflow 手册 tf.nn.softmax_cross_entropy_with_logits,“Logits 和标签必须具有相同的形状 [batch_size, num_classes] 和相同的 dtype(float32 或 float64)。”
pytorch 是否支持 tensorflow 中的相同功能?
很多想法