Cuda runtime error: out of memory While calculating accuracy

Getting out of memory error while calculating accuracy. The following code is used to calculate

out = model(x)
batch_acc = 0.0
pred = out.max(1)[1].unsqueeze(1)
batch_acc = ((y == pred).sum().item() * 1.0) / len(y)

If your GPU memory is nearly filled you might run OOM in the last compare operation.
You could push the tensors first to the CPU and calculate the accuracy later, as you are apparently storing them on the CPU anyway.

1 Like

Perhaps another workaround that I found to be working is this. Turns out that this could be a more memory efficient implementation in my case. Also it would prevent the IO time spent on transferring tensors to CPU.

val = out.max(1)[1]
batch_acc = val.eq_(y).sum().item()