You’re right, I had added it as a suggestion of the error I had before:

RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling .backward() or autograd.grad() the first time.

I could fix this inplace operation error that I described before by detaching the input of the last linear layer in the decoder, however I’m not sure about the meaning of dropping this tensor from the gradient computation graph…could you explain me if it makes sense not to propagate the gradient in this tensor?
class BahdanauDecoder(nn.Module): 
   def __init__(self, hidden_size, output_size, n_layers=1, drop_prob=0.1):
       super(BahdanauDecoder, self).__init__()
       self.hidden_size = hidden_size
       self.output_size = output_size
       self.n_layers = n_layers
       self.drop_prob = drop_prob
       self.input_size = input_size #number of machines 

       self.fc_hidden = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
       self.fc_encoder = nn.Linear(self.hidden_size, self.hidden_size, bias=False)
       self.weight = nn.Parameter(torch.FloatTensor(1, hidden_size))
       self.attn_combine = nn.Linear(self.hidden_size * 2, self.hidden_size)
       self.dropout = nn.Dropout(self.drop_prob)
       self.lstm = nn.LSTM(self.hidden_size + self.input_size, self.hidden_size, batch_first=True)
       self.classifier = nn.Linear(self.hidden_size, self.output_size)

   def forward(self, inputs, hidden, encoder_outputs):
       #inputs are actually the previous decoder outputs
       encoder_outputs = encoder_outputs.squeeze()
       
       embedded = inputs[0].clone() #shape (#num_machines, 1)
       
       embedded = self.dropout(embedded)

       # Calculating Alignment Scores
       x = torch.tanh(self.fc_hidden(hidden[0])+self.fc_encoder(encoder_outputs))
       alignment_scores = x.bmm(self.weight.unsqueeze(2))  

       # Softmaxing alignment scores to get Attention weights
       attn_weights = F.softmax(alignment_scores.view(1,-1), dim=1)

       # Multiplying the Attention weights with encoder outputs to get the context vector
       context_vector = torch.bmm(attn_weights.unsqueeze(0),
                                encoder_outputs.unsqueeze(0))

       # Concatenating context vector with embedded input word
       output = torch.cat((embedded.view(1,-1), context_vector[0]), 1).unsqueeze(0)
       
       # Passing the concatenated vector as input to the LSTM cell
       output, hidden = self.lstm(output, hidden)

       output = self.classifier(output[0].clone().detach()) #CHANGED HERE

       return output, hidden, attn_weights
   
   def init_in(self):
       return torch.zeros(1 , input_size, 1)
   
   def init_hidden(self):
       return (torch.zeros(1 , 1, hidden_size),  
                        torch.zeros(1 , 1, hidden_size))


The above works, even when taking out the option retain_graph=True from the backward propagation in the train, however I’m not sure why.