我學習pytorch,並採取ANPR項目,這是基於tensorflow (https://github.com/matthewearl/deep-anpr, http://matthewearl.github.io/2016/05/06/cnn-anpr/) 作爲練習,移植它pytorch平臺。約torch.nn.CrossEntropyLoss參數形狀
有一個問題,我使用nn.CrossEntropyLoss()作爲損失函數: 準則= 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
和targets.data [大小4x253的torch.FloatTensor]是:
- 1 0 0 ... 0 0 0
- 1 0 0 ... 0 0 0
- 1 0 0 ... 0 0 0
- 1 0 0 ... 0 0 0
- [大小4x253的torch.DoubleTensor]
當我打電話: 損耗=標準(輸出,目標)
發生錯誤,信息是: 類型錯誤:FloatClassNLLCriterion_updateOutput接收的參數無效的組合 - 得到(int,torch.FloatTensor,torch.DoubleTensor,torch.FloatTensor,bool,NoneType,torch.FloatTensor),但預計(int state,torch.FloatTensor input,torch.LongTensor target,torch.FloatTensor output, bool sizeAverage,[torch.FloatTensor weight or None],torch.FloatTensor total_w爲八個)
'預計torch.LongTensor' ...... '有torch.DoubleTensor',但如果我轉換指標納入LongTensor: torch.LongTensor(numpy.array(targets.data.numpy() ,numpy.long))
呼叫損失=標準(輸出,目標),錯誤是:
RuntimeError:在/數據/使用者/ soumith/miniconda2 /康達-BLD/pytorch多目標不支持-0.1.10_1488752595704/work/torch/lib/THNN/generic/ClassNLLCriterion.c:20
我上次的練習是mnist,來自pytorch的一個例子,我做了一些修改,batch_size是4,損耗函數: 損耗= F.nll_loss(輸出,標籤) outputs.data:
-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
[尺寸爲4×10的torch.FloatTensor]
labels.data:
- [大小的torch.LongTensor 4]
標籤,對於輸入圖像,必須是單一元件,在上面的示例,存在是253個數字,在'mnist'中只有一個數字,輸出的形狀與標籤不同。
我回顧了tensorflow手冊,tf.nn.softmax_cross_entropy_with_logits, 'Logits和標籤必須具有相同的形狀[batch_size,num_classes]和相同的dtype(float32或float64)。
pytorch是否支持tensorflow中的相同功能?
許多THKS