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

L2 Decay for Optimizers #269

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ Rmsprop(; lr=0.001, gclip=0, rho=0.9, eps=1e-6)=Rmsprop(lr, gclip, rho, eps, not


"""
Adam(;lr=0.001, gclip=0, beta1=0.9, beta2=0.999, eps=1e-8)
Adam(;lr=0.001, gclip=0, beta1=0.9, beta2=0.999, eps=1e-8, l2decay=0)
update!(w,g,p::Adam)

Container for parameters of the Adam optimization algorithm used by
Expand Down Expand Up @@ -280,9 +280,10 @@ type Adam
t::Int
fstm
scndm
l2decay::AbstractFloat
end

Adam(; lr=0.001, gclip=0, beta1=0.9, beta2=0.999, eps=1e-8)=Adam(lr, gclip, beta1, beta2, eps, 0, nothing, nothing)
Adam(; lr=0.001, gclip=0, beta1=0.9, beta2=0.999, eps=1e-8, l2decay=0)=Adam(lr, gclip, beta1, beta2, eps, 0, nothing, nothing, l2decay)


"""
Expand Down Expand Up @@ -377,6 +378,7 @@ for T in (Array{Float32},Array{Float64},KnetArray{Float32},KnetArray{Float64});
end

function update!(w::$T, g::$T, p::Adam)
l2decay!(w, g, p)
gclip!(g, p.gclip)
if p.fstm===nothing; p.fstm=zeros(w); p.scndm=zeros(w); end
p.t += 1
Expand Down Expand Up @@ -487,6 +489,12 @@ function gclip!(g, gclip)
end
end

function l2decay!(w, g, o)
o.l2decay == 0 && return g
axpy!(o.l2decay, w, g)
end


"""
optimizers(model, otype; options...)

Expand Down