Custom loss functions

Hi,
Would i able to use numpy operations at first and at last return tensor operations.
Im making custom triplet loss function , here is my code

class HardTripletLoss(nn.Module):
    def __init__(self, alpha=0.25):
        super(HardTripletLoss,self).__init__()
        self.alpha = alpha
        
    def forward(self, q1_vec, q2_vec):
       #..... some numpy operations with tensors
        l_full = torch.mean(l_1 + l_2)
        return l_full

would i able to do .backward .
I could use torch operations with this functions but some operations like np.max or np.maximum or some other which is difficult to do with torch. operations some good torch functions are in unstable yet.

If possible
would you please give me idea to use numpy operations on custom loss

Thanks :smiley:

Also
I have tried to use l_full=torch.mean(l_1+l_2, requires_grad=True) for gradient and while computing q1_vec and q2_vec at first i used .detach().numpy() ,

which i used a toy example, dont know whether it worked or not but gave gradient value for .backward() method,

v1 = torch.tensor([[0.26726124, 0.53452248, 0.80178373],[0.5178918 , 0.57543534, 0.63297887]], requires_grad=True)
v2 = torch.tensor([[ 0.26726124,  0.53452248,  0.80178373],[-0.5178918 , -0.57543534, -0.63297887]], requires_grad=True)
HardTripletLoss()(v1, v2).backward()

tensor(0.5509, grad_fn=<DivBackward0>)