Pytorch 训练时有时候会因为加载的东西过多而爆显存,有些时候这种情况还可以使用cuda的清理技术进行修整,当然如果模型实在太大,那也没办法。
使用torch.cuda.empty_cache()删除一些不需要的变量代码示例如下:
try:
optimizer.step()
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
测试的时候爆显存有可能是忘记设置no_grad, 示例代码如下:
with torch.no_grad():
self.optimizer.step()