Problem with forwarding two datasets simultaneously

username: anirudhk686
cooked: <p>I have a simple CNN network with 2 parts, extractor(CNN) and classifier(Fully connected layers).<br>
Its a binary classification problem.<br>
Scenario 1:<br>
When I train using only source_dataset as shown in code below, the network trains very well.<br>
code snip:</p>
<pre><code class="lang-auto">source_feature = extractor(source_eeg)
classifier_out = classifier(source_feature)
class_loss = criterion(classifier_out, source_main_labels)
loss = class_loss
loss.backward()
optimizer.step()
optimizer.zero_grad()
</code></pre>
<p>output:</p>
<pre><code class="lang-auto">epoch: 1
source_train |  class_loss: 0.689  class_accu: 0.544
epoch: 2
source_train |  class_loss: 0.658  class_accu: 0.611
epoch: 3
source_train |  class_loss: 0.626  class_accu: 0.679
epoch: 4
source_train |  class_loss: 0.602  class_accu: 0.744
epoch: 5
source_train |  class_loss: 0.568  class_accu: 0.802
epoch: 6
source_train |  class_loss: 0.556  class_accu: 0.873
epoch: 7
source_train |  class_loss: 0.567  class_accu: 0.892
epoch: 8
source_train |  class_loss: 0.551  class_accu: 0.921
epoch: 9
source_train |  class_loss: 0.518  class_accu: 0.937
</code></pre>
<p>Scenario 2:</p>
<p>When I even just forward another dataset along with the above code , the model does not learn.<br>
Code:</p>
<pre><code class="lang-auto">source_feature = extractor(source_eeg)
classifier_out = classifier(source_feature)
class_loss = criterion(classifier_out, source_main_labels)

target_feature = extractor(target_eeg)

loss = class_loss
loss.backward()
optimizer.step()
optimizer.zero_grad()
</code></pre>
<p>train output:</p>
<pre><code class="lang-auto">epoch: 1
source_train |  class_loss: 0.692  class_accu: 0.521
epoch: 2
source_train |  class_loss: 0.693  class_accu: 0.533
epoch: 3
source_train |  class_loss: 0.691  class_accu: 0.521
epoch: 4
source_train |  class_loss: 0.693  class_accu: 0.478
epoch: 5
source_train |  class_loss: 0.693  class_accu: 0.470
epoch: 6
source_train |  class_loss: 0.693  class_accu: 0.465
epoch: 7
source_train |  class_loss: 0.693  class_accu: 0.465
epoch: 8
source_train |  class_loss: 0.693  class_accu: 0.465
epoch: 9
source_train |  class_loss: 0.693  class_accu: 0.465
</code></pre>
<p>The problem is that training of first dataset (source) is getting affected even by just having a line to forward pass the second dataset.</p>
<p>EDIT: In the above code i understand that there is no use of forwarding the 2nd dataset without backpropagating the loss, and i intend to do that in future, but my query was that why just forwarding the second dataset is affecting the learning of the first dataset as well.</p>
<p>Thanks for the help!</p>

username: anirudhk686
cooked: <p>So my friend helped me find the culprit. the problem arises due to batchnorm layer. the batch stats are used in training but the whole dataset stat is used while validation and this is calculated during training. hence the forward pass of second dataset messes with the dataset stats calculated.<br>
This blog article should be helpful(tough its written for tf I think the problem is same): <a href="https://towardsdatascience.com/pitfalls-of-batch-norm-in-tensorflow-and-sanity-checks-for-training-networks-e86c207548c8" rel="nofollow noopener">https://towardsdatascience.com/pitfalls-of-batch-norm-in-tensorflow-and-sanity-checks-for-training-networks-e86c207548c8</a></p>