如何解决Pytorch 1.6.0-RuntimeError:CUDA错误:设备端断言在哪里触发预测==标签
当我运行此功能时:
def evaluate(model,dataloader,calc_loss=False):
'''
Helper function to get classification accuracy and (optional) loss for a model over the items in dataloader
This function can just return the accuracy,or calculate the losses if "calc_loss" is set to "True"
'''
correct = 0
total = 0
losses = []
# don't compute gradients
with torch.no_grad():
for batch in dataloader:
inputs,labels = batch
# forward pass through the model
outputs = model(inputs)
print('pass 1')
# calculate loss if parameter set to True
if (calc_loss==True):
loss = F.cross_entropy(outputs,labels) # Calculate loss
losses.append(loss.detach)
# Get the prediction of the net on the images
print('pass 2')
_,predicted = torch.max(outputs.data,dim=1)
total += labels.size(0)
# Count those we got correct
right = torch.sum(predicted==labels).item()
print('pass 3',right)
break
# calculate total correct cases
acc = 100 * correct / total
return losses,acc
这是我插入并返回的内容:
evaluate(model_1,dataloader=val_dl,calc_loss=True)
结果:
pass 1 pass 2 --------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-22-881e9c4f2402> in <module> 1 os.environ['CUDA_LAUNCH_BLOCKING'] = '1' ----> 2 evaluate(model_1,calc_loss=True) <ipython-input-15-edcb180f1931> in evaluate(model,calc_loss) 29 30 # Count those we got correct ---> 31 right = torch.sum(predicted==labels).item() 32 33 print('pass 3',right) RuntimeError: CUDA error: device-side assert triggered
predict == labels所在的位置似乎出现了此错误,但是当我独立运行该代码(在for循环/函数之外)时,它可以完美地运行所有代码。有什么想法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。