RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94

Posted myriads_changes_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94相关的知识,希望对你有一定的参考价值。

错误:

RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94 GiB already allocated; 169.19 MiB free; 1.96 GiB reserved in total by PyTorch)

我们可以看见内存是还有很多的,但是在运行程序的时候还是会出现CUDA out of memory

解决方法

一、正确调用指定GPU

我们在调用GPU时,并没有使用到指定“内存”,使用的是显存,这样就会导致我们在跑程序的时出现内存溢出
将下面的代码添加到代码中

os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")#判断CUDA是否能使用,不可以就使用CPU

二、杀死进程

这里我们杀死进程要注意的是,杀死进程是否可以pass掉,不然会导致你有的程序终止掉,切记切记

kill id

eg:

三、减小batchSize

每次读取的数据减小,或者说减小每次训练的数据大小

四、关机重启

要是前面三个方法还是没有解决问题,关机重新启动试一试。
还是不行我也表示~~~~~~sorry

希望这篇文章对你有用!
谢谢点赞评论!

以上是关于RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94的主要内容,如果未能解决你的问题,请参考以下文章

使用VGG网络训练发生错误RuntimeError: CUDA out of memory解决方案:

AI绘画——使用stable-diffusion生成图片时提示RuntimeError: CUDA out of memory处理方法

RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)

Pytorch RuntimeError: CUDA error: out of memory at loss.backward() , 使用 CPU 时没有错误

RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)

RuntimeError: CUDA out of memory. Tried to allocate 600.00 MiB (GPU 0; 23.69 GiB total capacity)