Skip to content

Commit 7f63d7a

Browse files
authored
Merge pull request #13 from ChevronETC/activations
adding hyperbolic tangent and tests
2 parents 25631f1 + 7d20ab0 commit 7f63d7a

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/JetPack.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ include("jop_roughness.jl")
3939
include("jop_shift.jl")
4040
include("jop_sigmoid.jl")
4141
include("jop_taper.jl")
42+
include("jop_tanh.jl")
4243
include("jop_translation.jl")
4344

4445
end

src/jop_tanh.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
F = JopTanh(spc)
3+
4+
where `F` is the hyperbolic tangent operator with domain and range given by `spc::JetSpace`.
5+
we use 'tanh'(c*x) = (\exp{c*x}-\exp{-c*x})/(\exp{c*x}+\exp{-c*x}),
6+
the derivative of which is 1-tanh(c*x)^2.
7+
We expect the domain and range to be real.
8+
"""
9+
JopTanh(spc::JetSpace{T}, c = 1) where {T} = JopNl(dom = spc, rng = spc, f! = JopTanh_f!, df! = JopTanh_df!, df′! = JopTanh_df′!, s = (c=T(c),))
10+
export JopTanh
11+
12+
JopTanh_f!(d::AbstractArray, m::AbstractArray; c) = d .= tanh.(c .* m)
13+
14+
JopTanh_df!(δd::AbstractArray, δm::AbstractArray; mₒ, c) = δd .= c .* (1 .- tanh.(c .* mₒ).^2) .* δm
15+
16+
JopTanh_df′!(δd::AbstractArray, δm::AbstractArray; mₒ, c) = δd .= conj.(c .* (1 .- tanh.(c .* mₒ).^2)) .* δm
17+

test/jop_tanh.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using LinearAlgebra, Jets, JetPack, Test
2+
3+
n1,n2 = 33,44
4+
5+
@testset "JopTanh, correctness T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32})
6+
F = JopTanh(JetSpace(T,n1,n2))
7+
x1 = rand(domain(F))
8+
@test F*x1 tanh.(x1)
9+
end
10+
11+
@testset "JopTanh, linearity test, T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32})
12+
F = JopTanh(JetSpace(T,n1,n2))
13+
J = jacobian(F, rand(domain(F)) )
14+
lhs,rhs = linearity_test(J)
15+
@test lhs rhs
16+
end
17+
18+
@testset "JopTanh, dot product test, T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32})
19+
F = JopTanh(JetSpace(T,n1,n2))
20+
J = jacobian!(F, rand(domain(F)) )
21+
lhs, rhs = dot_product_test(J, -1 .+ 2 .* rand(domain(J)), -1 .+ 2 .* rand(range(J)))
22+
@test lhs rhs
23+
end
24+
25+
# note the key here is to increase the size of the nonlinear vector
26+
@testset "JopTanh, linearization test, T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32})
27+
F = JopTanh(JetSpace(T,n1,n2))
28+
m0 = -0.5 .* rand(domain(F))
29+
μ = sqrt.([1/1,1/2,1/4,1/8,1/16,1/32,1/64,1/128,1/256,1/512,1/1024,1/2048])
30+
δm = rand(domain(F))
31+
observed, expected = linearization_test(F, m0, μ = μ, δm = δm)
32+
δ = minimum(abs, observed - expected)
33+
@test δ < 0.1
34+
end
35+
36+
@testset "JopTanh, odd funcion T=$(T)" for T in (Float64,Float32,Complex{Float64},Complex{Float32})
37+
F = JopTanh(JetSpace(T,n1,n2))
38+
x1 = 2 .* (-1 .+ 2 .* rand(domain(F)))
39+
@test F * (-1 .* x1) -(F * x1)
40+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ for filename in (
3636
"jop_shift.jl",
3737
"jop_sigmoid.jl",
3838
"jop_taper.jl",
39+
"jop_tanh.jl",
3940
"jop_translation.jl")
4041
include(filename)
4142
end

0 commit comments

Comments
 (0)