Thanks I have looked at that.  If I want to get the gradients of each input with respect to each output in a loop such as above then would I need to do
for digit in selected_digits:
output[digit].backward(retain_graph=True)
grad[digit] = input.grad()
If I do this will the gradients coming out of input increment each time or will they be overwritten.  I read that gradients are retained and only cleared when the model is zero grad is called.  If they are incremental then can I get each individual value but taking the difference of the previous value and the latest?
Thanks again