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

all zero vectors will generate NaN #41

Open
chainsawriot opened this issue Apr 17, 2023 · 1 comment
Open

all zero vectors will generate NaN #41

chainsawriot opened this issue Apr 17, 2023 · 1 comment

Comments

@chainsawriot
Copy link
Collaborator

chainsawriot commented Apr 17, 2023

In general, cosine is not a good distance measurement for all-zero vectors. But we can't change that.

https://github.com/chainsawriot/sweater/blob/6aebf710d813033c6d07f0268f12bd3e6badaee5/src/weat.cpp#L14

This will generate "divide by zero" problem because deno_* is zero, the sqrt of deno_* is also zero, and the denominator is zero.

A simple solution is to imitate pytorch to use eps (pytorch uses 1e-8). The denominator should always be positive (due to the squaring and then rooting).

@chainsawriot
Copy link
Collaborator Author

A quick demo:

sweater:::raw_cosine(c(1,2,3), c(0,0,0))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant