【error】RuntimeError: multi-target not supported at
阿新 • • 發佈:2019-02-08
錯誤:
使用Cross_entropy
損失函式時出現
RuntimeError: multi-target not supported at
原因:
其引數說明:
input has to be a 2D Tensor of size batch x n.
This criterion expects a class index (0 to nClasses-1) as the target for each value of a 1D tensor of size n
- 1
- 2
其標籤必須為0~n-1,而且必須為1維的,如果設定標籤為[nx1]的,則也會出現以上錯誤。
以上文字來源於https://blog.csdn.net/u011276025/article/details/73826562出錯程式碼為:
# print(outputs.size()) # (128L, 2L)
# print(trg) # (128L, 1L)
loss = criterion(outputs, trg)
更改為:
# print(outputs.size()) # (128L, 2L) # print(trg.squeeze()) # (128L, ) 或 [128] loss = criterion(outputs, trg.squeeze())
錯誤解決。