Solved by albanD in post #2 
              
              
                Hi, 
The detach() in the no_grad block is not needed. 
You will need to move all the ops into the no_grad block though to make sure no gradient is tracked