AssertionError: nn criterions don't compute the gradient w.r.t. targets - please mark these variables as volatile or not requiring gradients

I tried to extract the feature by adding the VGG16 network to the predicted image and ground truth, and use L1 loss to establish the extracted relationship,
ie, L1_LOSS(VGG16(predicted)-VGG16(ground truth)),


And the following error occurred

Traceback (most recent call last):
** File “/home/hcj/Desktop/cyc legan collection/0716/PyTorch-CycleGAN-master/train.py”, line 127, in **
** loss_identity_B_vgg = Criterion_perceptual(val_same_B, val_real_B)* 2.0**
** File “/home/hcj/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 357, in call**
** result = self.forward(input, kwargs)
** File “/home/hcj/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/loss.py”, line 82, in forward
*
** _assert_no_grad(target)**
** File “/home/hcj/.conda/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/loss.py”, line 11, in _assert_no_grad**
** "nn criterions don’t compute the gradient w.r.t. targets - please " **
AssertionError: nn criterions don’t compute the gradient w.r.t. targets - please mark these variables as volatile or not requiring gradients

I have an error in the last line of code in the diagram:
"nn criterias don’t compute the gradient w.r.t. targets - please "
AssertionError: nn criterions don’t compute the gradient w.r.t. targets - please mark these variables as volatile or not requiring gradients
I am a newbie. . I really don’t know what to do… Please help me.

Try to .detach() your target and calculate the loss again.

I still do it wrong according to your way…
And I want to know why the penultimate line does not appear the same error.
Their input types are Variable and same L1 loss

You have to assign the detached tensor:

target = target.detach()

or alternatively just detach it in the criterion:

loss = criterion(output, target.detach())
2 Likes

Thank you very much for your help. I finally succeeded in following your approach, But can you tell me the reason? Count the last two lines of code in the first image. They have almost the same structure(Same L1 loss and same type of data), but why does the second line report an error and need to be modified?

The loss calculation regarding real_b is done on a target sampled from your DataLoader, while val_real_B is still attached to the computation graph of your vgg_MODEL.
I assume you want to train the model to predict val_same_B to be as close as possible to val_real_B, so we only need the gradients based on the computation graph from val_same_B.

well ! I’m truly grateful for your help, I really appreciate it.

I also had a similar problem which I resolved it using the answer given here. However, I am still confused how the target got attached to the graph without setting target.requires_grad= True?