I’m not sure, how your code works exactly, but the usage of loss is wrong.
Variables are deprecated since PyTorch 0.4.0, so you can now use tensors.
However, you don’t need to initialize the loss tensor with requires_grad=True, since the loss is usually an output of your criterion and will require gradients, if you don’t detach the computation graph at some point.
The usage of .data is dangerous and you are manipulating the underlying data at the moment without Autograd tracking any of these operations.
It seems you are calculating the inside_loss in a loop, which should explain the “backward a second time” error.
E.g. if you feed some outputs from the initial loop as inputs to the next iteration, these tensors will still be connected to the computation graph. The next backward call will thus try to compute the gradients for the complete computation graph, which would result in both iterations.
Depending on your use case, you could .detach() the tensors to stop the backward pass at this point.