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

How to use cosine distance? #600

Closed korotaS closed 5 months ago

korotaS commented 5 months ago

Hi! I am using OML 3.1.0 to train a pairwise Reranker model and during metrics computation I noticed that only p-norm (moreover - in KNN it is hardcoded to 2-norm) is used as a distance metric and can't be changed and passed as a parameter somewhere:

https://github.com/OML-Team/open-metric-learning/blob/05842c84c980fa2c34510e3de176cd5c5dae9205/oml/utils/misc_torch.py#L127

Are you planning to add other distance metrics, for example cosine similarity? If you think this is a good feature, I can create a PR with those changes (although there are a couple of questions about the way it can be implemented and tested).

AlekseySh commented 5 months ago

@korotaS Thank you for your interest in OML!

From my practical experience, cosine and p=2 norms cover 80% + of real practical cases, so, I don't want to overcomplicate library and bring more distances except for these two at the moment.

If you want to have cosine norm, you should simply normalize model features, because cosine and p2 distances for the normalised vectors are almost the same (they differ on the constant scale factor equal to 2, which doesn't matter). You may google for the simple proof of this fact.

If you want to contribute, you may do something else. For example, I've left a few comments on another issue created by you about interpolation stuff.

korotaS commented 5 months ago

Thanks! Indeed, there was a solution without changing the code, I've missed it. Closing the issue