After model.eval(), then set bn_state to false?

cuixing158_1: <p>I used BN and dropout layers in my model. Do I still need to set the track_running_stats=false of BN after inference model.eval() is executed? When I execute both, the verification result is similar to the execution of one result. Does this mean that it is not necessary to set the status?</p>

ptrblck: <aside class="quote no-group" data-username="cuixing158_1" data-post="1" data-topic="88202">
<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/cuixing158_1/48/12146_2.png" class="avatar"> cuixing158_1:</div>
<blockquote>
<p>Do I still need to set the track_running_stats=false of BN after inference model.eval() is executed?</p>
</blockquote>
</aside>
<p>No, this argument is only used during the initialization of the batchnorm layer to define, if the running statistics should be tracked or not.<br>
Changing this attribute later should not have any effect.</p>
<p>You would only have to call <code>.train()</code> and <code>.eval()</code> to switch between the different behaviors.</p>

alisher0717: <p>Recently I have found an interesting behavior (may be I am doing something wrong…) during the ResNet training.</p>
<p>When I apply model.eval() [with torch.no_grad()] and evaluate on validation set and switch back to model.train(), the training loss increases suddenly (gets significantly bigger than the previous training losses). But this the case for the first model.eval() only…</p>
<p>I was wondering if model.eval() resets BN statistics (moving mean and moving variance)?</p>

ptrblck: <aside class="quote no-group" data-username="alisher0717" data-post="3" data-topic="88202">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/ecae2f/48.png" class="avatar"> alisher0717:</div>
<blockquote>
<p>I was wondering if model.eval() resets BN statistics (moving mean and moving variance)?</p>
</blockquote>
</aside>
<p>No, that’s not the case.<br>
Is this issue reproducible for different seeds?</p>

alisher0717: <p>Thank you so much for the reply. It is good to know that model.eval() does not change BN statistics.</p>
<p>As for my problem, I think I had a fundamental issue in my training. Now I am not facing the previous issue.</p>

ptrblck: <p>Good to hear the issue is solved. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<aside class="quote no-group" data-username="alisher0717" data-post="5" data-topic="88202">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/ecae2f/48.png" class="avatar"> alisher0717:</div>
<blockquote>
<p>It is good to know that model.eval() does not change BN statistics.</p>
</blockquote>
</aside>
<p>Just to be avoid a potential misunderstanding: <code>model.eval()</code> will use the running statistics instead of the batch statistics, but it won’t change (or reset) the running statistics.</p>