Pytorch nn.Module: Surprising model improvement by changing init

Kath_Pra: <p>Hello everyone,<br>
I am currently running experiments with a model, trying to improve its performance. I noticed that the model accuracy increases, when I add one new layer (convf) in the init function of my class. This is the case, even though I never use this layer in my forward function, so the data should never get in contact with it.</p>
<p>Can someone explain to me, why this is the case?</p>
<p>Many thanks in advance,<br>
Katharina</p>
<p>My code:</p>
<pre><code>class four_unit_tcn(nn.Module):

def __init__(self, in_channels, out_channels, kernel_size=5, stride=1):
    super(four_unit_tcn, self).__init__()
    pad = int((kernel_size - 1) / 2)
    self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=(kernel_size, 1), padding=(pad, 
                       0), stride=(stride, 1))
    **self.convf = nn.Conv2d(in_channels, out_channels, kernel_size=1, padding=0, stride=(stride, 1))**
    self.bn = nn.BatchNorm2d(out_channels)
    self.bnf = nn.BatchNorm2d(out_channels)
    self.relu = nn.ReLU(inplace=True)
    conv_init(self.conv)
    bn_init(self.bn, 1)

def forward(self, x):
    fourier_input = torch.clone(x)
    fourier_2 = torch.fft.fft(fourier_input,dim=2)
    fourier_2 = fourier_2.abs() 
    fourier_2 = self.bnf(fourier_input)
    x = self.bn(self.conv(x))
    x = torch.cat([x,fourier_2], dim =2)
    return x</code></pre>

Andrei_Cristea: <p>Can you show some data on performance with and without <code>self.convf</code> in <code>__init__</code>? Maybe train 10 times with and without for a set amount of epochs and record the distribution of validation losses in both cases so we can compare.</p>
<p>It would be very surprising for this to make a difference, and I’m guessing what you saw was just coincidental due to the stochastic nature of (randomness inherent in) training.</p>

Kath_Pra: <p><a class="mention" href="/u/andrei_cristea">@Andrei_Cristea</a> thank you for your quick response! It should not affect the model, I agree with you!</p>
<pre><code>def conv_init(conv):

      if conv.weight is not None:
            nn.init.kaiming_normal_(conv.weight, mode='fan_out')
      if conv.bias is not None:
            nn.init.constant_(conv.bias, 0)
</code></pre>
<p>I am initializing the model weights to reduce to the amount of randomness during training. Below I posted the result that I repeatedly got from training, after 10 epochs. I am training for 65 epochs in total where the model incl. convf achieves 0.841 accuracy in comparison to 0.837 of the model w/o convf.</p>
<p><strong>Without the convf layer the results of epoch 10 is this</strong>:<br>
Training epoch: 10<br>
Mean training loss: 0.8802.  Mean training acc: 73.49%.<br>
Time consumption: [Data]04%, [Network]95%<br>
Eval epoch: 10<br>
Mean test loss of 796 batches: 1.0756102995806602.<br>
Top1: 68.88%<br>
Top5: 91.26%</p>
<p><strong>With the convf layer in init but not in forward the results of epoch 10 is this</strong>:<br>
Training epoch: 10<br>
Mean training loss: 0.8689.  Mean training acc: 74.03%.<br>
Time consumption: [Data]02%, [Network]96%<br>
Eval epoch: 10<br>
Mean test loss of 796 batches: 1.1364069590017425.<br>
Top1: 67.30%<br>
Top5: 91.09%</p>
<p>This is (part of) my environment:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/2/9248c7c0dcf1797fda4e3b74611c59ff4abb721e.png" data-download-href="https://discuss.pytorch.org/uploads/default/9248c7c0dcf1797fda4e3b74611c59ff4abb721e" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/2/9248c7c0dcf1797fda4e3b74611c59ff4abb721e.png" alt="image" data-base62-sha1="kS5DMOl3evHBgz6LD7EpWWLjThc" width="690" height="57" data-dominant-color="2B2B2B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">737×61 2.25 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

Andrei_Cristea: <p>Could you repeat this experiment a number of times (in a loop, storing just the final losses and accuracies for comparison)? I don’t know your exact data but it seems plausible to me that there is some amount of randomness in the performance such that you will see sim-to-sim random variation in the losses and accuracies, on the order of this difference. In other words, can we rule out that this difference is just random, and not “statistically insignificant”?</p>
<p>Also, it looks to me like the training loss is better with the convf layer, but the test loss (and test accuracy) is <em>worse</em>:</p>
<ul>
<li>training loss goes from 0.8802 to 0.8689, so it improves</li>
<li>test loss goes from 1.0756 to 1.1364, so it worsens</li>
</ul>

Kath_Pra: <p>I repeated the experiments for multiple times and each model produced exactly the same results due to fixed seed during random initilization.</p>
<p>My thesis supervisor  helped me to understand the matter: Even though the seed is fixed, the initalization of parameters differs, when you increase the number of parameters. This explains the difference in the training behaviour and final result.</p>
<p>Thank you a lot for your support!</p>