Solved by albanD in post #2 
              
              
                Hi, 
The .item() that you use converts the result of your max into a python number. And we can’t track gradient for it. So it is ignored. 
You will need to remove this .item() to get proper gradients.