解决:RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB

有文章说:减小batch_size 解决。但是减小到4还是解决不了。

 

上一篇文章https://mp.csdn.net/editor/html/109211594:说with torch.no_grad(),就是不生成计算图。计算结果没有差异。

但是!!!对于计算机资源的占用却有差异!不生成计算图,GPU就会小很大的负担

 

如图,bath_size = 4,依然报错

解决:RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB

在for i,(data,labels)  in enumerate(data)前加上这句代码:顺利执行。同时,batch_size取到128也照样可以运行。

解决:RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB

所以出现此错误,不是单纯减小batch_size()这么简单。

参考:https://www.cnblogs.com/yifanrensheng/p/13381931.html