【pytorch】CrossEntropyLoss Assertion `t >= 0 && t < n_classes` failed.

应用交叉熵计算多分类损失Assertion t >= 0 && t < n_classes failed.

报错语句:

loss_c = F.cross_entropy(c, cl.type(torch.cuda.LongTensor))
/opt/conda/conda-bld/pytorch_1565272279342/work/aten/src/THCUNN/ClassNLLCriterion.cu:105: 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: [17,0,0] Assertion `t >= 0 && t < n_classes` failed.
/opt/conda/conda-bld/pytorch_1565272279342/work/aten/src/THCUNN/ClassNLLCriterion.cu:105: 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: [18,0,0] Assertion `t >= 0 && t < n_classes` failed.
/opt/conda/conda-bld/pytorch_1565272279342/work/aten/src/THCUNN/ClassNLLCriterion.cu:105: 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: [19,0,0] Assertion `t >= 0 && t < n_classes` failed.

CrossEntropyLoss 的输入是batch size×class count大小的分类输出和class count大小的对应真值,分类输出无需softmax,真值是从零开始的类别计数。

因此,Assertion t >= 0 && t < n_classes failed.的意思是真值计数超出了[0, class count - 1]的范围。thread: [19,0,0]指的是真值输入索引19处的真值超出了计数范围。
因为这个错误还导致了后续报错:

RuntimeError: CUDA error: device-side assert triggered

把交叉熵的报错解决了之后RuntimeError也没了。。

你可能感兴趣的:(pytorch编程,python,pytorch,深度学习)