Use one Module to provide loss function for another Module

username: turian
cooked: <p>I am using one trained Module (E) to provide the loss function for the output of another Module (M).<br>
Of course, I would like the errors specifically back-propagated through the specific features of M’s output.</p>
<p>However, I don’t want E to update its parameters at all.</p>
<p>In the <strong>init</strong> of M, I have tried:</p>
<pre><code class="lang-auto">        for param in self.E.parameters():
            param.requires_grad = False
</code></pre>
<p>and also</p>
<pre><code class="lang-auto">            param.detach()
</code></pre>
<p>but after training M a bit, I see that E has been updated. I test this before and after training M by passing some dummy input to E, and seeing that it has changed.</p>
<p>How can I backprop errors from E to M, but keep E fixed?</p>

username: turian
cooked: <p>By the way, I have searched the forums and found some related posts, but not the answer to my question. For example:</p>
<aside class="quote" data-post="1" data-topic="61285">
  <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/kuppharish/48/13969_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/using-output-of-one-network-in-another-with-only-one-loss-function/61285">Using output of one network in another with only one loss function</a> <a class="badge-category__wrapper " href="/c/autograd/7"><span data-category-id="7" data-drop-close="true" class="badge-category " title="A category of posts relating to the autograd engine itself."><span class="badge-category__name">autograd</span></span></a>
  </div>
  <blockquote>
    I am using two neural networks say NN1 and NN2. Output of NN1 is used in forward function of NN2 and output of forward function of NN2 is the loss required. So in the training loop if I write 
loss = NN2(data) 
loss.backward()

Will this update the weights of both NN1 and NN2?
  </blockquote>
</aside>
<aside class="quote" data-post="12" data-topic="48293">
  <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/ptrblck/48/1823_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-can-i-connect-a-new-neural-network-after-a-trained-neural-network-and-optimize-them-together/48293/12">How can I connect a new neural network after a trained neural network and optimize them together</a> 
  </div>
  <blockquote>
    Ah OK, thanks for the information. 
You can get the kernel size using conv.kernel_size and store it in your config file.
  </blockquote>
</aside>


username: turian
cooked: <p>So here’s what I discovered.</p>
<pre><code class="lang-auto">        for param in self.E.parameters():
            param.requires_grad = False
</code></pre>
<p>works, except that even though I run E.eval() before passing it into M, somehow it gets into state E.train() during M’s training, and thus dropout is enabled. If I run E.eval() every time before I call E to do evaluation, it is fine. I don’t really understand why this is, if someone can explain.</p>

username: Scott_Hoang
cooked: <p>This sound like transfer learning with extra steps. Why not simply transfer the trained portion of E to M and freeze it?</p>

username: turian
cooked: <p>Can you explain what you mean?<br>
The simplest thing would be not to copy the weights and the network structure, but instead just to pass E into M</p>