Skip to content

Commit

Permalink
simplify lr decay
Browse files Browse the repository at this point in the history
  • Loading branch information
cossio committed Sep 12, 2020
1 parent 05fb997 commit b166d74
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "RestrictedBoltzmannMachines"
uuid = "12e6b396-7db5-4506-8cb6-664a4fe1e50e"
authors = ["Jorge Fernandez-de-Cossio-Diaz <[email protected]>"]
version = "0.2.17"
version = "0.2.18"

[deps]
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Expand Down
17 changes: 6 additions & 11 deletions src/train/lr_schedules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ using Flux: Optimiser
export Optimiser, SqrtDecay, GeometricDecay

abstract type AbstractLrDecay end
Flux.Optimise.apply!(o::AbstractLrDecay, x, Δ) = Δ .*= update_lr!(o, x)
function Flux.Optimise.apply!(o::AbstractLrDecay, x, Δ)
t::Int = o.t[x] = get(o.t, x, 0) + 1
Δ .*= update_lr(o, t)
end

"""
SqrtDecay
Expand All @@ -22,11 +25,7 @@ mutable struct SqrtDecay <: AbstractLrDecay
t::IdDict
end
SqrtDecay(; lr0=1, lrmin=0, decay=0) = SqrtDecay(lr0, lrmin, decay, IdDict())

function update_lr!(o::SqrtDecay, x)
t::Int = o.t[x] = get(o.t, x, 0) + 1
return max(o.lr0 / (1 + (t - 1) * o.decay), o.lrmin)
end
update_lr(o::SqrtDecay, t::Real) = max(o.lr0 / sqrt(1 + (t - 1) * o.decay), o.lrmin)

"""
GeometricDecay
Expand All @@ -42,8 +41,4 @@ mutable struct GeometricDecay <: AbstractLrDecay
t::IdDict
end
GeometricDecay(; lr0=1, lrmin=0, decay=1) = GeometricDecay(lr0, lrmin, decay, IdDict())

function update_lr!(o::GeometricDecay, x)
t::Int = o.t[x] = get(o.t, x, 0) + 1
return max(o.lr0 * o.decay^t, o.lrmin)
end
update_lr(o::GeometricDecay, t::Real) = max(o.lr0 * o.decay^t, o.lrmin)

2 comments on commit b166d74

@cossio
Copy link
Owner

@cossio cossio commented on b166d74 Sep 12, 2020

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/21284

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.2.18 -m "<description of version>" b166d743af3d87ee135317ba1641a67c4889a7a5
git push origin v0.2.18

Please sign in to comment.