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

modifications to support proximal optimization #778

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/Flux.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using MacroTools, Juno, Requires, Reexport, Statistics, Random
using MacroTools: @forward

export Chain, Dense, Maxout, RNN, LSTM, GRU, Conv, CrossCor, ConvTranspose, MaxPool, MeanPool,
DepthwiseConv, Dropout, AlphaDropout, LayerNorm, BatchNorm, InstanceNorm, GroupNorm,
DepthwiseConv, Dropout, AlphaDropout, LayerNorm, BatchNorm, InstanceNorm, GroupNorm,
params, mapleaves, cpu, gpu, f32, f64

@reexport using NNlib
Expand All @@ -21,7 +21,8 @@ using .Optimise
using .Optimise: @epochs
export SGD, Descent, ADAM, Momentum, Nesterov, RMSProp,
ADAGrad, AdaMax, ADADelta, AMSGrad, NADAM,
ADAMW, InvDecay, ExpDecay, WeightDecay
ADAMW, InvDecay, ExpDecay, WeightDecay,
L1_regularization, L2_regularization

include("utils.jl")
include("onehot.jl")
Expand Down
4 changes: 3 additions & 1 deletion src/optimise/Optimise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ module Optimise
export train!,
SGD, Descent, ADAM, Momentum, Nesterov, RMSProp,
ADAGrad, AdaMax, ADADelta, AMSGrad, NADAM, ADAMW,
InvDecay, ExpDecay, WeightDecay, stop, Optimiser
InvDecay, ExpDecay, WeightDecay, stop, Optimiser,
L1_regularization, L2_regularization

include("optimisers.jl")
include("regularization.jl")
include("train.jl")
include("deprecations.jl")

Expand Down
34 changes: 34 additions & 0 deletions src/optimise/regularization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Proximal updates for convex regularization
using LinearAlgebra

struct L1_regularization
α::Float64
f::Function
end

shrink(α) = f(z) = z > α ? α : z < -α ? -α : z
Copy link
Member

Choose a reason for hiding this comment

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

Passing a closure to the forward pass can be simplified since we already have alpha. Might be worth just making it into a normal function and combining it in the update rule?


function L1_regularization(α)
return L1_regularization(α, shrink(α))
end

function apply!(r::L1_regularization, x, Δ)
z = data(x)
Δ .= r.f.(z)
return Δ
end

struct L2_regularization
α::Float64
end

function apply!(r::L2_regularization, x, Δ)
z = data(x)
norm_z = norm(z)
if norm_z > r.α
Δ .= (r.α/norm_z) .* z
else
Δ .= z
end
return Δ
end
5 changes: 4 additions & 1 deletion src/optimise/train.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The callback can call `Flux.stop()` to interrupt the training loop.

Multiple optimisers and callbacks can be passed to `opt` and `cb` as arrays.
"""
function train!(loss, ps, data, opt; cb = () -> ())
function train!(loss, ps, data, opt; regularization = nothing, cb = () -> ())
ps = Params(ps)
cb = runall(cb)
@progress for d in data
Expand All @@ -72,6 +72,9 @@ function train!(loss, ps, data, opt; cb = () -> ())
loss(d...)
end
update!(opt, ps, gs)
if regularization != nothing
update!(regularization, ps, gs)
end
if cb() == :stop
depwarn("Use of `:stop` is deprecated; use `Flux.stop()` instead", :stop)
break
Expand Down