Runtime Error : one of the variables needed for gradient computation has been modified by an in place operation

class Net(torch.nn.Module):

    def __init__(self, filename, num_epochs, learning_rate = 1e-2):
        super().__init__()

        self.filename = filename

        # reproducibility
        torch.manual_seed(0)
        
        self.input_neurons = 78
        self.hidden_neurons = 54
        self.output_neurons = 2

        # learning rate
        self.lr = learning_rate

        # layers
        self.hidden = torch.nn.Linear(self.input_neurons,self.hidden_neurons)
        self.output = torch.nn.Linear(self.hidden_neurons, self.output_neurons)

        # define sigmoid and softmax output
        self.sigmoid = torch.nn.Sigmoid()
        self.softmax = torch.nn.Softmax(dim = 1)

        self.loss = torch.nn.MSELoss(reduction = 'mean')

def forward(self, x):
        hidden_inputs = self.hidden(x)
        hidden_outputs = self.sigmoid(hidden_inputs)
        final_inputs = self.output(hidden_outputs)
        final_outputs = self.softmax(final_inputs)
        return final_outputs


  def run_model(self):
        self.preprocessing()
        # create your optimizer
        optimizer = torch.optim.SGD(self.parameters(), lr = self.lr)
        accuracy = 0
        for epoch in range(2):

            torch.autograd.set_detect_anomaly(True)
            optimizer.zero_grad()
            label_predict = self.forward(self.features)
            # two classes: Benign and DDos to 0 and 1 respectively
            label_target = self.label_encode(self.labels_unique, self.labels)
            label_predict_benign = label_predict[:,0]
            for i,x in enumerate(label_predict_benign): 
                if label_predict_benign[i] > 0.5:
                    label_predict_benign[i] = 0 # BENIGN
                else:
                    label_predict_benign[i] = 1 # DDOS    
            # print(label_predict_benign)
           
            print(label_predict_benign.shape, label_target.shape) # torch.size([227541]), torch.size([227541])
            
            loss = self.loss(label_predict_benign.float(), label_target.float())

            # Backward pass: compute gradient of the loss with respect to model
            # parameters
            loss.backward()

            # Calling the step function on an Optimizer makes an update to its
            # parameters
            optimizer.step()

[torch.FloatTensor [225741, 2]], which is output 0 of SoftmaxBackward, is at version 225741; expected version 0 instead.

Everything works fine until the loss.backward() line.
I don’t understand the error. Can someone please help, I tried to find the solution as much as I could but stuck at this point.

Check you code for inplace operations such as x += y or all operations with a trailing underscore: e.g. x.copy_() and replace them with the out of place equivalent.
If you cannot find these operations, could you post the model definition?

Hi, I have edited the orignal post and added more of my code. Can you please help me debug where the error is?