Unused model parameters affect optimization for Adam

dem123456789: <p>I recently encounter a situation where some of the model parameters will not be updated during certain iterations. The unused parameters are those are not in computation graph (after backward(), the gradients of those unused parameters is None)</p>
<p>I find the training result is different when I do not have those unused parameters. The only reason I could think of is the optimizer Adam and maybe other adaptive learning rate optimization. Because those optimizer take all model parameters as input when initialized but I only update part of them.</p>
<p>Anyone knows how should I resolve this issue. I am not sure the reason I propose above is correct or not.</p>

ptrblck: <p>Could you post a small code snippet showing how these “unused” parameters are created?<br>
I would like to reproduce this issue and debug a bit as currently I can only speculate about the reason.</p>

dem123456789: <p>For example, I have a maximum number of layers of nn.Conv2d. However, the forward pass can use first few layers in computation. How many layers I need to use is an input of the forward pass. In other words, the depth of model is dynamic. For now, I only test with a fixed depth which is smaller than the maximum depth. Those layers of nn.Conv2d are created with a for loop and stored into a list. Then I use nn.ModuleList to wrap the list. I am not sure if this phenomenon indeed exists.</p>

ptrblck: <p>As long as you store the <code>Modules</code> in a <code>ModuleList</code> they should be properly registered.<br>
Are these modules missing in the <code>state_dict</code>?<br>
If so, could you post the model definition?</p>

dem123456789: <p>They are in the state_dict().</p>

ptrblck: <p>Sorry! Now I get the issue.<br>
Your parameters are all properly registered but unused.<br>
In the case where unused parameters (never called in <code>forward</code>) are in the model, the performance is worse compared to the plain model without the unnecessary parameters. Let me try to debug it.</p>

dem123456789: <p>Thanks you so much.<br>
Sorry for the confusion I made.<br>
I am not sure this phenomenon exists or this is just an illusion.<br>
I use Adam as optimizer and pytorch 0.4.1.</p>

ptrblck: <p>I created a small code snippet comparing two models. One model uses all its modules, while the other one has some unused modules.<br>
The code passes for <code>0.4.1</code> and <code>1.0.0.dev20181014</code>.<br>
You can find the code <a href="https://gist.github.com/ptrblck/e78b59052f4bea6f91ec35f9f15adfb4">here</a>.</p>
<p>Let me know, if you can spot some differences between your and mine implementation.</p>

dem123456789: <p>I actually modify a little bit of your code and I can reproduce the error with nn.ModuelList().<br>
I also try SGD, and it has the same error.<br>
Can you help me verifying this?<br>
You can find the code <a href="https://gist.github.com/dem123456789/1204cc684ec11b20b0cc0126e5f4b1b7" rel="nofollow noopener">here</a></p>

ptrblck: <p>Thanks for the code update!<br>
The difference in your implementation is due to the order of instantiation of the layers. Since you are creating the unused conv layers before the linear layer, the PRNG will be have additional calls and the weights of the linear layers will differ.<br>
If you change the <code>__init__</code> method of <code>MyModuleUnused</code> to</p>
<pre><code class="lang-python">        super(MyModelUnused, self).__init__()
        self.conv_list = nn.ModuleList()
        self.conv_list.append(nn.Conv2d(3, 6, 3, 1, 1))
        self.conv_list.append(nn.Conv2d(6, 12, 3, 1, 1))
        self.pool1 = nn.MaxPool2d(2)
        self.pool2 = nn.MaxPool2d(2)
        self.fc = nn.Linear(12*6*6, 2)
        self.conv_list.append(nn.Conv2d(12, 24, 3, 1, 1))
        self.conv_list.append(nn.Conv2d(24, 12, 3, 1, 1))
</code></pre>
<p>, you’ll get the same results again.<br>
Alternatively, you could set the seed before instantiating each layer.</p>

dem123456789: <p>Can you explan what is PRNG? I am still confused. Why the ordering of the initialization matters?<br>
I never saw a documentation on the ordering of module intialization. Should I always initilize ModuleList first? Is there a disciplinary way of doing this so that I can avoid this kind of problem?</p>

ptrblck: <p>Sorry for being not clear enough.<br>
By PRNG I mean the <a href="https://en.wikipedia.org/wiki/Pseudorandom_number_generator">Pseudorandom Number Generator</a>.<br>
The ordering just matters for the sake of debugging, as we are dealing with pseudorandom numbers.</p>
<p>In order to compare the weights and gradients, we should make sure both models have the same parameters.<br>
One way would be to initialize one model and copy the parameters into the other.<br>
Another way is to seed the PRNG for both models and just sample the same “random” numbers.</p>
<p>You can think about seeding the random number generation as setting a start value. All “random” numbers will be the same after setting the same seed:</p>
<pre><code class="lang-python">torch.manual_seed(2809)
print(torch.randn(5))
&gt; tensor([-2.0748,  0.8152, -1.1281,  0.8386, -0.4471])
print(torch.randn(5))
&gt; tensor([-0.5538, -0.8776, -0.5635,  0.5434, -0.8192])

