Multi-task multi-loss learning

Alva-2020: <p>Hello, I have one multi-task multi-loss problem when I implement one multi-task classification problem.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/5/5/55b3b1f6ba67580167f682c4ce81971e9bd3b4dd.png" alt="Untitled Diagram.drawio" data-base62-sha1="ce9zXY0qsqoJIeHVwId7PWMsX7v" width="201" height="331"></p>
<pre><code class="lang-auto">self.optimizer_task1 = torch.optim.Adam([{'params':self.modules['shared_model'].parameters()},{'params': self.modules['net1'].parameters()}])
self.optimizer_task2 = torch.optim.Adam([{'params':self.modules['shared_model'].parameters()},{'params': self.modules['net2'].parameters()}])
......
self.module.train()
loss1.backward()
self.optimizer_task1.step()
self.optimizer_task1.zero_grad()

loss2.backward()
self.optimizer_task2.step()
self.optimizer_task2.zero_grad()

</code></pre>
<p>When the codes run to the line &lt;loss2.backward()&gt;, there will be an error “RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [128]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).”. I wonder if the two optimizer could have the same part ‘shared_net’</p>

JuanFMontesinos: <p>No, backprop works at a tensor level.<br>
You should call loss1.backward(retain_graph=True)</p>
<p>Or you can backward the sum of both (which makes more sense usually )</p>

Alva-2020: <p>Thank you for your reply. I have tried loss1.backward(retain_graph=True), but there is the same error.<br>
If I use the loss = loss1 + loss2 to optimize the net1, net2, shared-net with one optimizer, will the gradient update of net1 be affected by the loss2 of the net2 part?</p>

JuanFMontesinos: <p>Oh sorry I think I read a totally different thing.<br>
The in-place computation means you modified a variable by manually changing its data. For example if you write <code>w[7]=18</code> in the forward pass, in-place sums or similar things. Here you have a more elaborated explanation.</p><aside class="quote" data-post="1" data-topic="16244">
  <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/rickyim/48/29677_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/what-is-in-place-operation/16244">What is `in-place operation`?</a> 
  </div>
  <blockquote>
    I came across the term in-place operation in the 
documentation <a href="http://pytorch.org/docs/master/notes/autograd.html" rel="noopener nofollow ugc">http://pytorch.org/docs/master/notes/autograd.html</a> What does it mean?
  </blockquote>
</aside>
<p>
Would you mind to post the code of the networks? I may identify it visually.</p>
<p>Wrt your other question no.  Think that the gradient of the sum depends on each term only. This way the shared network gets a proper optimization as the gradient will be optimized jointly. Otherwise the shard net gets their parameters updated, then the error from loss2 was computed with the previous weights and they kinda fight to optimize the problems separately</p>

wxr5899: <p>Hello, did you solve the problem? I have the same problem too! When there are multi loss items, and they share the same backbone, if sum all losses together, will one loss have an influence on other networks?</p>