美文网首页
pytorch学习笔记-CUDA: out of memory

pytorch学习笔记-CUDA: out of memory

作者: 升不上三段的大鱼 | 来源:发表于2020-07-31 23:44 被阅读0次

错误信息:

RuntimeError: CUDA out of memory. Tried to allocate....

解决方法:

  1. 减小batch size
  2. 在测试的时候,使用 torch.no_grad()
with torch.no_grad():
    output = model(inputs)    
  1. 释放缓存
    可以使用 torch.cuda.empty_cache()
try:
    output = model(inputs)
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
  1. 其他可能的方法
  • 清理一下RAM和GPU的空间
  • 如果在Dataloader里使用了num_workers, 可以设成num_works=1
  • 如果没有显示已经使用多少内存的信息,可能是pytorch, cuda之类的版本不匹配

参考
pytorch-显存释放问题

相关文章

网友评论

      本文标题:pytorch学习笔记-CUDA: out of memory

      本文链接:https://www.haomeiwen.com/subject/fwverktx.html