天天看点

解决:RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB方法一:方法二 : 方法三:

方法一:

仅需减小batchsize

改文件的配置cfg的batchsize=4,一般在cfg文件下的查找batch或batchsize,将batchsize调小后,再次运行,类似于改下面

解决:RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB方法一:方法二 : 方法三:

方法二 : 

上述方法还没解决,不改batchsize,可以考虑下面的方法的链接

不计算梯度:

ps: 在报错的哪一行代码的上面,加上下面一行代码,不计算梯度

with torch.no_grad()

不计算梯度的方法

方法三:

释放内存:链接如下

释放内存

if hasattr(torch.cuda, 'empty_cache'):
	torch.cuda.empty_cache()
           

ps: 在报错的哪一行代码的上面,加上下面两行代码,释放无关的内存

if hasattr(torch.cuda, 'empty_cache'):
	torch.cuda.empty_cache()
           

继续阅读