Trying to backward through the graph

Hi,
I tried to train an MLP model using the following code, but for the third data batch in the dataloader, I received this error msg:

“Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad().”

def train_nn(self, dataloader, epoch_number):
model.train()
for epoch in range(epoch_number):
epoch_loss = 0
for input, target in dataloader:
Q_pred = model(input)
loss = criterion(Q_pred, target)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
epoch_loss += loss
mean_epoch_loss = epoch_loss / len(dataloader)
print(f"[Epoch: {epoch+1:>02}] → loss = {mean_epoch_loss:>7.4f}")

where the dataloader is defined in a separate function as follows:

def create_dataloader(self):
inputs, targets = self.create_dataset()
n = targets.shape[0]
inputs = torch.reshape(inputs, (n, state_size + input_number))
targets = torch.reshape(targets, (n, 1))
inputs = inputs.type(torch.float32)
targets = targets.type(torch.float32)
train_dataset = data.TensorDataset(inputs, targets)
train_dataloader = data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
self.dataloader = train_dataloader

I also used “loss.backward(retain_graph=True)” instead, but it resulted in the following error msg:

one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [128, 1]], which is output 0 of AsStridedBackward0, is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

can anyone help me how I can solve this issue.
Thank you

You are probably trying to backprop through your data loader, make sure your tensors do not require grad when you manipulate them in your data loader.