../aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [192,0,0], thread: [95,0,0] Assertion

../aten/src/ATen/native/cuda/Loss.cu:115: operator(): block: [192,0,0], thread: [95,0,0] Assertion `input_val >= zero && input_val <= one` failed.

RuntimeError: CUDA error: device-side assert triggered CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.

For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

truth =

源代码上下文(计算损失函数):
        loss = nn.BCEWithLogitsLoss()(logit, truth.float())
        pos = (truth>0.5).float()
为了确认truth符合预期的形状和数值,使用.shape和torch.unique()打印无果,调试发现是一个地址
使用.min().item()打印其值,发现最小值确实小于0,不符合损失计算的预期;而以为有错的truth,用truth.unique().cpu().numpy()打印发现值为0和1,符合预期;
虽然报错是在pos = (truth>0.5).float() ,但实际出错的原因是模型的值输出超出了F.binary_cross_entropy()能够接受的范围,其函数没有sigmoid归一化的机制,如果模型本身带有sigmoid归一化还可以避免这个错误,否则就可能出现上述的错误;
为了对logit的范围更健壮,可以使用具有Sigmoid()归一化机制的nn.BCEWithLogitsLoss()来代替原来的BCEloss,这个实现的数值稳定性也更好;
       

你可能感兴趣的:(深度学习,人工智能)