New model means new optimizer?

username: LeErnst
cooked: <p>Hello together,</p>
<p>lets say we have a <code>ReLU-NN</code> class and for the training phase a <code>training_backend</code> class which handles all the optimization data and so on. Within the <code>training_backend</code> <code>__init__</code> method there are the following assignments:</p>
<pre><code class="lang-auto">self.model = model # a NN-model of class ReLU-NN
self.optimizer = torch.optim.LBFGS(self.model.parameters(),...)
def closure():
      ...
      nn_output = self.model(self.mu_tr)
      ...
      return loss
self.closure = closure
</code></pre>
<p>The <code>training_backend</code> has also the following method to train the assigned model:</p>
<pre><code class="lang-auto">def optimize_grad_control(self, maxIt, gradIt=5):
      ...
      self.optimizer.step(self.closure)
      ...
</code></pre>
<p>Now imagine that i have trained the model and after that i have created a new model of the ReLU-NN class. This new model is then assigned to the <code>training_backend</code> with</p>
<pre><code class="lang-auto">def new_model(self, new_model):
    # assign the new model
    self.model = new_model
</code></pre>
<p>My question is when i want to train the new model with the <code>optimize_grad_control</code> method do i have to reassign a new optimizer and define a new closure function? (I have read somewhere that there is no possibility to dynamically change the <code>model.parameters()</code> within the optimizer.)</p>

username: ptrblck
cooked: <p>Yes, you would need to create a new optimizer for the new model, as the old optimizer stores references to the initially passed parameters.<br>
Alternatively, you could add the new parameters via <code>add_param_group</code>, but I don’t think you would have any benefit from it.</p>

username: LeErnst
cooked: <p>Okay, so it is a PyTorch internal thing? Because i think that i do not need to redefine the closure function. The closure uses self.model which is a reference to model and whenever i change the object under the name self.model the closure function evaluates the new assigned model.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="LeErnst" data-post="3" data-topic="83387">
<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/leernst/48/23609_2.png" class="avatar"> LeErnst:</div>
<blockquote>
<p>Okay, so it is a PyTorch internal thing?</p>
</blockquote>
</aside>
<p>What do you mean by that?</p>
<aside class="quote no-group" data-username="LeErnst" data-post="3" data-topic="83387">
<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/leernst/48/23609_2.png" class="avatar"> LeErnst:</div>
<blockquote>
<p>Because i think that i do not need to redefine the closure function. The closure uses self.model which is a reference to model and whenever i change the object under the name self.model the closure function evaluates the new assigned model.</p>
</blockquote>
</aside>
<p>I don’t know, how you’ve defined the <code>closure</code> method, but if it’s just using some objects, there would be no need to redefine it.</p>

username: LeErnst
cooked: <blockquote>
<p>Okay, so it is a PyTorch internal thing?</p>
</blockquote>
<p>Forget about it. I thought it matters that i pass the parameters as <code>self.model.parameters()</code> but they create a new name for it within the optimizer <code>__init__</code> and when i change <code>self.model</code> with the <code>new_model()</code> method the optimizer object does not notice this change.</p>