So your code has form like this?
feature_target = model(input1)
feature_identity = model(input2)
for i, data in enumerate(loader):
    feature_adversarial = model(data)
    distance_target = torch.sum((feature_adversarial - feature_target.clone()) **2)
    distance_identity = torch.sum((feature_adversarial - feature_identity.clone()) ** 2)
    loss = distance_target + 2*distance_identity
    loss.backward()

In that case you should call loss.backward(retain_graph=True) instead of loss.backward() to retain saved intermediate tensors those are necessary to compute gradients. Otherwise you would lose the information for gradient computation.
If it’s not the case, please tell me what do you mean to say but they are fixed thereafter. in detail. Does it mean you want feature_target and feature_identity don’t affect on model update?