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

Fix tanh_fast for large Float64 #408

Merged
merged 2 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "NNlib"
uuid = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
version = "0.8.4"
version = "0.8.5"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
2 changes: 1 addition & 1 deletion src/activations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ end
# Instead, we switch to a polynomial, which is very accurate within its range:
x2 = x * x
ypoly = x * evalpoly(x2, (1.0, -0.33333333333324583, 0.13333333325511604, -0.05396823125794372, 0.02186660872609521, -0.008697141630499953))
ifelse(x2 > 900.0, sign(y), ifelse(x2 < 0.017, oftype(y, ypoly), y))
ifelse(x2 > 900.0, sign(x), ifelse(x2 < 0.017, ypoly, y))
end

# These approximations are very badly behaved for Float16; none are fast.
Expand Down
4 changes: 2 additions & 2 deletions test/activations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ end
@testset "tanh_fast & sigmoid_fast: Float64" begin

x64 = 1e-6:1e-4:5
xbig = 6:3:200.0
xbig = vcat(6:3:200.0, 1000, 10^6, typemax(Float64))

@testset "tanh" begin
mean_eps(tanh, tanh, x64) # 0.06582
Expand Down Expand Up @@ -263,7 +263,7 @@ end
@testset "tanh_fast & sigmoid_fast: Float32" begin

x32 = 1f-6:1f-4:5
xbig32 = 6:3:200f0
xbig32 = vcat(6:3:200f0, 1000, typemax(Float32))

@testset "tanh" begin
mean_eps(tanh, tanh, x32) # 0.065
Expand Down