Solved by albanD in post #2 
              
              
                Hi, 
I think the simplest thing to do here is to guard the register_hook() call with if score.requires_grad:. 
Also side note: you don’t need to call retain_grad() for the hook to work. Only if you want to be able to read the value in the .grad field on the Tensor.