-
Notifications
You must be signed in to change notification settings - Fork 22
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
logstatsexp #77
base: master
Are you sure you want to change the base?
logstatsexp #77
Changes from all commits
3a2d0f3
0a68325
4376e9c
5aef521
d28a5ce
3fdd368
1154e2e
4f5c996
689ea92
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 |
---|---|---|
|
@@ -31,4 +31,7 @@ softmax! | |
softmax | ||
cloglog | ||
cexpexp | ||
logmeanexp | ||
logvarexp | ||
logstdexp | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
logmeanexp(A::AbstractArray; dims=:) | ||
|
||
Computes `log.(mean(exp.(A); dims))`, in a numerically stable way. | ||
""" | ||
function logmeanexp(A::AbstractArray; dims=:) | ||
R = logsumexp(A; dims=dims) | ||
N = convert(eltype(R), length(A) ÷ length(R)) | ||
return R .- log(N) | ||
end | ||
|
||
""" | ||
logvarexp(A::AbstractArray; dims=:) | ||
|
||
Computes `log.(var(exp.(A); dims))`, in a numerically stable way. | ||
""" | ||
function logvarexp( | ||
A::AbstractArray; dims=:, corrected::Bool=true, logmean=logmeanexp(A; dims=dims) | ||
) | ||
R = logsumexp(2logsubexp.(A, logmean); dims=dims) | ||
N = convert(eltype(R), length(A) ÷ length(R)) | ||
if corrected | ||
return R .- log(N - 1) | ||
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. Again this causes undesired promotions and allocations. 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. Thanks, I think it's better now. 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. The unnecessary allocations are still present. 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. Then I'm not sure what you are referring to? 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. R .- log(N - 1) creates a new array. But if |
||
else | ||
return R .- log(N) | ||
end | ||
end | ||
|
||
""" | ||
logstdexp(A::AbstractArray; dims=:) | ||
|
||
Computes `log.(std(exp.(A); dims))`, in a numerically stable way. | ||
""" | ||
function logstdexp( | ||
A::AbstractArray; dims=:, corrected::Bool=true, logmean=logmeanexp(A; dims=dims) | ||
) | ||
return logvarexp(A; dims=dims, corrected=corrected, logmean=logmean) / 2 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Test: @test, @testset, @inferred | ||
using Statistics: mean, var, std | ||
using LogExpFunctions: logmeanexp, logvarexp, logstdexp | ||
|
||
@testset "logmeanexp, logvarexp" begin | ||
A = randn(5,3,2) | ||
for dims in (2, (1,2), :) | ||
@test logmeanexp(A; dims=dims) ≈ log.(mean(exp.(A); dims=dims)) | ||
for corrected in (true, false) | ||
@test logvarexp(A; dims=dims, corrected=corrected) ≈ log.(var(exp.(A); dims=dims, corrected=corrected)) | ||
@test logstdexp(A; dims=dims, corrected=corrected) ≈ log.(std(exp.(A); dims=dims, corrected=corrected)) | ||
end | ||
end | ||
@test logvarexp(A; dims=2) ≈ log.(var(exp.(A); dims=2)) | ||
@test logstdexp(A; dims=2) ≈ log.(std(exp.(A); dims=2)) | ||
@test logmeanexp(A) ≈ log.(mean(exp.(A))) | ||
@test logvarexp(A) ≈ log.(var(exp.(A))) | ||
@test logstdexp(A) ≈ log.(std(exp.(A))) | ||
end | ||
|
||
@testset "logmeanexp properties" begin | ||
X = randn(1000, 1000) | ||
@test first(logmeanexp(logmeanexp(X; dims=1); dims=2)) ≈ logmeanexp(X) | ||
@test first(logmeanexp(-logmeanexp(-X; dims=1); dims=2)) ≤ first(-logmeanexp(-logmeanexp(X; dims=1); dims=2)) | ||
x = randn() | ||
@test logmeanexp([x]) ≈ x | ||
X = randn(1000, 1000, 1) | ||
@test logmeanexp(X; dims=3) ≈ X | ||
@test -logmeanexp(-X) ≤ logmeanexp(X) | ||
end |
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 will cause undesired promotions and allocations.
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 better now.
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.
It seems you adressed my comment about promotions but not the allocations.
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.
@devmotion you might argue that unless there's a way to modify
logsumexp
to directly takelog(N)
into account, an in-place operation might create issues with AD systems which do not support it?