LSTM Autoencoder throwing an error during backward() function

aksve: <p>LSTM Autoencoders is throwing an error during the backward() function. Below is the model.</p>
<p>class Encoder(nn.Module):<br>
def <strong>init</strong>(self,input_size,hidden_size):<br>
super(Encoder,self).<strong>init</strong>()</p>
<pre><code>    self.input_size = input_size
    self.hidden_size = hidden_size
    self.lstm = nn.LSTM(input_size = input_size,
                        hidden_size = hidden_size,
                        num_layers = 1,batch_first=True)
    
def forward(self,x):
    output , (h,c) = self.lstm(x)
    hidden_layer = h[-1,:,:]
    return hidden_layer     
</code></pre>
<p>class Decoder(nn.Module):<br>
def <strong>init</strong>(self,seq_len,input_size,output_size):<br>
super(Decoder,self).<strong>init</strong>()</p>
<pre><code>    self.seq_len = seq_len
    self.input_size = input_size
    self.layer1 = (input_size * 2)
    self.output_size = output_size
    self.lstm = nn.LSTM(input_size = input_size,
                       hidden_size = self.layer1,
                       num_layers = 1,
                       batch_first = True)
    self.fc = nn.Linear(self.layer1, output_size)
        
    
def forward(self,x):
    x = x.unsqueeze(1).repeat(1,self.seq_len,1)
    x,(h,c) = self.lstm(x)
    x = x.reshape((-1, self.seq_len, self.layer1))
    out = self.fc(x)
    return out
</code></pre>
<p><span class="hashtag">#AutoEncoders</span> in place<br>
class LSTM_AE(nn.Module):<br>
def <strong>init</strong>(self, seq_len, input_size, hidden_size):<br>
super(LSTM_AE,self).<strong>init</strong>()</p>
<pre><code>    self.seq_len = seq_len
    self.input_size = input_size
    self.hidden_size = hidden_size

    self.encoder = Encoder(self.input_size, self.hidden_size)
    self.decoder = Decoder(self.seq_len, self.hidden_size, self.input_size)
    

def forward(self, x):
    encoded = self.encoder(x)
    decoded = self.decoder(encoded)
    return decoded
</code></pre>
<p><span class="hashtag">#Model</span><br>
model = LSTM_AE(seq_len=max_len,input_size=final_embed[events[0]].size(2),hidden_size=32)<br>
<span class="hashtag">#Loss</span><br>
loss_fn = nn.L1Loss(reduction=‘sum’)<br>
<span class="hashtag">#Optimizer</span><br>
optimizer = torch.optim.Adam(model.parameters(),lr=0.001)</p>
<p>n_epochs = 1 # or whatever<br>
losses = []</p>
<p>for epoch in range(n_epochs):<br>
for batch_idx, (a,b,c,d) in enumerate(loader):</p>
<pre><code>    #a = a.clone().detach().requires_grad_(True)

    # in case you wanted a semi-full example
    decoded = model.forward(a)
    print(decoded.size())
    
    loss = loss_fn(decoded,a)
    
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if batch_idx%10 == 0:
        losses.append(loss)
        print("epoch {}.\tbatch {}.\tloss : {}".format(epoch,batch_idx,loss))
</code></pre>
<p>This is throwing the error as</p>
<p>RuntimeError: 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(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.</p>
<p>I am able to run this by adding the requires_grad on the train dataset  i.e. a in the above forward call but unable to understand the problem here as I generally don’t do that while running the other models on torch</p>

anantguptadbl: <p><a class="mention" href="/u/aksve">@aksve</a> This has been explained in detail in the below post</p>
<aside class="quote quote-modified" data-post="1" data-topic="1426">
  <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/wasiahmad/48/248_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/solved-why-we-need-to-detach-variable-which-contains-hidden-representation/1426">[solved] Why we need to detach Variable which contains hidden representation?</a> 
  </div>
  <blockquote>
    I was going through the pytorch official example - “word_language_model” and found the following line of code in the train() function. 
# Starting each batch, we detach the hidden state from how it was previously produced.
# If we didn't, the model would try backpropagating all the way to start of the dataset.
hidden = repackage_hidden(hidden)

I am not understanding why we need to detach hidden variable from the hidden variable associated with the previous batch of input? When gradients are com…
  </blockquote>
</aside>


aksve: <p>This talks about detaching the previous hidden state to save from the problem of exploding/Vanishing gradients. But here, the problem is to run the auto-encoder, I am providing the input to the model where requires_grad = True to run it successfully. However, it should not be the case as we don’t update the input given to the model. So, inherently something is going wrong as ideally it should not update the input</p>

aksve: <p>Please close this as the right answer has been identified</p>