OML-Team / open-metric-learning

Metric learning and retrieval pipelines, models and zoo.
https://open-metric-learning.readthedocs.io/en/latest/index.html
Apache License 2.0
895 stars 61 forks source link

Question: Negative sampling without labels? #591

Closed RyanZotti closed 5 months ago

RyanZotti commented 5 months ago

I'm training a two-tower recommender model where I have one embedding tower for users and another embedding tower for items. I'd like to use cosine similarity and triplet/contrastive loss to train the model so that when I need to serve recommendations for a user, I can calculate their embedding and then do approximate nearest neighbor search of the user embedding against a vector DB of items only.

Anyways, to train such a model I need to sample negatives. I was hoping to do in-batch negative sampling since I only have positive pairs (items that users clicked). It seems like both this library and Pytorch Metric Learning only support sampling when there is some sort of label (e.g., all the images that belong to the "tiger" class). In my recommender dataset, I don't think it makes sense to assign a user-item pair a label. Is what I'm trying to accomplish with OML possible?

AlekseySh commented 5 months ago

Hey, @RyanZotti and thank you for your interest in OML!

You are right that in-batch mining approach is not what is suitable for your case.

I can recommend you to implement your own dataset that precomputes triplets based on your positive and negative pairs in advance. So, your __getitem__ returns a triplet and you don't need miner and sampler. Then use built-in triplet loss without miner (this one or that one).

I had a similar task setup on practice and used the approach above. In my case we used crowd service to label positive and negative pairs of clothes items, so, we also did not work with "class" labels and used the dataset of pre-computed triplets. Moreover, we joined those pre-computed triplets and obtained by mining from another dataset having labels.

RyanZotti commented 5 months ago

Thanks for the details and quick reply! I'll mark this as closed / resolved.

AlekseySh commented 5 months ago

@RyanZotti you are welcome!

If you would use the project in your company, please, consider putting the logo to the "trusted by" section. Ping me in this case :)