Gradient of using the output of the network twice

doojin: <p>Below is a toy code.<br>
(The actual model and <code>USERFUNC</code> are more complex.)</p>
<pre><code class="lang-auto">def USERFUNC(network_output):
  comp = network_output + 1j*network_output
  comp = comp * 123
  func_out = torch.abs(comp)
  return func_out

model = nn.Linear(1,1)

criterion = nn.MSELoss()

x = torch.randn(1,1)


out = model(x)

out_c = out.clone()

lossA = criterion(out, x)

out_prime = USERFUNC(out_c)

lossB = criterion(out_prime, x)

a = 0.5
loss = lossA + a * lossB
loss.backward()

print(model.weight.grad)
</code></pre>
<p>The toy code seems to work normally, but the actual learning result seems strange.<br>
I think the cause is related to gradient.</p>
<p>If only loss A is applied, it will operate normally, but if loss B is applied, the result will be wrong.<br>
I thought <code>USERFUNC</code> was wrong, but this should not be a problem because I checked that the input of <code>USERFUNC</code> was operated with <code>a = torch.randn (3,256,256, device='cuda', requirements_grad=True)</code>.</p>
<p>What is the problem? I’ve been wandering this problem for months now…<img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"><br>
Please let me know if you need more explanation.</p>
<p>Any response would be appreciated.<br>
Thank you.</p>

Shresth_Somya: <aside class="quote no-group quote-modified" data-username="doojin" data-post="1" data-topic="156169">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/9f8e36/48.png" class="avatar"> doojin:</div>
<blockquote>
<pre><code class="lang-auto">def USERFUNC(network_output):
  comp = network_output + 1j*network_output
  comp = comp * 123
  func_out = comp.abs()
  return func_out

model = nn.Linear(1,1)

criterion = nn.MSELoss()

x = torch.randn(1,1)


out = model(x)

out_c = out.clone()

lossA = criterion(out, x)

out_prime = USERFUNC(out_c)

lossB = criterion(out_prime, x)

a = 0.5
loss = lossA + a * lossB
loss.backward()

print(model.weight.grad)
</code></pre>
</blockquote>
</aside>
<p>What <code>comp = network_output + 1j*network_output</code> this line does?</p>

doojin: <p>Thanks for your reply!</p>
<p>The line is to make it complex number.<br>
(It may seem strange, but I need it…)</p>

Shresth_Somya: <p>here <code>1</code> is constant and <code>j</code> is a variable?</p>

doojin: <p>1 is constant, j is a imaginary <em>i</em>.</p>

Shresth_Somya: <p>replace line</p>
<pre><code class="lang-auto">func_out = comp.abs()
</code></pre>
<p>with this</p>
<pre><code class="lang-auto">func_out = torch.abs(comp)
</code></pre>

doojin: <p>Oh, that’s my typo. I’m already using <code>func_out = torch.abs(comp)</code> as you suggest.<br>
I’m sorry to confuse you.<img src="https://discuss.pytorch.org/images/emoji/apple/sob.png?v=12" title=":sob:" class="emoji" alt=":sob:" loading="lazy" width="20" height="20"></p>

Shresth_Somya: <p>What is the final code and error you are receiving?</p>

doojin: <p>Sorry for replying late.</p>
<p>The error does not occur. It’s just that the learning results are weird.<br>
(There is no final code, but the toy code is the overall framework.)</p>
<p>The meaning of “weird” means that it seems to return the loss to be average as a result of learning.<br>
For example, if the probability model returns 0.5</p>