RuntimeError: CUDA out of memory. Tried to allocate 1.54 GiB...

26 篇文章 3 订阅

报错

F:\Anaconda3\python.exe "E:/Program Files/PyCharm 2019.2/***/xxx.py"
F:\Anaconda3\lib\site-packages\torch\nn\modules\loss.py:432: UserWarning: Using a target size (torch.Size([50, 1])) that is different to the input size (torch.Size([50])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  return F.mse_loss(input, target, reduction=self.reduction)
Traceback (most recent call last):
  File "E:/Program Files/PyCharm 2019.2/***/***.py", line 192, in <module>
    CNN_train(file_name, B)
  File "E:/Program Files/PyCharm 2019.2/***/***.py", line 134, in CNN_train
    loss.backward()
  File "F:\Anaconda3\lib\site-packages\torch\tensor.py", line 198, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "F:\Anaconda3\lib\site-packages\torch\autograd\__init__.py", line 100, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: CUDA out of memory. Tried to allocate 1.54 GiB (GPU 0; 4.00 GiB total capacity; 1.54 GiB already allocated; 1.38 GiB free; 1.54 GiB reserved in total by PyTorch) (malloc at ..\c10\cuda\CUDACachingAllocator.cpp:289)
(no backtrace available)

解决

  1. 如果是模型训练时报错,应该减小batch_size。
  2. 如果是模型预测时报错,可以在预测前加上一句:
with torch.no_grad():
    pred = cnn(seq).tolist()

加上no_grad()后表示此处不需要进行梯度运算,只是需要前向传播时计算一下输出,从而释放GPU空间。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril_KI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值