BatchNorm2d results not matching BatchNorm in libtorch 1.4

botelho: <p>Hello,<br>
I recently upgraded from libtorch 1.2 to 1.4 and, among other things, replaced <code>torch::nn::BatchNorm</code> with <code>torch::nn::BatchNorm2d</code> in my code, since the former is now deprecated. I started seeing vastly different results with the new library, and tracked them to batch norm. Below is a minimal example that already shows the issue.</p>
<p>I would maybe expect some small delta between the two classes, but the differences I’m seeing in large runs are huge, going all the way from training fine to not converging at all. I did not see anything in the documentation that would explain such change in behavior.</p>
<p>Could someone please advise if there was any intentional (possibly undocumented) change in behavior between <code>BatchNorm</code> and <code>BatchNorm2d</code> in libtorch 1.4? Thanks!</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main()
{
    int64_t const C(3), H(4), W(4);
    torch::manual_seed(1);
    auto const input = torch::rand({1, C, H, W});

    /*** The two outputs below should be identical, but are vastly different! ***/
    torch::manual_seed(1);
    std::cout &lt;&lt; torch::nn::BatchNorm(C)-&gt;forward(input) &lt;&lt; std::endl;

    torch::manual_seed(1);
    std::cout &lt;&lt; torch::nn::BatchNorm2d(C)-&gt;forward(input) &lt;&lt; std::endl;

    return 0;
}
</code></pre>
<p>Here’s the output of the above:</p>
<pre><code class="lang-auto">Warning: torch::nn::BatchNorm module is deprecated and will be removed in 1.5. Use BatchNorm{1,2,3}d instead. (BatchNormImpl at ../../torch/csrc/api/src/nn/modules/batchnorm.cpp:21)
(1,1,.,.) = 
  0.9024 -0.8736 -0.4140  0.8172
 -1.8019  1.0592 -0.4361  0.8903
  0.2040 -0.2815  0.4608  0.0375
  0.6239 -0.7776 -0.1895 -0.2213

(1,2,.,.) = 
  0.0272 -0.0509  0.4098  0.1145
 -0.2442 -0.3657 -0.1367 -0.2751
 -0.2169 -0.0237  0.2639  0.2363
 -0.5616  0.2764  0.0978  0.4488

(1,3,.,.) = 
  0.4244 -0.5917 -0.6174 -0.3144
  0.5654 -0.1102  0.2743 -0.3030
  0.6321 -0.2761  0.4825  0.0052
 -0.3432  0.3300 -0.3458  0.1877
[ CPUFloatType{1,3,4,4} ]
(1,1,.,.) = 
  1.1911 -1.1530 -0.5465  1.0787
 -2.3783  1.3981 -0.5756  1.1752
  0.2692 -0.3715  0.6082  0.0494
  0.8235 -1.0264 -0.2501 -0.2921

(1,2,.,.) = 
  0.0975 -0.1822  1.4672  0.4098
 -0.8742 -1.3091 -0.4895 -0.9849
 -0.7766 -0.0848  0.9450  0.8462
 -2.0108  0.9897  0.3500  1.6069

(1,3,.,.) = 
  1.0529 -1.4680 -1.5317 -0.7800
  1.4028 -0.2734  0.6805 -0.7517
  1.5682 -0.6850  1.1971  0.0129
 -0.8514  0.8186 -0.8578  0.4658
[ CPUFloatType{1,3,4,4} ]
</code></pre>

yf225: <p>For the simple repro example, the Python equivalent gives the same output as BatchNorm2d in libtorch 1.4:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.manual_seed(1)
&lt;torch._C.Generator object at 0x7f3d14d2ddb0&gt;
&gt;&gt;&gt; input = torch.rand(1, 3, 4, 4)
&gt;&gt;&gt; torch.manual_seed(1)
&lt;torch._C.Generator object at 0x7f3d14d2ddb0&gt;
&gt;&gt;&gt; torch.nn.BatchNorm2d(3)(input)
tensor([[[[ 1.1911, -1.1530, -0.5465,  1.0787],
          [-2.3783,  1.3981, -0.5756,  1.1752],
          [ 0.2692, -0.3715,  0.6082,  0.0494],
          [ 0.8235, -1.0264, -0.2501, -0.2921]],
 
         [[ 0.0975, -0.1822,  1.4672,  0.4098],
          [-0.8742, -1.3091, -0.4895, -0.9849],
          [-0.7766, -0.0848,  0.9450,  0.8462],
          [-2.0108,  0.9897,  0.3500,  1.6069]],
 
         [[ 1.0529, -1.4680, -1.5317, -0.7800],
          [ 1.4028, -0.2734,  0.6805, -0.7517],
          [ 1.5682, -0.6850,  1.1971,  0.0129],
          [-0.8514,  0.8186, -0.8578,  0.4658]]]],
       grad_fn=&lt;NativeBatchNormBackward&gt;)
</code></pre>
<p>For model convergence in general, have you tried training the equivalent Python model and checking that it converges?</p>

botelho: <p>Hi, thanks for your response.<br>
Interestingly, I get the old <code>BatchNorm</code> answer from Python (I have <code>torch.__version__ = 1.1.0</code>, you’re probably using a more recent version).<br>
This indicates that <strong>both</strong> Python and libtorch changed behavior recently. A simple math shows that the results of the new <code>BatchNorm2d</code> are actually correct. What I would like to understand, though, is what changed between these two. If I know what the old implementation did, hopefully I can understand why my model converged there, but not anymore.</p>

botelho: <p>Answering my own question: apparently there was a change in how the weights (gamma) are initialized from BatchNorm to BatchNorm2d, going from random uniform to a vector of ones. Unfortunately, I don’t think this was documented anywhere and, for reasons I don’t really understand, had a huge impact on convergence for the model I was training. The good news is that I can initialize those weights myself and recover the previous behavior.</p>
<p>Anyway, thanks for your help!</p>

ptrblck: <p>The change of the gamma initialization landed in the <code>1.2</code> release and was mentioned in the <a href="https://github.com/pytorch/pytorch/releases/tag/v1.2.0">release notes</a>.<br>
Were you using a <code>1.2</code>-pre-release?</p>

botelho: <p>Hi, I’m pretty sure I wasn’t using a pre-release version (the build-version file shows <code>1.2.0+cpu</code>).<br>
Assuming I’m note mistaken there, libtorch 1.2.0 doesn’t even seem to have a BatchNorm2d class. Are you sure you’re not talking about Python?</p>

yf225: <p>PyTorch 1.2.0 changed the weight init behavior of BatchNorm2d in Python (with reasons documented in <a href="https://github.com/pytorch/pytorch/issues/12259" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/12259</a>). BatchNorm2d in libtorch is mirroring how BatchNorm2d in Python behaves currently.</p>

botelho: <p>Thanks for the reference! Besides matching Python, initializing the weights to all-ones intuitively seems like the right thing to do – although, technically, this is not suggested in the original paper, and I haven’t seen any study of the effects of BatchNorm initialization on convergence/performance. I’m still puzzled by the huge negative impact this had on my results!</p>