In side the train_loader loop you are already calling loss.backward(), which will calculate the gradients and will free the intermediate activations, which are needed for a second backward pass using this loss.
Later in the same loop you are appending loss to loss_list and try to call backward again on the sum of all losses, which will raise this issue. Besides the already freed intermediate activations, you are also already updating the parameters via optimizer.step() inside the train_loader loop, which would then raise another error, since the forward activations (kept via retain_graph=True) would be stale.