Hi, everyone. It’s my first time to use Pytorch for my project. When I start to train my network, an error occurred:

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.

I wander that my customized loss module caused this error.
My network has 4 stacks and each stack tries to predict the same target of different scales, just like:

pred: list of tensors [(nstack, bacth, C, H1, W1), (nstack, bacth, C, H2, W2), (nstack, bacth, C, H2, W2)]
target: tensor (stack, batch, C, H, W)

I rescale the target tensor and calculate each scale and each stack losses.
Can I just calculate the loss of predicted tensors with the same target tensors, or need I repeat the target tensor for nstack times and then calculate the loss?