Skip to content

Weakly supervised metric learning? #482

Answered by KevinMusgrave
nikosmichas asked this question in Q&A
Discussion options

You must be logged in to vote

If you mean training on triplets alone instead of labels, then yes. Any tuple-based loss function takes in an argument indices_tuple which describes the pairs or triplets you want to train on.

For triplets, the format of indices_tuple is (a, p, n), where a, p, and n are integer tensors:

import torch
from pytorch_metric_learning.losses import TripletMarginLoss

loss_fn = TripletMarginLoss()
embeddings = torch.randn(32, 128)

# Two triplets. The first triplet has
# anchor: embeddings[0]
# positive: embeddings[1]
# negative: embeddings[5]
triplet_indices = [(0, 1, 5), (2, 3, 18)]

# "a" is all anchors, "p" is all positives, and "n" is all negatives
a, p, n = [torch.tensor(x) for x in list(zip(*

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@nikosmichas
Comment options

@KevinMusgrave
Comment options

@KevinMusgrave
Comment options

Answer selected by nikosmichas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants