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

created at 01-31-2022 views: 15

error

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)

solution

If an error is reported during model training, batch_size should be reduced. If the model predicts an error, you can add a sentence before the prediction:

with torch.no_grad():
     pred = cnn(seq).tolist()

Adding no_grad() means that no gradient operation is required here, but the output needs to be calculated when forward propagation is needed, thereby freeing up GPU space.

created at:01-31-2022
edited at: 01-31-2022: