-
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
Proposal: generalise logsumexp
slightly.
#69
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -190,7 +190,13 @@ function logsumexp(X) | |
isempty(X) && return log(sum(X)) | ||
reduce(logaddexp, X) | ||
end | ||
function logsumexp(X::AbstractArray{T}) where {T<:Real} | ||
function logsumexp(X::AbstractArray{T}; dims=nothing) where {T<:Real} | ||
dims isa Nothing && return _logsumexp(X) | ||
isempty(X) && return log(zero(T)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this type unstable? I think you'd still have to consider dimensions as in julia> sum(zeros(0, 0), dims=1)
1×0 Array{Float64,2} |
||
u = maximum(X; dims=dims) | ||
return log.(sum(exp.(X .- u); dims=dims)) .+ u | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe reuse the array created by |
||
end | ||
function _logsumexp(X::AbstractArray{T}) where {T<:Real} | ||
isempty(X) && return log(zero(T)) | ||
u = maximum(X) | ||
isfinite(u) || return float(u) | ||
|
@@ -199,7 +205,6 @@ function logsumexp(X::AbstractArray{T}) where {T<:Real} | |
end | ||
end | ||
|
||
|
||
""" | ||
softmax!(r::AbstractArray, x::AbstractArray) | ||
|
||
|
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 it's more contentional to make this something like
though it doesn't really matter; they're entirely equivalent and should perform the same. Just figured I'd note it.
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.
isnothing(dims)
?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.
===
is special cased in the compiler and is generally more efficient for checkingnothing
(and indeed, evenmissing
).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
isnothing
is also elided since it's dealt with at dispatch time, and it was the recommended way to check for nothingness? The only thing is that it requires Julia 1.1.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.
How did you come to that conclusion?
It is not, see JuliaLang/julia#27681.
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.
Well, it's a exported function named![:trollface: :trollface:](https://github.githubassets.com/images/icons/emoji/trollface.png)
isnothing
In reality I wasn't aware of that issue. Thanks for pointing it out.