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

报错

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空间。

你可能感兴趣的:(PyTorch,Python,pytorch,深度学习,gpu)