About torch back propagation

zhj123169: <p>I am coding about perturbation with torch and I want to get a mask with the same shape of input</p>
<pre><code class="lang-auto">mask  = model1(mask_initial).  #to get a mask tensor with the same shape of input
threshold = torch.max(mask)*radio
#to perturbate input
after_input['id'] = torch.where(mask &gt; threshold, initial_input['id'],perturbation_input['id'])
 # initial_input['id'],perturbation_input['id'] is torch.int32
after_input['value'] = torch.where(mask &gt; threshold, initial_input['value'],perturbation_input['value'])
 # initial_input['value'],perturbation_input['value'] is torch.float
#to get loss
score1 = model2(initial_input)
score2 = model2(after_input)
loss = score1 - score2

</code></pre>
<p>I expect this loss can optimize the parameters of model1, however the mask.grad is none as well as the grad of the parameters of model1.How can I make it? Can torch.where propagate grad to its condition?</p>

KFrank: <p>Hi Zhj!</p>
<aside class="quote no-group" data-username="zhj123169" data-post="1" data-topic="151941" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhj123169/48/49708_2.png" class="avatar"> zhj123169:</div>
<blockquote>
<p>Can torch.where propagate grad to its condition?</p>
</blockquote>
</aside>
<p><code>torch.where()</code> does not propagate gradients to its condition.</p>
<p>The reason for this is those gradients – although well defined – aren’t useful.<br>
When <code>mask &lt; threshold</code>, the gradient is zero.  When  <code>mask &gt; threshold</code>,<br>
the gradient is also zero.  When mask is exactly equal to threshold, the<br>
gradient is (usually) undefined.  So the gradient is almost always zero, and<br>
there is no point in backpropagating it.</p>
<p>Best.</p>
<p>K. Frank</p>

zhj123169: <p>Thank you very much for your answer! It helps me a lot.<br>
If I have to optimize the parameters of model1 with loss.Can I do it in this way?</p>
<pre><code class="lang-auto">mask.grad = function(loss)
#this function get a tensor with the same shape as mask, setting mask.grad mannually
mask.backward()
#then I backward it
</code></pre>
<p>Could you give me some advice？I’ll appreciate any suggestion.</p>

KFrank: <p>Hi Zhj!</p>
<aside class="quote no-group" data-username="zhj123169" data-post="3" data-topic="151941" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/zhj123169/48/49708_2.png" class="avatar"> zhj123169:</div>
<blockquote>
<pre><code class="lang-auto">mask.grad = function(loss)
#this function get a tensor with the same shape as mask, setting mask.grad mannually
mask.backward()
</code></pre>
</blockquote>
</aside>
<p>Conceptually, you can do something like this.</p>
<p><em>Assuming</em> that you can construct a <code>function()</code> that gives you a <em>useful</em><br>
surrogate (or approximation to the) gradient of <code>loss</code> with respect to <code>mask</code>,<br>
then this could work.</p>
<p>Note, however, that if your surrogate gradient is zero almost everywhere,<br>
it won’t be <em>useful</em> and won’t do you any good (even though it might be<br>
a very good approximation to the actual gradient).</p>
<p>If you use this approach, you don’t want to overwrite <code>mask.grad</code>.   Instead<br>
call <code>mask.backward (function (loss))</code>.  You can pass to <code>.backward()</code><br>
the gradient (or a surrogate) of your downstream scalar loss function with<br>
respect to <code>mask</code> (or whatever you’re calling <code>.backward()</code> on), and autograd<br>
will complete the backpropagation for you.</p>
<p>But this will only work if your surrogate gradient – <code>function (loss)</code> – is<br>
actually useful and approximates the gradient of some sort of “smoothed<br>
thresholding” version of the not-usefully-differentiable thresholding you<br>
apply to <code>mask</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

zhj123169: <p>Hi KFrank!<br>
I have solved this problem. Thank you very much for your patience and your suggestion really helps me.<br>
Thanks again!<br>
Best.<br>
Zhj</p>