Multi loss backprop for mulriple actions

ixombi: <p>Inspired by <a href="https://arxiv.org/pdf/1711.08946.pdf" rel="noopener nofollow ugc">https://arxiv.org/pdf/1711.08946.pdf</a> and <a href="https://s3-us-west-2.amazonaws.com/openai-assets/dota_benchmark_results/network_diagram_08_06_2018.pdf" rel="noopener nofollow ugc">https://s3-us-west-2.amazonaws.com/openai-assets/dota_benchmark_results/network_diagram_08_06_2018.pdf</a> I’m writing a reinforcement learning program and I’m implementing multiple action heads sharing a common network.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/0/d07dfaa5ad0b51d737a7cb582759ac13c3c98d1a.png" alt="image" data-base62-sha1="tKpdOtRLKPrLmGB583H3fX2psH8" width="374" height="309"></p>
<p>I want to propagate loss1, loss2 and loss3 to the output of the shared network and MSE(loss1,loss2,loss3) from the output of the shared network all the way back. The shared network and each of the action head networks have their own network classes and torch.optim.</p>
<p>I’ve tried</p>
<pre><code class="lang-auto">optim_head1.zero_grad(retain_graph=False)
loss1 = loss_fn(x1,y1)
loss1.backward()
optim_head1.step()

:

optim_head2.zero_grad(retain_graph=False)
loss2 = loss_fn(x2,y2)
loss2.backward()
optim_head2.step()

:

optim_head3.zero_grad(retain_graph=False)
loss3 = loss_fn(x3,y3)
loss3.backward()
optim_head3.step()
</code></pre>
<p>But I’m getting this error during the second backward() call</p>
<blockquote>
<p>“Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.”</p>
</blockquote>
<p>How can I do this? What am I not understanding about how Pytorch performs backprop? Thanks.</p>

suraj.pt: <p>You can combine the losses from all heads, and backprop that using a single optimizer that is initialized with all the parameters in your model (shared network and each of the action heads). Read more about <a href="https://towardsdatascience.com/deep-multi-task-learning-3-lessons-learned-7d0193d71fd6" rel="noopener nofollow ugc">backpropagating loss in multi-task learning architectures</a>.</p>
<p>So your code might look like</p>
<pre><code class="lang-auto">optim = optimizer(full_model.parameters(), ...)
combined_loss = weighted_sum(loss1, ... lossN)
optim.zero_grad()
combined_loss.backward()
optim.step()
</code></pre>
<p><code>Action_head1</code> will be updated by gradients only w.r.t. <code>loss1</code> (and so on), and the shared network will be updated by gradients wrt to all losses.</p>

ixombi: <p>Thank you sir. I had tried that before but I couldn’t make my agent learn properly, so that’s why I was trying something different. Is there a good reference that explains how .backward() and .step() actually do their magic? This is the most difficult aspect of Pytorch for me to understand. Thanks.</p>

suraj.pt: <p>You’re welcome! It can be a bit tricky to wrap your head around what’s happening at first under the hood, but very simply:</p>
<ol>
<li>autograd creates a graph of all your model’s operations</li>
<li>when you call backward, it traverse this graph and collects gradients (using <a href="https://www.youtube.com/watch?v=tIeHLnjs5U8" rel="noopener nofollow ugc">backpropagation</a>). once it reaches the end of the graph, it is released from memory. that’s why you get the error on the second backward() call</li>
<li>when you call step(), every parameter is updated by the gradients collected in the backward() pass.</li>
</ol>
<p><a href="https://pytorch.org/docs/stable/notes/autograd.html" rel="noopener nofollow ugc">More detail on how autograd works</a></p>