Model optimization whit adam not working

Ione_Ianniruberto: <p>Hi everyone,<br>
I need to optimized the parameter of an electric circuit minimizing the square mean errore between the inlet know pressure and the measured inlet pressure. I would like to use the Adam optimizer, however i always get the same errore and i don’t know how to fix this.<br>
Below there is the code thai I wrote and the error that i get:<br>
import torch<br>
import numpy as np</p>
<p>class AortaModel(torch.nn.Module):<br>
def <strong>init</strong>(self, fixed_values, dt):<br>
super(AortaModel, self).<strong>init</strong>()<br>
self.Rp_sup = torch.nn.Parameter(torch.tensor(1.0, dtype=torch.float32))<br>
self.Rd_sup = torch.nn.Parameter(torch.tensor(1.0, dtype=torch.float32))  # R è ottimizzabile<br>
self.Rp_out = torch.nn.Parameter(torch.tensor(1.0, dtype=torch.float32))  # R è ottimizzabile<br>
self.Rd_out = torch.nn.Parameter(torch.tensor(1.0, dtype=torch.float32))  # R è ottimizzabile<br>
self.Csup = torch.nn.Parameter(torch.tensor(1.0, dtype=torch.float32))  # C è ottimizzabile<br>
self.Cout = torch.nn.Parameter(torch.tensor(1.0, dtype=torch.float32))  # C è ottimizzabile</p>
<pre><code>    # Valori fissi
    self.a = fixed_values['a']
    self.RAA = fixed_values['RAA']
    self.RDA = fixed_values['RDA']
    self.b = fixed_values['b']
    self.c = fixed_values['c']
    self.dt = dt

def forward(self, Qin_next, Pin_prev, Qin_prev, Qda_prev):
    Qsup_prev = Qin_prev - Qda_prev
    Parco_prev = Pin_prev - self.a * (Qin_prev ** 2) - (self.b + self.RAA) * Qin_prev - self.c
    Pout_prev = Parco_prev - self.a * (Qda_prev ** 2) - (self.b + self.RDA) * Qda_prev - self.c

    beq = self.b + self.RDA + self.Rp_out + self.Rp_sup
    ceq = - ((self.dt / self.Csup) * (Qsup_prev - ((Parco_prev - self.Rp_sup * Qsup_prev) / self.Rd_sup))) + (
            (self.dt / self.Cout) * (
                Qda_prev - (Pout_prev - self.Rp_out * Qda_prev) / self.Rd_out)) + self.Rp_sup * (
                  Qsup_prev - Qin_next) - self.a * (Qin_prev ** 2) - (self.b + self.RDA + self.Rp_out) * Qda_prev

    with torch.no_grad():  # To ensure no gradient computation on these parts
        sqrt_term = torch.sqrt(beq ** 2 - 4 * self.a * ceq)

    Qda_next = (-beq + torch.sqrt(beq ** 2 - 4 * self.a * ceq)) / (2 * self.a)
    Qsup_next = Qin_next - Qda_next

    Pin_next = self.a * (Qin_next ** 2) + (self.b + self.RAA) * Qin_next + self.c + \
               (self.dt / self.Csup) * (Qsup_prev - ((Parco_prev - (self.Rp_sup * Qsup_prev)) / self.Rd_sup)) + \
               Parco_prev + self.Rd_sup * (Qsup_next - Qsup_prev)

    return Qda_next, Pin_next
