Thanks a lot. I got another error
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
Iam actually manually upgrading the weights by getting inputs from user. I read your answer to provide retain_graph=True. Still it does not work. Here is my code.
correct_class=torch.LongTensor([output_class[get_ans]])
optimizer.zero_grad()
loss=criterion(output,correct_class)
loss.backward(retain_graph=True)
optimizer.step()