Can you have for loops in the forward prop?

George3d6: <p>I’ve previously written code such as</p>
<pre><code class="lang-auto">def forward(self, input):
  out1 = network1(input1)
  out2 = network2(input2)
  embedded_input = torch.cat((out1, out2),1)
  output = net(embedded_input)
</code></pre>
<p>And torch/autograd seems to know how to build the backprop graph in order to train this network.</p>
<p>However, if I define my operations in a for loop, rather than linearly, such as:</p>
<pre><code class="lang-auto">def forward(self, input):
  embedded_input = None
  for i, network in embedding_networks:
    out = embedding_networks(input[i])
    if embedded_input is None:
        embedded_input = out
    else:
        embedded_input = torch.cat((embedded_input, out),1)

  output = net(input)
</code></pre>
<p>The forward/backwards prop work, but when I look at the parameter of my network (using .parameters() and iterating through their .shap) it seems that the parameters only include the final <code>net</code> object and not all the objects in the <code>embedding_networks</code> list through which I first pass my input.</p>
<p>Is this to be expected ? Is there something obviously wrong with the second snippet compared to the next ? How would I best achieve something like what’s shown in the second snippet ?</p>

George3d6: <p>Ok, so I think I’ve found what I was doing wrong, <code>embedding_networks</code> was a python <code>list</code> and instead it should have been a <code>torch.nn.ModuleList</code> (<a href="https://pytorch.org/docs/stable/nn.html#torch.nn.ModuleList" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html#torch.nn.ModuleList</a>)</p>
<p>It seems to work once I made that change, still need to validate that it’s actually training (as in, that the backwards prop graph has the correct linking inside of it), but barring that, just by printing the parameters it looks fine.</p>

G.M: <p>The loop should work fine, but definitely slower than the non-loop version.</p>

George3d6: <p><a class="mention" href="/u/g.m">@G.M</a></p>
<p>Is that because the code is not in-lined ? Would there be any easy way to un-roll the loop so that pytorch can better optimize it ?</p>
<p>The amount of networks I need is determined once and stays the same.</p>

G.M: <p>Python loops are slow, u can write it in C++ instead. It is better to use Pytorch methods to replace loops in your code. Another way to speed things up is to use TouchScript(jit).</p>

George3d6: <p><a class="mention" href="/u/g.m">@G.M</a>  The problem with writing in C++ is that I have to distribute this as a library, so binary blobs might cause their own issues, especially since I distribute it as setup on machine, rather than as a wheel.</p>
<p>Would you mind explaining how to replace this kind of loop with a Pytroch method ?</p>

G.M: <aside class="quote no-group" data-username="George3d6" data-post="1" data-topic="68295">
<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/george3d6/48/21758_2.png" class="avatar"> George3d6:</div>
<blockquote>
<p>ers</p>
</blockquote>
</aside>
<p>Didn’t u already have a non-loop version in your first post?<br>
Sadly, not all loops can be replaced by Pytorch methods. In this case, the loops is fine because u r only iterating through a list of models, not using iteration inside the algorithm.</p>
<p>Also, if ur <code>embedding_networks</code> is a sequence of models, I guess u should change this line</p>
<pre><code class="lang-auto">for i, network in embedding_networks:
</code></pre>
<p>to</p>
<pre><code class="lang-auto">for i, network in enumerate(embedding_networks):
</code></pre>
<p>.</p>

George3d6: <p>That was a mistake when re-writing the code, it did originally use enumerate.</p>
<p>Sadly enough the number of networks is indeterminate at code-writing time. I was assuming pytroch had some alterantive to for loops. There to seem to be some functionalities for distributing a list of processes between GPUs, so I might end up using that.</p>
<p>Thanks for your help <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

David_Choqueluque: <p>Hi. What about the backprop? Can you share that code?</p>