Skip to content

Commit

Permalink
Merge pull request #1963 from cossio/bias
Browse files Browse the repository at this point in the history
AdaBelief bias correction
  • Loading branch information
ToucheSir authored May 10, 2022
2 parents 25457f5 + 6403805 commit eaa7ee8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Flux"
uuid = "587475ba-b771-5e3f-ad9e-33799f191a9c"
version = "0.13.0"
version = "0.13.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
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]])
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

2 comments on commit eaa7ee8

@ToucheSir
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/60029

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.1 -m "<description of version>" eaa7ee853543d7bb0488655f2c106e0c21d03e1c
git push origin v0.13.1

Please sign in to comment.