Fixing NaN sampling for von Mises Fisher distribution #1918
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.




Summary of contribution
I have made a change to the sampling algorithm of von Mises Fisher distributions to avoid
NaNoutputs.The problem
Sampling from certain von Mises Fisher distributions will result in
NaN. For example:...returns:
The error happens because of operations performed in the function
_vmf_householder_vec:Distributions.jl/src/samplers/vonmisesfisher.jl
Lines 87 to 102 in 3de6038
The problem arises when
μis a vector exactly in the direction of the first dimension, or very close to the first dimension. (For example,μ = [1.0, 0.0],μ = [1.0, 0.0, 0.0], orμ = [1.0, 0.0, 1e-8], etc...) In these cases, variableswill become zero, leading vectorvto become filled with NaNs.This issue was previously raised in #1423
Proposed solution
I propose a solution, where I have added a small epsilon value to the denominators in
_vmf_householder_vec. This approach is common in machine learning algorithms like ADAM, where dividing by zero is avoided during accumulation:...returns:
... which are not
NaNs!Discussion
I have the following discussion topics:
NaNoutputs.As always, feedback is appreciated