I want to apply a mask to my model’s output and then use the masked output to calculate a loss and update my model.
I don’t want the autograd to consider the masking operation when calculating the gradients, i.e. I want the autograd to treat my model as if it had outputed the masked version of my input. In other words, I don’t want to calculate any loss for the covered regions of output.
my code is something like:

batch = get_batch()
x = Variable(batch[‘data’], requires_grad=False)
mask = Variable(batch[‘mask’], requires_grad=False)
y = model(x)
z = y * mask
loss = loss_function(z)
loss.backward()
optimizer.step()

I assume since requires_grad is set to False for both x and mask this operation should not affect the gradients.
Currently my model behaves as if there is no masking so I want to know if I’m doing something wrong when applying the mask or if I should look for the problem somewhere else.
I’m also not sure if I should use with torch.no_grad() when applying the mask but I’m guessing I shouldn’t.