RuntimeError: CUDA out of memory. Tried to allocate 132.00 MiB (GPU 2; 3.95 GiB total capacity; 3.41

pytorch报错:RuntimeError: CUDA out of memory. Tried to allocate 132.00 MiB (GPU 2; 3.95 GiB total capacity; 3.41 GiB already allocated; 37.88 MiB free; 98.29 MiB cached)

如果是训练时遇到该问题,说明模型的参数太多了,将模型的参数减少该问题就解决了,改小batch_size是不能解决的(我将batch_size设为1都没解决,而且报错时的内存数据都没变)

如果是测试时遇到该问题,在测试代码前面加上:with torch.no_grad():

with torch.no_grad():
  # test process

ps:程序运行中可以通过watch -n 0.1 -d nvidia-smi命令来实时查看GPU占用情况,按Ctrl+c退出:

因为我使用的是2号GPU,当我的程序一运行,可以看到2号GPU的占用内存从1MiB一直飙升,直至接近4043MiB,然后就报错上面的RuntimeError

猜你喜欢

转载自blog.csdn.net/weixin_38314865/article/details/105998844