Please help me with this error

username: kmc
cooked: <p>Hello, everyone<br>
I try to run LSTM for multivariate time series data</p>
<p>But the source code below causes error<br>
which says<br>
RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.</p>
<p>Source code is as below<br>
Thank you in advance</p>
<pre><code class="lang-python">class LSTMModel(torch.nn.Module):
    def __init__(self,n_features,seq_length):
        super(LSTMModel, self).__init__()
        self.n_features = n_features
        self.seq_len = seq_length
        self.n_hidden = n_features # number of hidden states
        self.n_layers = 1 # number of LSTM layers (stacked)
    
        self.l_gru = torch.nn.LSTM(input_size = n_features, 
                                 hidden_size = self.n_hidden,
                                 num_layers = self.n_layers, 
                                 batch_first = True)
        self.l_linear = torch.nn.Linear(self.n_hidden*self.seq_len, 1)
        self.hidden = self.init_hidden(batch_size)
        

    def init_hidden(self, batch_size):
        # even with batch_first = True this remains same as docs
        hidden_state = torch.zeros(self.n_layers,batch_size,self.n_hidden)
        cell_state = torch.zeros(self.n_layers,batch_size,self.n_hidden)
        return (hidden_state, cell_state)
    
    
    def forward(self, x):        
        batch_size, seq_len, _ = x.size()
        lstm_out, self.hidden = self.l_gru(x,self.hidden)
        x = lstm_out.contiguous().view(batch_size,-1)
        return self.l_linear(x)


mv_net.train()
for t in range(train_episodes):
    for b in range(0,len(X),batch_size):
        inpt = X[b:b+batch_size,:,:]
        target = y[b:b+batch_size]    
        
        x_batch = torch.tensor(inpt,dtype=torch.float32)    
        y_batch = torch.tensor(target,dtype=torch.float32)
    
        #mv_net.init_hidden(x_batch.size(0))
        output = mv_net(x_batch) 
        loss = criterion(output.view(-1), y_batch)
        
        loss.backward()
        optimizer.step()        
        optimizer.zero_grad() 
    print('step : ' , t , 'loss : ' , loss.item())
</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p>I think you don’t handle the <code>hidden</code> state properly.<br>
In particular, I think you should be resetting <code>self.hidden</code> when you get a new sequence.</p>

username: kmc
cooked: <p>Hello, albanD<br>
Thank you for your nice answer.<br>
Now I figured out what the problem is.</p>
<p>But may I ask you one more question?<br>
Why should I reset self.hidden everytime?<br>
I don’t understand.</p>
<p>Thank you and have a nice day, albanD</p>

username: albanD
cooked: <p>Hi,</p>
<p>You might want to look at other code that runs lstm as I don’t know <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"><br>
Maybe <a href="https://pytorch.org/tutorials/beginner/nlp/sequence_models_tutorial.html">this</a> tutorial will help.</p>

username: kmc
cooked: <p>Hello, albanD<br>
Thank you for your help<br>
LSTM is not easy for me<br>
I will study more as you suggest<br>
Have a nice day, albanD</p>