How can I use my custom distance in TripletMarginLoss?

I’d like to use TripletMarginLoss, but i like to modify the distance d with some custom fuction.

Can anyone please help and show me how I can rewrite the TripletMarginLoss in a way that i can modify the parts.
Looking at the source code it gets me to def triplet_margin_loss which is not that much useful either :frowning:

You can write a normal python function. You have to return the loss as a scalar, and all the operations have to be through pytorch tensors.

So, its quite chill :smiley:

Thanks for your answer.
I dont think you can write a normal python function and use it in pytorch. Unless they change it.
It has to be in pytorch to be able to do backpropagation.
Can you give me a simple example?

Sure. As I said, you can write a normal python function as long as you don’t break the pytorch magic i.e. do the calculations on tensors to not break the gradients.

import torch

def myloss(t1, t2):
    loss_per_batch = (t1 - t2).abs().mean(dim=1)
    return loss_per_batch.mean()


tensor1 = torch.randn(2, 3)
tensor2 = torch.randn(2, 3)

loss = myloss(tensor1, tensor2)
print(loss)

@Oli is it something new in pytorch?, i.e. I thought using normal python function will always break the paytorch gradient

No, not new. Don’t know if it was always like that