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

log1pexp #37

Merged
merged 38 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ Return `log(1+exp(x))` evaluated carefully for largish `x`.
This is also called the ["softplus"](https://en.wikipedia.org/wiki/Rectifier_(neural_networks))
transformation, being a smooth approximation to `max(0,x)`. Its inverse is [`logexpm1`](@ref).
"""
log1pexp(x::Real) = x < 18.0 ? log1p(exp(x)) : x < 33.3 ? x + exp(-x) : oftype(exp(-x), x)
log1pexp(x::Float32) = x < 9.0f0 ? log1p(exp(x)) : x < 16.0f0 ? x + exp(-x) : oftype(exp(-x), x)
log1pexp(x::Real) = x ≤ -37 ? exp(x) : x ≤ 18 ? log1p(exp(x)) : x 33.3 ? x + exp(-x) : float(x)
cossio marked this conversation as resolved.
Show resolved Hide resolved
cossio marked this conversation as resolved.
Show resolved Hide resolved
log1pexp(x::Float32) = x < 9f0 ? log1p(exp(x)) : x < 16f0 ? x + exp(-x) : oftype(exp(-x), x)

"""
$(SIGNATURES)
Expand Down
20 changes: 11 additions & 9 deletions test/basicfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ end
# log1pexp, log1mexp, log2mexp & logexpm1

@testset "log1pexp" begin
@test log1pexp(2.0) ≈ log(1.0 + exp(2.0))
@test log1pexp(-2.0) ≈ log(1.0 + exp(-2.0))
@test log1pexp(10000) ≈ 10000.0
@test log1pexp(-10000) ≈ 0.0

@test log1pexp(2f0) ≈ log(1f0 + exp(2f0))
@test log1pexp(-2f0) ≈ log(1f0 + exp(-2f0))
@test log1pexp(10000f0) ≈ 10000f0
@test log1pexp(-10000f0) ≈ 0f0
# test every branch
for x in (0, 1, 2, 10, 20, 40), T in (Int, Float32, Float64)
@test (@inferred log1pexp(-T(x))) ≈ log1p(exp(big(-x)))
@test (@inferred log1pexp(+T(x))) ≈ log1p(exp(big(+x)))
cossio marked this conversation as resolved.
Show resolved Hide resolved
end

# large arguments
@test (@inferred log1pexp(1e4)) ≈ 1e4
@test (@inferred log1pexp(1f4)) ≈ 1f4
@test iszero(@inferred log1pexp(-1e4))
@test iszero(@inferred log1pexp(-1f4))
end

@testset "log1mexp" begin
Expand Down