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 a new optimizer PAdam #149

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Optimisers.AMSGrad
Optimisers.NAdam
Optimisers.AdamW
Optimisers.AdaBelief
Optimisers.PAdam
```

In addition to the main course, you may wish to order some of these condiments:
Expand Down
2 changes: 1 addition & 1 deletion src/Optimisers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export destructure

include("rules.jl")
export Descent, Adam, Momentum, Nesterov, Rprop, RMSProp,
AdaGrad, AdaMax, AdaDelta, AMSGrad, NAdam, AdamW, RAdam, OAdam, AdaBelief,
AdaGrad, AdaMax, AdaDelta, AMSGrad, NAdam, AdamW, RAdam, OAdam, AdaBelief, PAdam,
WeightDecay, ClipGrad, ClipNorm, OptimiserChain, Lion,
AccumGrad

Expand Down
34 changes: 34 additions & 0 deletions src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,41 @@ function apply!(o::AdaBelief, state, x, dx)

return (mt, st, βt .* β), dx′
end
"""
PAdam(η = 1f-2, β = (9f-1, 9.99f-1), ρ = 2.5f-1, eps(typeof(η)))

The partially adaptive momentum estimation method (PADAM) [https://arxiv.org/pdf/1806.06763v1.pdf]

# Parameters
- Learning rate (`η`): Amount by which gradients are discounted before updating
the weights.
- Decay of momentums (`β::Tuple`): Exponential decay for the first (β1) and the
second (β2) momentum estimate.
- Partially adaptive parameter (`p`): Varies between 0 and 0.5.
- Machine epsilon (`ϵ`): Constant to prevent division by zero
(no need to change default)
"""
struct PAdam{T} <: AbstractRule
eta::T
beta::Tuple{T, T}
rho::T
epsilon::T
end
PAdam(η = 1f-2, β = (9f-1, 9.99f-1), ρ = 2.5f-1, ϵ = eps(typeof(η))) = PAdam{typeof(η)}(η, β, ρ, ϵ)

init(o::PAdam, x::AbstractArray) = (onevalue(o.epsilon, x), onevalue(o.epsilon, x), onevalue(o.epsilon, x))

function apply!(o::PAdam, state, x, dx)
η, β, ρ, ϵ = o.eta, o.beta, o.rho, o.epsilon
mt, vt, v̂t = state

@.. mt = β[1] * mt + (1 - β[1]) * dx
@.. vt = β[2] * vt + (1 - β[2]) * abs2(dx)
@.. v̂t = max(v̂t, vt)
dx′ = @lazy η * mt / (v̂t ^ ρ + ϵ)

return (mt, vt, v̂t), dx′
end
"""
WeightDecay(γ = 5f-4)

Expand Down
6 changes: 3 additions & 3 deletions test/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RULES = [
# All the rules at default settings:
Descent(), Adam(), Momentum(), Nesterov(), Rprop(), RMSProp(),
AdaGrad(), AdaMax(), AdaDelta(), AMSGrad(), NAdam(),
AdamW(), RAdam(), OAdam(), AdaBelief(), Lion(),
AdamW(), RAdam(), OAdam(), AdaBelief(), PAdam(), Lion(),
# A few chained combinations:
OptimiserChain(WeightDecay(), Adam(0.001)),
OptimiserChain(ClipNorm(), Adam(0.001)),
Expand Down Expand Up @@ -181,7 +181,7 @@ end
empty!(LOG)
@testset "$(name(opt))" for opt in [
# The Flux PR had 1e-2 for all. But AdaDelta(ρ) needs ρ≈0.9 not small. And it helps to make ε not too small too:
Adam(1e-2), RMSProp(1e-2), RAdam(1e-2), OAdam(1e-2), AdaGrad(1e-2), AdaDelta(0.9, 1e-5), NAdam(1e-2), AdaBelief(1e-2),
Adam(1e-2), RMSProp(1e-2), RAdam(1e-2), OAdam(1e-2), AdaGrad(1e-2), AdaDelta(0.9, 1e-5), NAdam(1e-2), AdaBelief(1e-2), PAdam(1e-2),
4SAnalyticsnModelling marked this conversation as resolved.
Show resolved Hide resolved
# These weren't in Flux PR:
Descent(1e-2), Momentum(1e-2), Nesterov(1e-2), AdamW(1e-2),
]
Expand Down Expand Up @@ -266,4 +266,4 @@ end

tree, x4 = Optimisers.update(tree, x3, g4)
@test x4 ≈ x3
end
end