try:
outputs = (model(train_datas)).cuda()
except RuntimeError as exception:
if "out of memory" in str(exception):
print("WARNING: out of memory")
if hasattr(torch.cuda, 'empty_cache'):
torch.cuda.empty_cache()
else:
raise exception
单独使用 torch.cuda.empty_cache() 不会奏效。

本文介绍了一种在遇到GPU内存溢出错误时的处理方法,特别是当使用PyTorch进行深度学习模型训练时。文中详细解释了如何通过释放缓存来解决运行时错误,并强调仅调用torch.cuda.empty_cache()可能不足以解决问题。





