When I tried to run my training loop
iters=0
lossesTrain = []
lossesTest = []
print('Training Started!')
torch.backends.cudnn.benchmark = True # Optimizes cudnn
for epochs in range(num_epochs):
for i,(c, n) in enumerate(trainLoader):
#converting images and labels to variables so as to calculate gradient
if torch.cuda.is_available():
clean = Variable(c.cuda())
noisy = Variable(n.cuda())
else:
clean = Variable(c)
noisy = Variable(n)
#clearing gradient buffers
optimizer.zero_grad()
#finding outputs
output = model(noisy)
#calculating losses
loss=criterion(output,clean)
#backpropagating the loss
loss.backward()
#updating the parameters
optimizer.step()
#updating counter
iters+=1
print(i)
if iters % 25 == 0:
for c,n in testLoader:
cleanT = Variable(c)
noisyT = Variable(n)
if torch.cuda.is_available():
cleanT = Variable(c.cuda())
noisyT = Variable(n.cuda())
else:
cleanT = Variable(c)
noisyT = Variable(n)
outputT = model(noisyT)
lossesTest.append(criterion(outputT, cleanT).detach())
lossesTrain.append(criterion(output, clean).detach())
print('Epoch: {}, Iter: {}, lossesTrain: {}, lossesTest: {}'.format(epochs, iters, sum(lossesTrain[-1]), sum(lossesTest[-1])))
# print(plt.imshow(copy.reshape((512,512,-1)).detach().numpy()))
print('Training Done!! Save the model')
I got this as a RunTimeError:-
RuntimeError: CUDA out of memory. Tried to allocate 2.00 GiB (GPU 0; 4.00 GiB total capacity; 1.13 GiB already allocated; 212.59 MiB free; 2.75 GiB reserved in total by PyTorch)
Any ideas on what could be done to avoid this and why is this happening?