RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 23.65 GiB total capacity; 20.5

torch遇到错误:RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 23.65 GiB total capacity; 20.53 GiB already allocated; 9.56 MiB free; 20.94 GiB reserved in total by PyTorch)

原因:应该是我使用的图数据集太大了,而且是一开始就全部怼到了cuda上,所以就内存不够了

解决方法:
参考链接

将批次迭代地发送到 CUDA,并制作小批量,不要一开始就一次将所有数据发送到 CUDA。

for e in range(epochs):
    for images, labels in train_loader:   
        if torch.cuda.is_available():
            images, labels = images.cuda(), labels.cuda()   
        # blablabla  

另外,在evaluate或者test时候,注意使用torch.autograd.no_grad() 阻止梯度更新。

with torch.autograd.no_grad():
	# blabla
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值