Solved by KFrank in post #2 
              
              
                Hi Bahman! 

Your code should do what you want.  Autograd tracks computations 
with pytorch tensors and doesn’t care whether those computations are 
“free standing” (such as your mask operation) or part of your model or 
part of your loss – they’re all treated the same. 
If mask has 0s in it, then w…