Solved by KFrank in post #2 
              
              
                Hi Jhuteau! 

Easy peasy: 
import math

import torch

print (torch.__version__)

torch.manual_seed (20212021)


# code with tensor operations

def hardest_triplet_margin_lossB (preds, targets, margin, hardest_fraction):
    # preds:             the similarity scores, of shape [num_anchors, num-docum…