Saved intermediate values of the graph are freed when you call .backward() or autograd.grad()

Salman_Abbasi: <p>Hi,<br>
I am trying to run for loops inside each training iteration(<em>part of my code, is attached below</em>). What I would like to achieve  is that for each <em>iith</em> and <em>ith</em> loop i have fresh tensors with no grads, as at end of the loop I save the output to vector, and before the next iteration(epoch), these are backpropagated,.</p>
<p>First of all I do not know how to make the tensor and all intermediary tensors in each  of the <em>for</em> loops to be gradient free, after each <em>iith</em> loop ends. So I know there is a problem there. Second if I run this code, it runs through the first epoch and I presume it goes on through the second, but did not backpropagate  the second time. I get the error “Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad().”</p>
<p>Please note that I have not added the model here, as the model function works ok, if I do not have these two for loops inside the epoch(iterations). For this loop I also used retain_graph=True, it give me in_place variable error. But I do not want to use this option, and is more interested in what is happening. I appreciate assistance.</p>
<pre><code>for epoch in range(epochs):
  for ii in range(seis_forward.shape[2]):# for all shots
      for i in range(tmmax_all_times.shape[2]):
          time_step=tmmax_all_times[:,:,i]
          time_step=torch.flatten(time_step)
          time_step=nxx.reshape(len(time_step),1)
      
          net_Input_tensor = torch.cat((vp_new,nxx,nzz,time_step),dim=1)
      
          x=net_Input_tensor
          u,u_xx,u_zz,u_tt,vel_pred=solve_pde_pinn_fwi(x, dnn_Linear.train(),vel_in)
      

          #loss PDE
          u=u.reshape(self.v0.T.shape)
          u_xx=u_xx.reshape(self.v0.T.shape)
          u_zz=u_zz.reshape(self.v0.T.shape)
          u_tt=u_tt.reshape(self.v0.T.shape)
  
  
  
          u_train[:,:,i,ii]=u
          u_xx_train[:,:,i,ii]=u_xx
          u_zz_train[:,:,i,ii]=u_zz
          u_tt_train[:,:,i,ii]=u_tt
          
          seis_pred[:,:,ii]=u[receivers_depth,first_rcvr_point-1:last_rcvr_point]

          vpp=vel_pred.reshape(self.v0.shape).T
          wave_analytic_from_nn=u_tt-((vpp)**2)*(u_xx+u_zz) 

          wave_analytic_from_nn_all[:,:,i,ii]= wave_analytic_from_nn
  

  PDE_Loss=loss(wave_analytic_from_nn_all,u_nn_zeros)
  seismic_loss=loss(seis_pred,seis_forward)
  
 Total_loss=seismic_loss + PDE_Loss
      
 
  optimizer.zero_grad()     
  Total_loss.backward()
</code></pre>

Salman_Abbasi: <p>In case someone is interested. The following helped me:</p><aside class="quote quote-modified" data-post="1" data-topic="16915">
  <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/m/977dab/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/detach-no-grad-and-requires-grad/16915">Detach, no_grad and requires_grad</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>
    Hello 
It’s general question but currently I’m looking at tutorial: 
<a href="http://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html" class="onebox" target="_blank" rel="noopener nofollow ugc">http://pytorch.org/tutorials/intermediate/reinforcement_q_learning.html</a> 
why in optimize_model function - we have 
detach() method 
and in select_action we use 
with torch.no_grad(): 
on the other hand doc: 
<a href="http://pytorch.org/docs/stable/notes/autograd.html" class="onebox" target="_blank" rel="noopener nofollow ugc">http://pytorch.org/docs/stable/notes/autograd.html</a> 
mentions only requires_grad 
of course I understand we don’t want to compute gradients here - but i don’t fully understand the difference between all those 3 methods… 
als…
  </blockquote>
</aside>

<p>I had to detach and preferably empty all tensors used inside the for loops. I am not adding the code here. But for the for loops inside epoch loop, I have detached and zeroed tensors, before they are being computed and saved</p>