Thank you for the answer.
I’ve included



 ptrblck:

hidden = tuple(h.detach() for h in hidden)



this line just before the use of the lstm layer as so the code becomes the following:

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
       hidden = tuple(h.detach() for h in hidden) #HERE
       output, hidden = self.lstm(output, hidden)

       output = self.classifier(output[0])

       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))



However I’m getting the same sort of error as before:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [500, 309]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!