albanD:

In that case, you can use the nightly version of pytorch an use the new inputs argument to the .backward() function:


So for doing this you mean, I should create a seperate Contrastive loss function instead of doing it in layer backward. And then use nightly version code that you mentioned. Right?
And can you please explain what this code will do. additional_loss.backward(inputs=net.your_contrastive_layer.parameters()) 
I try to install nightly version with from here.
But I encounterd an error this error:
EnvironmentNotWritableError: The current user does not have write permissions to the target environment.
  environment location: C:\ProgramData\Anaconda3

Looks like I don’t have permission from the admin of the PC I’m using.