天天看點

/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:106: void cunn_ClassNLLCriterion_updateOutput_kernel(D

welcome to my blog

頭一次使用pytorch, 調用交叉熵損失函數nn.CrossEntropyLoss()的時候報錯

/pytorch/aten/src/THCUNN/ClassNLLCriterion.cu:106: void cunn_ClassNLLCriterion_updateOutput_kernel(Dtype *, Dtype *, Dtype *, long *, Dtype *, int, int, int, int, long) [with Dtype = float, Acctype = float]: block: [0,0,0], thread: [4,0,0] Assertion

t >= 0 && t < n_classes

failed.

原因: nn.CrossEntropyLoss(input, target)中, input的列數和target不比對

舉例來說, 如果是個5分類問題, 那麼input的形狀是batch_size * 5, target中每個元素的取值範圍是{0,1,2,3,4}, 也就是[0, 類别數量減1]

是以對于n分類問題來說, 如果target中某個元素的取值不在[0,n-1]中就會報上述錯誤