torch.manual_seed(2809)
print(torch.randn(5))
&gt; tensor([-2.0748,  0.8152, -1.1281,  0.8386, -0.4471])
print(torch.randn(5))
&gt; tensor([-0.5538, -0.8776, -0.5635,  0.5434, -0.8192])
</code></pre>
<p>Although we call <code>torch.randn</code>, we get the same “random” numbers in the successive calls.<br>
Now if you add the unused layers before the linear layer, the PRNG will get an additional call to sample the parameters of these layers, which will influence the linear layer parameters.</p>
<p>Ususally, you don’t have to think about these issues. As I said, it’s just to debug your issue.</p>

dem123456789: <p>Thank you so much.<br>
I move the module_list after all other modules and it works.<br>
If I understand it correctly, since it only affects PRNG, it shoud not create performance issue. However, the performance will slightly differ for each run because of PRNG even if we seed in advance.</p>

ptrblck: <p>Are you getting the same or comparable results now?<br>
I still think the unused parameters are no problem for the optimizer and your results should be comparable.<br>
Note that I performed the tests on CPU and seeded unusually often just for the sake of debugging.<br>
If you don’t properly seed or use the GPU with non-deterministic operations, you will get slight differences.</p>

dem123456789: <p>After I move conv_list behind all other modules, it passes the test.<br>
However, I m not sure this is valid in other cases.<br>
For example I have an encoder and decoder and either of them will have variant depth. Then I have a large wrapper module for the encoder and decoder. Even if I move the initialization of conv_list in encoder and decoder itself. It might still have problems because of the wrapper module.</p>
<p>For the seed before instantiating each layer, could you give me an example?</p>
<p>I understand I will get differences but I am just wondering will this effect degrades the performance in general. If this only affects debugging, it might not be a big issue.</p>

ptrblck: <p>I think the seeding approach is getting cumbersome for more complex use cases.<br>
Let’s just initialize the model with unused modules and load the parameters in the “complete” model.<br>
Could you add this code to the gist and compare the results again?</p>
<pre><code class="lang-python">def copy_params(modelA, modelB):
    modelA_dict = modelA.state_dict()
    modelB_dict = modelB.state_dict()
    equal_dict = {k: v for k, v in modelB_dict.items() if k in modelA_dict}
    modelA.load_state_dict(equal_dict)



modelA = MyModel()
modelB = MyModelUnused()

copy_params(modelA, modelB)

# Check weights for equality
check_params(modelA, modelB)
</code></pre>

dem123456789: <p>The results are the same if we copy the parameters.<br>
I find another problem. If I have two module_list each initialized with two different methods, moving the module_list behind fc layer still has errors.<br>
You can see the code <a href="https://gist.github.com/dem123456789/1204cc684ec11b20b0cc0126e5f4b1b7" rel="nofollow noopener">here</a></p>

ptrblck: <p>I’ve checked your code and it seems you are copying the parameters between models and the seeding approach later. Just remove the second approach, as it’s not a good fit anymore regarding the construction of your models.<br>
Remove these lines and you’ll get the same results:</p>
<pre><code class="lang-python">torch.manual_seed(2809)
modelA = MyModel()
torch.manual_seed(2809)
modelB = MyModelUnused()
</code></pre>

dem123456789: <p>So if I do not manually seed, there will not be any problems?</p>

ptrblck: <p>No, the manual seed is not the issue. I’ve just used it in my first example to show, that the optimizer does not have any problems optimizing a model with unused parameters.<br>
Even if we copy all parameters between models, the optimizer works identically.</p>
<p>So back to your original question. The discrepancy you are observing is not due to some unused parameters in your model. However, if your whole training procedure is very sensitive to the initialization, and therefore to the seeding as well, you might get these results.</p>
<p>To debug the problem I would suggest to try the following:</p>
<ul>
<li>Compare the results of your “good” model using several random seeds at the beginning of your script. If tht accuracy stays approx. the same, it should be alright. If you see an accuracy drop for different seeds, I would suggest to use some weight init functions and see it we can stabilize the performance.</li>
<li>Create your good model and save the <code>state_dict</code> after initializing the model. Create another script using your model containing unused layers and load the <code>state_dict</code> for the common layers. Then train this model and see, how the performance is compared to the initial model.</li>
</ul>
<p>I’m still in doubt the optimizer is causing this issue.</p>