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

add elu and selu activations #263

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 src/Knet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ include("uva.jl")
include("kptr.jl"); export knetgc # KnetPtr
include("karray.jl"); export KnetArray
include("unfuse.jl"); # julia6 broadcast fixes
include("unary.jl"); export relu, sigm, invx
include("unary.jl"); export relu, sigm, invx, elu, selu
include("broadcast.jl"); # elementwise broadcasting operations
include("reduction.jl"); # sum, max, mean, etc.
include("linalg.jl"); export mat # matmul, axpy!, transpose, (i)permutedims
Expand Down
1 change: 1 addition & 0 deletions src/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ broadcast_ops = [
# "fdim",
("invxback","invxback","(-xi*yi*yi)"),
("reluback","reluback","(yi>0?xi:0)"),
("eluback", "eluback", "ifelse(yi>0,dyi,yi+1)"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part turns to a cuda code snippet. So, I think it must be replaced with the following:

("eluback", "eluback", "(yi>0?xi:yi+1)")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the comment. ifelse(yi>0,dyi,yi+1) is valid julia code and should be the right derivative

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried to build the package, it threw following errors:

cuda01.cu(395): error: identifier "dyi" is undefined
cuda01.cu(395): error: identifier "ifelse" is undefined
cuda01.cu(408): error: identifier "dyi" is undefined
cuda01.cu(408): error: identifier "ifelse" is undefined

I am able to build the package with the ("eluback", "eluback", "(yi>0?xi:yi+1)") code snippet.

("sigmback","sigmback","(xi*yi*(1-yi))"),
("tanhback","tanhback","(xi*(1-yi*yi))"),
("rpow","rpow","pow(yi,xi)"), # need this for Array.^Scalar
Expand Down
22 changes: 22 additions & 0 deletions src/dropout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,25 @@ function dropback!(p,x,y,dy,dx)
return dx
end


"""
alpha_dropout(x, p)

Dropout associated to the `selu` activation.

Paper Ref.:
Self-Normalizing Neural Networks
https://arxiv.org/abs/1706.02515
"""
function alpha_dropout(x, p)
training = x isa Rec
(p == 0 || !training) && return x

alpha = Float32(-1.758099)
q = Float32(1-p)
x = q*dropout(x .- alpha, p) .+ alpha #set dropped input to alpha
a = 1 / sqrt(q + alpha^2 * q*p)
b = -a * alpha * p
return a*x + b
end

20 changes: 20 additions & 0 deletions src/unary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ unary_ops = [
# "normcdfinv",
# "rcbrt",
("relu", "relu", "(xi>0?xi:0)"),
("elu", "elu", "(xi>0?xi:exp(xi)-1)"),
# "rint",
"round",
# "rsqrt",
Expand Down Expand Up @@ -99,6 +100,7 @@ end
for (f,g,y,dx) in
((:invx, :invxback, :(one(T)/xi), :(-yi*yi*dyi)),
(:relu, :reluback, :(max(zero(T),xi)), :(ifelse(yi>0,dyi,zero(T)))),
(:elu, :eluback, :(ifelse(xi>0,xi,exp(xi)-1)), :(ifelse(yi>0,dyi,yi+1))),
(:tanx, :tanhback, :(tanh(xi)), :(dyi*(one(T)-yi*yi))),
(:sigm, :sigmback,
# Numerically stable implementation from
Expand Down Expand Up @@ -153,3 +155,21 @@ broadcast(::typeof(+), a::KnetArray)=a
+(a::KnetArray)=a
-(a::KnetArray)=broadcast(-,a)

"""
selu(x)

Self-Normalizing Exponential Linear Unit. Returns
`scale*(max(0,x) + alpha*(exp(min(x,0)) - 1))`
where `scale=1.0507009` and `alpha=1.6732632`.

Paper Ref. :
Self-Normalizing Neural Networks
https://arxiv.org/abs/1706.02515
"""
function selu(x)
alpha = 1.6732632f0
scale = 1.0507009f0
p = relu(x)
m = -relu(-x)
return scale*(p + alpha*(exp(m) - 1))
end