Thanks a lot for your reply!
One more question, I understand that I have to use retain_variables=True if I want to compute the gradient for multiple times. Can I still do that without computing additional variables if I use current_score = output[selected_digit] and then current_score.backward(retain_variables=True)? Or do I have to do it with output.backward(torch.Tensor([0,0,0,1,0,...,0]),retain_variables=True)while changing the position of 1 for different digits?