Triplet sampling

Could anyone recommend a good way to sample triplets from datasets, where a triplet is defined as examples (x1, x2, x3) where x1, x2 are from the same class and x3 is from a different class?

1 Like

You can construct all possible triplets and them sample randomly in this list

That seems like it might take up an excessive amount of memory. Is there a way to use a custom DataLoader?

This two projects might help.
https://github.com/edgarriba/examples/tree/6f3366a55a71644b1876b4d51c79fbecc3376d13
https://github.com/andreasveit/triplet-network-pytorch

This repo also looks fantastic, it looks like the author has even written up several of the popular triplet selection strategies into pytorch dataloaders https://github.com/adambielski/siamese-triplet