Thanks for the updated post.  I am building a binary classification model for sequences using the following code.
class RNNClassifier(nn.Module):

    def __init__(self):
        super(RnnClassifier, self).__init__()

        ...
        self.hidden = None
        ...
    
    def forward(self, inputs):
        ...
        output, self.hidden = self.rnn(X, self.hidden)
        ...
        
        #self.hidden[0][-1] is passed a FC layer and finally sigmoid act
        
    #assuming one layer and one direction
    def init_hidden(self, batch_size):
        return (torch.zeros(1, batch_size, self.hidden_dim),
            torch.zeros(1, batch_size, self.hidden_dim)) 


model = RNNClassifier()
model.hidden = model.init_hidden(batch_size)

for X,y in train_ldr:
    yhat = model(input)
    loss = criterion(...,...)
    optimizer.zero_grad()
    loss.backward()
    optimized.step()

The error I get is:
“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.”
It seems like this post is trying to address the issue –

  
      stackoverflow.com
  
  
      
    
  

  Pytorch - RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed 4



  python, machine-learning, deep-learning, pytorch



  asked by
  
  
    Viet Phan
  
  on 05:57AM - 16 Jan 18 UTC


  
  
    
    
  
  


The hidden.detach_(); hidden = hidden.detach()  did not work for me and I am not even sure
where to put it.
Did you run in to this issue?  You will need to go through a couple of mini-batches
in the first epoch before you run into this.