Adding dropout after training

segum: <p>Hi all,</p>
<p>I have a question for you. Do you know if it is possible to add dropout layers to an already trained neural network? I have a model trained without dropout, but I want to use dropout at inference time to estimate uncertainty.</p>
<p>I managed to copy the weights from my trained model into a model with dropout layers, but predictions are messed up. This is probably because in PyTorch when dropout is set in train mode it will divide the weights by 1-p.</p>
<p>Do you know if there is any way to deal with this issue? Or it is simply wrong to add dropout to an already trained model without retraining it?</p>
<p>Thanks in advance for your help</p>

alex.veuthey: <p>Indeed, training with dropout needs to account for scaling, so the strategy is to divide the weights by <code>1/p</code> after training or multiply the weights by <code>1/p</code> during training (I don’t know which one PyTorch uses).</p>
<p>If you need to apply Dropout during inference, you therefore need to compensate for the missing nodes in the network by dividing the weights by <code>1/p</code> on the affected layers. It will obviously produce wrong results, but that is what you want to measure.</p>
<p>E.g: if <code>p = 1/5</code>, you multiply the weights by <code>5</code>, and this simulates having all nodes active in the layer. See <a href="https://stats.stackexchange.com/questions/251363/purpose-of-scaling-weights-states-when-using-dropout-in-a-neural-network" rel="nofollow noopener">this answer</a> for more details.</p>

segum: <p>Exactly, what PyTorch does is to account for scaling at training time (multiplies the weights at training time by 1/(1-p_drop) and leaves weights untouched at test time).</p>
<p>It means that if I add dropout layers in between in my trained model and at test time I apply dropout as in train mode, PyTorch directly scales correctly the weights to account for missing nodes.</p>
<p>Now, my intuition is that the results are worse than what I expected because network weights were not trained to be “robust”, because dropout layers were not present during training. Hence, since by adding dropout at test time I get bad predictions, this means that my model is extremely uncertain about its predictions (I am using Monte Carlo dropout to estimate uncertainty).</p>
<p>I don’t know if you are familiar with this technique, in case do you think that my deduction is correct?</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>

alex.veuthey: <p>I haven’t used this approach in practice, but I can understand the procedure. I doubt you can measure confidence using dropout however, but you can evaluate the resilience to co-adaptation between neurons, and therefore the regularization of the network…</p>
<p>As to the actual procedure, I think you really need to scale by hand, because as you said, PyTorch doesn’t do anything with dropout at test time. Therefore, you will have fewer nodes in the layer, which won’t contribute as much to the next layer’s input as in the default network. This will produce very different results than the untouched network.</p>
<p>Note that it’s easier to use <code>nn.functional.dropout</code> if you want it active during evaluation, as <code>model.eval()</code> will deactivate the <code>nn.Dropout</code> layers…</p>