</code></pre>
<h1><a name="p-453974-definizione-dei-dati-1" class="anchor" href="#p-453974-definizione-dei-dati-1"></a>Definizione dei dati</h1>
<p>num_points = 322<br>
t = np.linspace(0, 0.8, num_points)<br>
Qin = np.sin(t)  # Flusso di ingresso<br>
Qda = np.zeros(num_points)  # Esempio di Qda<br>
Pin_real = np.sin(t)  # Esempio di valori reali per la pressione</p>
<h1><a name="p-453974-converti-i-dati-in-tensori-pytorch-2" class="anchor" href="#p-453974-converti-i-dati-in-tensori-pytorch-2"></a>Converti i dati in tensori PyTorch</h1>
<p>Qin_tensor = torch.tensor(Qin, dtype=torch.float32)<br>
Qda_tensor = torch.tensor(Qda, dtype=torch.float32)<br>
Pin_real_tensor = torch.tensor(Pin_real, dtype=torch.float32)<br>
Qda_var = torch.zeros_like(Qda_tensor)</p>
<h1><a name="p-453974-parametri-fissi-3" class="anchor" href="#p-453974-parametri-fissi-3"></a>Parametri fissi</h1>
<p>fixed_values = {<br>
‘a’: 1.0,<br>
‘RAA’: 2.0,<br>
‘RDA’: 3.0,<br>
‘b’: 4.0,<br>
‘c’: 5.0<br>
}<br>
dt = t[1] - t[0]</p>
<h1><a name="p-453974-istanzia-il-modello-4" class="anchor" href="#p-453974-istanzia-il-modello-4"></a>Istanzia il modello</h1>
<p>model = AortaModel(fixed_values, dt)</p>
<h1><a name="p-453974-definisci-la-funzione-di-perdita-mse-5" class="anchor" href="#p-453974-definisci-la-funzione-di-perdita-mse-5"></a>Definisci la funzione di perdita (MSE)</h1>
<p><span class="hashtag-raw">#def</span> loss_fn(Pin_pred, Pin_real):<br>
<span class="hashtag-raw">#return</span> torch.mean((Pin_pred - Pin_real) ** 2)</p>
<p>criterion = torch.nn.MSELoss(reduction=‘sum’)</p>
<h1><a name="p-453974-definisci-lottimizzatore-adam-6" class="anchor" href="#p-453974-definisci-lottimizzatore-adam-6"></a>Definisci l’ottimizzatore (Adam)</h1>
<p>optimizer = torch.optim.Adam(model.parameters(), lr=0.001)</p>
<h1><a name="p-453974-ciclo-di-addestramento-7" class="anchor" href="#p-453974-ciclo-di-addestramento-7"></a>Ciclo di addestramento</h1>
<p>num_epochs = 1000</p>
<h1><a name="p-453974-inizializza-la-pressione-delliterazione-precedente-8" class="anchor" href="#p-453974-inizializza-la-pressione-delliterazione-precedente-8"></a>Inizializza la pressione dell’iterazione precedente</h1>
<p>Pin_prev = torch.zeros_like(Pin_real_tensor)<br>
<span class="hashtag-raw">#Pin_var</span> = torch.zeros_like(Pin_real_tensor, requires_grad=True)  # Inizia con valori iniziali di pressione, es. zero<br>
torch.autograd.set_detect_anomaly(True)</p>
<p>loss_Adam = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for epoch in range(num_epochs):<br>
if not epoch ==0:<br>
tensore_detached = Qda_new.detach()<br>
tensore_detached2 = Pin_predicted.detach()<br>
# Inizializza Pin_predicted come tensore vuoto<br>
Pin_predicted = torch.zeros(len(Qin_tensor), dtype=torch.float32)<br>
Qda_new = Qda_tensor.clone()</p>
<pre><code>for i in range(len(Qin_tensor) - 1):
    # Calcola la pressione per l'istante i
    Qda_next, Pin_i = model(Qin_tensor[i + 1], Pin_predicted[i], Qin_tensor[i], Qda_new[i])

    # Aggiorna i valori per l'iterazione successiva senza usare in-place
    Qda_new[i + 1] = Qda_next
    Pin_predicted[i + 1] = Pin_i
print('fine for')

# Calcola la perdita
loss = criterion(Pin_predicted, Pin_real_tensor)
loss_Adam.append(loss.item())

# Zeroing gradients
optimizer.zero_grad()
# Backward pass
# Aggiorna i parametri
optimizer.step()


if epoch % 100 == 0:
    print(f'Epoch {epoch}, Loss: {loss.item()}')
