So for doing this you mean, I should create a seperate Contrastive loss function instead of doing it in layer backward.

I think it is going to be simpler than modifying the backward and writing the gradients yourself yes.

And can you please explain what this code will do.

It will run the backward as usual but will only update the .grad fields of the inputs that were given. So in your case, you only want to update the parameters of that one layer.

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

You might want to create a new environment in your conda so that you can install things.