Could you give me some tips to train several decoders

Miguel_Campos: <p>Hello all,</p>
<p>I need to train 3 <code>torch.nn.Module</code> <strong>individually</strong>. For this, instead of using 3 separated <code>torch.optim.Adam</code> I am concatenating their parameters in this way.</p>
<pre><code class="lang-auto">models = torch.nn.ModuleList([net1, net2, net3])
optimizer = torch.optim.Adam(models.parameters(), lr=1e-3)
</code></pre>
<p>As I need to train 3 models, I need to use 3 inputs for training them, one for each model.</p>
<pre><code class="lang-auto">for epoch in range(100): # per epoch
    for features in dataloader: # I have features= [input0, input1, input2], one per each model.
        for id, input in enumerate(features):
            loss = models[id](input) # each model ingests one input from features
            optimizer.zero_grad() # forward
            loss.backward() # backward
            torch.nn.utils.clip_grad_norm_(models[id].parameters(), 1e0) # clipping
            optimizer.step() # adam step
</code></pre>
<p>Then, I tried to simplify the training loop in this way</p>
<pre><code class="lang-auto">for epoch in range(100):
    optimizer.zero_grad() # Zero gradient per epoch == 3 models do one step of training
    for features in dataloader:
        for id, input in enumerate(features): 
            loss = models[id](input) # forward per model
            loss.backward() # backward per model
    
    # Then, clipping the 3 models and step optimizer
    torch.nn.utils.clip_grad_norm_(models.parameters(), 1.)
    optimizer.step()
</code></pre>
<p>For my surprise, after testing the change for 3 trials, the second version always returns a lower performance that the first one.</p>
<p>What am I missing here?</p>
<p>Thanks in advance</p>

AlphaBetaGamma96: <p>Hi <a class="mention" href="/u/miguel_campos">@Miguel_Campos</a>,</p>
<p>What I <em>think</em> might be happening is that when you move the <code>optimizer.zero_grad()</code> outside your dataloader for-loop, you’re accumulating gradients throughout your entire batch. So you first use the gradient of the first example, then add the gradient of the 2nd example to it and so on, then once you’ve used your entire batch you then update your parameters.</p>
<p>In the other case (first example), it seems you take a single example, compute its loss then update your parameters and repeat this for <strong>all</strong> samples in your batch.</p>
<p>So, it seems like the first example is a single-sample mini-batch gradient descent and the second example is full-batch gradient descent.</p>
<p>The reason why the second code performs better may be due to full-batch gradient descent having little noise within its updates, so it can easily minimise the loss but for a single-sample stochastic gradient descent (SGD) it has too much noise to accurately update your model. Usually for SGD a sub-sample of the batch (aka mini-batch) consistently of 10s of samples is used to get a useful update, but still get some noise to not get stuck within a local minima.</p>

Miguel_Campos: <p>Hello <a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a> .</p>
<p>Sorry, I fixed a important mistake in the second version (The position of <code>optimizer.zero_grad()</code>)</p>
<p>Could you take a look at it again and confirm your theory?</p>
<p>As I said, the first version is performing better than the second one.<br>
Thanks</p>

ptrblck: <p>The training loops are not equivalent so a difference in performance would be expected. As <a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a> explained you are accumulating the gradients for an entire epoch in the second approach which is not the case in the first approach.<br>
Also, calling <code>optimiyer.step()</code> in the first approach will update the parameters of all models even of the models which were not used in the current iteration, since <code>Adam</code> creates running stats for each param and will use it for an update even with a zero gradient.</p>

Miguel_Campos: <p>Hello, It seems that you are saying that the second one is the correct way.</p>
<ul>
<li>If yes, why could be the first method performing better?</li>
<li>How can it be possible that Adam updates the parameters of a not used model?</li>
<li>Should I create 3 different optimizers to train the models</li>
<li>Maybe you could suggest me the correct way to do this training.</li>
</ul>
<p><a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a> said “ The reason why the second code performs better” but it is the opposite case.</p>
<p>Thanks</p>

ptrblck: <blockquote>
<p>It seems that you are saying that the second one is the correct way.</p>
</blockquote>
<p>No, as the first approach is the common training loop (besides iterating all samples in the nested <code>features</code> loop).</p>
<blockquote>
<p>If yes, why could be the first method performing better?</p>
</blockquote>
<p>Stochastic gradient descent empirically performs better than gradient descent (using the entire dataset).</p>
<blockquote>
<p>How can it be possible that Adam updates the parameters of a not used model?</p>
</blockquote>
<p>Once a parameter was updated, <code>Adam</code> will create running stats and will use these to update the parameter even if the gradient is <code>0.</code>. Set the <code>.grad</code> attribute to <code>None</code> and <code>Adam</code> will skip the update.</p>

Miguel_Campos: <p>Thank you for all the replies.</p>
<p>Last question is, having 3 models, would you create 3 individual optimizers?</p>

ptrblck: <p>It depends on the use case and how each model should be updated. I.e. if you want to update models separately based on some conditions, I would create 3 separate optimizers and call them explicitly. On the other hand, if you are using a standard training loop where all models are used, get gradients, and should be updated, you could also stick to one optimizer.</p>