</code></pre>
<p>optimized_Rp_sup = model.Rp_sup.item()<br>
optimized_Rd_sup = model.Rd_sup.item()<br>
optimized_Rp_out = model.Rp_out.item()<br>
optimized_Rd_out = model.Rd_out.item()<br>
optimized_Csup = model.Csup.item()<br>
optimized_Cout = model.Cout.item()</p>
<p>the error is this: C:\Users\Ugo\anaconda3\envs\opt\Lib\site-packages\torch\autograd\graph.py:768: UserWarning: Error detected in MulBackward0. Traceback of forward call that caused the error:<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 2199, in <br>
main()<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 2181, in main<br>
globals = debugger.run(setup[‘file’], None, None, is_module)<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 1493, in run<br>
return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 1500, in _exec<br>
pydev_imports.execfile(file, globals, locals)  # execute the script<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py”, line 18, in execfile<br>
exec(compile(contents+“\n”, file, ‘exec’), glob, loc)<br>
File “C:\Users\Ugo\Desktop\Ione\Progetto NIH\Codici ottimizzazione\Circuit_opt.py”, line 94, in <br>
loss_Adam = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 1160, in do_wait_suspend<br>
self._do_wait_suspend(thread, frame, event, arg, suspend_type, from_this_thread)<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 1174, in _do_wait_suspend<br>
self.process_internal_commands()<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev\pydevd.py”, line 873, in process_internal_commands<br>
int_cmd.do_it(self)<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_comm.py”, line 1850, in do_it<br>
result, exception_occurred = pydevd_console_integration.console_exec(self.thread_id, self.frame_id, self.expression, dbg)<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_console_integration.py”, line 236, in console_exec<br>
Exec(code, updated_globals, updated_globals)<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_exec2.py”, line 3, in Exec<br>
exec(exp, global_vars, local_vars)<br>
File “”, line 15, in <br>
File “C:\Users\Ugo\anaconda3\envs\opt\Lib\site-packages\torch\nn\modules\module.py”, line 1553, in _wrapped_call_impl<br>
return self._call_impl(*args, **kwargs)<br>
File “C:\Users\Ugo\anaconda3\envs\opt\Lib\site-packages\torch\nn\modules\module.py”, line 1562, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
File “C:\Users\Ugo\Desktop\Ione\Progetto NIH\Codici ottimizzazione\Circuit_opt.py”, line 35, in forward<br>
Qsup_prev - Qin_next) - self.a * (Qin_prev ** 2) - (self.b + self.RDA + self.Rp_out) * Qda_prev<br>
(Triggered internally at C:\actions-runner_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\autograd\python_anomaly_mode.cpp:116.)<br>
return Variable.<em>execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
Traceback (most recent call last):<br>
File “C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.2\plugins\python-ce\helpers\pydev_pydevd_bundle\pydevd_exec2.py”, line 3, in Exec<br>
exec(exp, global_vars, local_vars)<br>
File “”, line 30, in <br>
File “C:\Users\Ugo\anaconda3\envs\opt\Lib\site-packages\torch_tensor.py”, line 521, in backward<br>
torch.autograd.backward(<br>
File "C:\Users\Ugo\anaconda3\envs\opt\Lib\site-packages\torch\autograd_<em>init</em></em>.py", line 289, in backward<br>
_engine_run_backward(<br>
File “C:\Users\Ugo\anaconda3\envs\opt\Lib\site-packages\torch\autograd\graph.py”, line 768, in _engine_run_backward<br>
return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor <span class="chcklst-box fa fa-square-o fa-fw"></span>], which is output 0 of AsStridedBackward0, is at version 321; expected version 320 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!<br>
How can I solve this? Thanks</p>
<p>Ione</p>

KFrank: <p>Hi Ione!</p>
<aside class="quote no-group" data-username="Ione_Ianniruberto" data-post="1" data-topic="209297" data-full="true">
<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/ione_ianniruberto/48/72624_2.png" class="avatar"> Ione_Ianniruberto:</div>
<blockquote>
<pre><code>    # Aggiorna i valori per l'iterazione successiva senza usare in-place
    Qda_new[i + 1] = Qda_next
    Pin_predicted[i + 1] = Pin_i
print('fine for')
</code></pre>
</blockquote>
</aside>
<p>As you note in your comment, writing into <code>Pin_predicted</code> with<br>
an index is an inplace modification and could well be the<br>
cause of the error you are seeing.</p>
<aside class="quote no-group">
<blockquote>
<pre><code># Zeroing gradients
optimizer.zero_grad()
# Backward pass
# Aggiorna i parametri
optimizer.step()
</code></pre>
</blockquote>
</aside>
<p>The code you posted doesn’t show any call to <code>.backward()</code>,<br>
so it’s not possible to track down where things are going wrong.</p>
<aside class="quote no-group">
<blockquote>
<p>the error is this:<br>
…<br>
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor <span class="chcklst-box fa fa-square-o fa-fw"></span>], which is output 0 of AsStridedBackward0, is at version 321; expected version 320 instead.</p>
</blockquote>
</aside>
<p>You can find some suggestions for locating and fixing<br>
inplace-modification errors in this post:</p>
<aside class="quote quote-modified" data-post="7" data-topic="171826">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation-torch-floattensor-64-1-which-is-output-0-of-asstridedbackward0-is-at-version-3-expected-version-2-instead-hint-the-backtrace-further-a/171826/7">"RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [64, 1]], which is output 0 of AsStridedBackward0, is at version 3; expected version 2 instead. Hint: the backtrace further a</a> <a class="badge-category__wrapper " href="/c/autograd/7"><span data-category-id="7" style="--category-badge-color: #AB9364; --category-badge-text-color: #FFFFFF;" 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>
    Hi Fahmyadan and Sangyoon! 

Here are some suggestions about how to track down (and maybe fix) 
inplace-modification errors.  Note that an inplace modification in the forward 
pass is not necessarily* an error – it depends on whether and how the tensor 
that was modified is used in the backward pass.  Note that inplace operations 
can be useful for saving memory – if you replace an innocent inplace operation 
with an out-of-place equivalent, your training will use more memory (and, to a 
minor e…
  </blockquote>
</aside>

<p>Good luck!</p>
<p>K. Frank</p>