-
Notifications
You must be signed in to change notification settings - Fork 40
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
Fixes initial value for reduce in logsumexp #82
Conversation
src/basicfuns.jl
Outdated
@@ -191,7 +191,7 @@ function logsumexp(X) | |||
reduce(logaddexp, X) | |||
end | |||
function logsumexp(X::AbstractArray{T}; dims=:) where {T<:Real} | |||
u = reduce(max, X, dims=dims, init=log(zero(T))) | |||
u = reduce(max, X, dims=dims, init=zero(T)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not right, X
can be all negative.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching the error. Do you know what the correct initial value should be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not have init
at all. Just check if X
is empty and return log(zero(T))
then. Otherwise, keep going with the reduce
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I made the changes you suggested and the results hold for both negative and positive values. Please review if you can.
Would be nice to add a |
This isn't correct when |
The problem is that |
|
I guess we could use another way to get |
Yup |
I am curious though, why is the solution here not correct when |
Cool. Actually this won't work for integer inputs, so better use something like
julia> reduce(max, zeros(0, 2), dims=1, init=log(zero(Int)))
1×2 Array{Float64,2}:
-Inf -Inf
julia> reduce(max, zeros(0, 2), dims=1)
ERROR: ArgumentError: reducing over an empty collection is not allowed
|
@nalimilan |
I don't have write access to this PR, so @itsdfish please use: u = reduce(max, X, dims=dims, init=oftype(log(zero(T)), -Inf)) instead of u = reduce(max, X, dims=dims, init=log(zero(T))) |
The updates seem to work on my end. |
Thanks. One last check: is it really expected/correct that Anyway, if we make this change, we should add a comment to ensure somebody doesn't revert this without knowing the reason why it exists. EDIT: I've found http://www.juliadiff.org/ForwardDiff.jl/stable/user/advanced/#Fixing-NaN/Inf-Issues-1, so this seems to be expected. |
To make things easier, I added you both as collaborators in case you want to make changes. |
Perhaps merge this? |
This PR request fixes the initial value for
reduce
inlogsumexp
. This error was discovered in the following issue in Turing (see final comment).