pytorch出现RuntimeError: CUDA out of memory.

  • Post author:
  • Post category:其他


无论batch-size设置多小也是会出现这个问题的,我的原因是我将pytorch升级到了1.0.1,然后出现了这个问题

RuntimeError: CUDA out of memory. Tried to allocate 823.88 MiB (GPU 0; 7.93 GiB total capacity; 6.96 GiB already allocated; 189.31 MiB free; 10.26 MiB cached)

你可以监控一下之GPU的使用情况 ,使用下面的命令

watch -n 0.1 nvidia-smi

在期间会出现GPU的使用率达到99%,估计是没有释放GPU内存吧。

解决方法

我出现问题的代码,在输入到网络里面 ,如下:

output = net(input,inputcoord)

将这个代码做如下修改,

其中torch.no_grad()是禁用梯度计算的上下文管理器,一般是在validate或者test会使用,只需要计算网络的输出,而无需计算梯度了

with torch.no_grad():
    output = net(input,inputcoord)

附带

在调试低版本的pytorch源程序的时候也会出现警告

 UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.

也可以在前面添加下面的解决这个问题

with torch.no_grad():

参考


https://blog.csdn.net/xijuezhu8128/article/details/86594478


https://pytorch.org/docs/stable/generated/torch.no_grad.html?highlight=torch%20no_grad#torch.no_grad



版权声明:本文为pursuit_zhangyu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。