Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the numerical stability of cosine similarity #134

Open
chenzhekl opened this issue Jun 29, 2022 · 3 comments
Open

Improve the numerical stability of cosine similarity #134

chenzhekl opened this issue Jun 29, 2022 · 3 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@chenzhekl
Copy link

Related: pytorch/pytorch#31378
Current implementation:

tmp_dist = Cosine<scalar_t>::dot(x, y, n_x, n_y, dim) /

As introduced in the linked PR for PyTorch, normalizing the vectors first and then doing the inner product could prevent overflow for vectors with large norms. In addition, this could ensure the calculated cosine similarity is within the range [-1, +1].

@rusty1s
Copy link
Owner

rusty1s commented Jun 29, 2022

This is clever. Do you want to contribute this feature? :)

@chenzhekl
Copy link
Author

Sure, I would love to.

@rusty1s
Copy link
Owner

rusty1s commented Jun 29, 2022

Thanks!

@rusty1s rusty1s added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants