Hi Bahman!



 Bahman_Rouhani:

I want the autograd to treat my model as if it had outputed the masked version of my input.

y = model(x)
z = y * mask
loss = loss_function(z)
loss.backward()



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 when you multiply an element of y by 0,
no gradients will flow back through that element of y (or, more
precisely, the gradients that flow back through it will be zero).
As an aside, Variable has been deprecated for some time you, so
you probably don’t want to be using it – just use regular tensors.


Currently my model behaves as if there is no masking


Just to check, if your mask is entirely zero, do your gradients all
become zero?  Does changing z = y * mask in your code to z = y
truly have no effect on your gradients?  “No effect” would indicate a
bug somewhere other than in the code you posted.
Best.
K. Frank