Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Wrapper functions for NNlib #615

Merged
merged 3 commits into from
Apr 13, 2020
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
32 changes: 12 additions & 20 deletions src/nnlib.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
using NNlib

# Activation functions
@cufunc softplus(x::Real) = ifelse(x > 0, x + log1p(exp(-x)), log1p(exp(x)))

@cufunc σ(x) = ifelse(x < -80, zero(x), one(x) / (one(x) + exp(-x)))
@cufunc logσ(x::Real) = -softplus(-x)

@cufunc function logσ(x)
max_v = max(zero(x), -x)
z = exp(-max_v) + exp(-x-max_v)
-(max_v + log(z))
@cufunc function gelu(x::Real)
p = oftype(x / 1, π)
λ = oftype(x / 1, √(2 / p))
α = oftype(x / 1, 0.044715)
h = oftype(x / 1, 0.5)
h * x * (one(x) + tanh(λ * (x + α * x^3)))
end

@cufunc elu(x, α = one(x)) =
ifelse(x ≥ 0, x/1, α * (exp(x) - one(x)))
@cufunc lisht(x::Real) = x * tanh(x)

@cufunc swish(x) = x * σ(x)
@cufunc logcosh(x::Real) = x + softplus(-2x) - log(oftype(x, 2))

@cufunc function gelu(x)
λ = oftype(x/1, √(2/π))
α = oftype(x/1, 0.044715)
h = oftype(x/1, 0.5)
h * x * (one(x) + tanh(λ * (x + α * x^3)))
end
@cufunc mish(x::Real) = x * tanh(softplus(x))

@cufunc function selu(x)
λ = oftype(x/1, 1.0507009873554804934193349852946)
α = oftype(x/1, 1.6732632423543772848170429916717)
λ * ifelse(x > 0, x/1, α * (exp(x) - 1))
end
@cufunc tanhshrink(x::Real) = x - tanh(x)

@cufunc softplus(x) = ifelse(x > 0, x + log1p(exp(-x)), log1p(exp(x)))

# Batched matrix multiplication

Expand Down
5 changes: 4 additions & 1 deletion test/dnn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ end
@test testf(CUDNN.cudnnActivationBackward, cu(rand(Float64, 10, 10, 3, 1)), cu(rand(Float64, 10, 10, 3, 1)), cu(rand(Float64, 10, 10, 3, 1)), cu(rand(Float64, 10, 10, 3, 1)))

# activations defined in src/nnlib.jl
ACTIVATION_FUNCTIONS = [σ, logσ, hardσ, hardtanh, relu, leakyrelu, relu6, rrelu,
elu, gelu, celu, swish, lisht, selu, trelu, softplus,
softsign, logcosh, mish, tanhshrink, softshrink];
for dims in ((5,5), (5,))
for f in (σ, logσ, elu, swish, gelu, selu, softplus)
for f in filter(x -> x != rrelu, ACTIVATION_FUNCTIONS)
@test testf(x -> f.(x), rand(Float64, dims))
end
end
Expand Down