Deep metric learning trains an embedding so that same-class points are close. Implement three pieces of it, all with the Euclidean distance between embedding rows.
contrastive_loss(E1, E2, same, m): the pairwise (Siamese) loss, averaged over the rows of the (n, L) arrays E1 and E2. same[i] is a boolean, true when pair has equal labels:
mine_triplets(E, labels, m, mode): in-batch mining. For each anchor (in increasing index order) that has at least one other point with its label, let be its nearest positive (ties to the smaller index). Then scan the negatives (different label) in increasing index order and keep when:
mode="hard": ;mode="semi-hard": ;mode="all": always.Return a list of tuples of Python ints.
batch_triplet_loss(E, triplets, m): the mean over the given triplets of ; return 0.0 for an empty list.