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

AdaBelief bias correction #1963

Merged
merged 5 commits into from
May 10, 2022
Merged
Changes from 4 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
22 changes: 19 additions & 3 deletions src/optimise/optimisers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,26 @@ AdaBelief(η::Real, β::Tuple, state::IdDict) = AdaBelief(η, β, EPS, state)

function apply!(o::AdaBelief, x, Δ)
η, β = o.eta, o.beta
mt, st = get!(() -> (zero(x), zero(x)), o.state, x)::NTuple{2,typeof(x)}

mt, st, βp = get!(o.state, x) do
(zero(x), zero(x), Float64[β[1], β[2]])
Copy link
Member

Choose a reason for hiding this comment

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

🤮

I know this is what ADAM does, but it really is non-optimal. Not a blocker for this PR, but more impetus to move to Optimisers.jl ASAP.

end :: Tuple{typeof(x), typeof(x), Vector{Float64}}

#= st is a variance and can go to zero. This is in contrast to ADAM, which uses the
second moment which is usually far enough from zero. This is problematic, since st
can be slightly negative due to numerical error, and the square root below will fail.
Also, if we want to differentiate through the optimizer, √0 is not differentiable.
To protect against this, we add a small number, st -> st + eps2.
The original implementation (https://github.com/juntang-zhuang/Adabelief-Optimizer)
uses the square of Adam's epsilon, which we do here.
See also: https://github.com/juntang-zhuang/Adabelief-Optimizer/issues/61 =#
eps2 = o.epsilon^2 # TODO: make epsilon^2 the default in next breaking release

@. mt = β[1] * mt + (1 - β[1]) * Δ
@. st = β[2] * st + (1 - β[2]) * (Δ - mt) * conj(Δ - mt)
@. Δ = η * mt / (√(st) + o.epsilon)
@. st = β[2] * st + (1 - β[2]) * (Δ - mt) * conj(Δ - mt) + eps2
@. Δ = η * mt / (1 - βp[1]) / (√(st / (1 - βp[2])) + eps2)
βp .= βp .* β

return Δ
end

Expand Down