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 support for Optimisers.jl #114

Merged
merged 6 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
InlineTest = "bd334432-b1e7-49c7-a2dc-dd9149e4ebd6"
OnlineStats = "a15396b6-48d5-5d58-9928-6d29437db91e"
Optimisers = "3bd65402-5787-11e9-1adc-39752487f4e2"
ParameterSchedulers = "d7d3b36b-41b8-4d0d-a2bf-768c6151755e"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Expand All @@ -34,8 +35,8 @@ Graphs = "1"
ImageCore = "0.8, 0.9"
InlineTest = "0.2"
OnlineStats = "1.5"
Parameters = "0.12"
ParameterSchedulers = "0.3.1"
Parameters = "0.12"
PrettyTables = "1, 1.1, 1.2"
ProgressMeter = "1.4"
Reexport = "1.0"
Expand Down
1 change: 1 addition & 0 deletions src/FluxTraining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module ES
end
import OnlineStats
using OnlineStats: EqualWeight, Mean, OnlineStat
import Optimisers
using Parameters
using ProgressMeter: Progress, next!
using Statistics: mean
Expand Down
18 changes: 10 additions & 8 deletions src/learner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mutable struct Learner
data::PropDict
optimizer
lossfn
# this used to store `Flux.Params` but now stores the optimiser state
# if an optim from Optimisers.jl is used
params
step::PropDict
callbacks::Callbacks
Expand Down Expand Up @@ -96,7 +98,7 @@ function Learner(
_dataiters(data),
optimizer,
lossfn,
paramsrec(model),
setupoptimstate(model, optimizer),
PropDict(),
cbs,
PropDict())
Expand Down Expand Up @@ -129,9 +131,15 @@ phasedataiter(::AbstractValidationPhase) = :validation

function model!(learner, model)
learner.model = model
learner.params = paramsrec(model)
learner.params = setupoptimstate(model, learner.optimizer)
end

# Flux.jl optimisers store `params`, while Optimisers.jl store the result of `setup`
setupoptimstate(model, ::Flux.Optimise.AbstractOptimiser) = Flux.params(model)
# Optimisers.jl has no abstract supertype so we assume non-Flux optimisers
# conform to the Optimisers.jl interface.
setupoptimstate(model, optim) = Optimisers.setup(optim, model)


_dataiters(d::PropDict) = d
_dataiters(t::NamedTuple) = PropDict(pairs(t))
Expand All @@ -146,9 +154,3 @@ function _dataiters(t::Tuple)
error("Please pass a `NamedTuple` or `PropDict` as `data`.")
end
end


paramsrec(m) = Flux.params(m)
paramsrec(t::Union{Tuple,NamedTuple}) = map(paramsrec, t)

# Callback utilities
18 changes: 15 additions & 3 deletions src/training.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,30 @@ function step! end
function step!(learner, phase::TrainingPhase, batch)
xs, ys = batch
runstep(learner, phase, (; xs=xs, ys=ys)) do handle, state
state.grads = gradient(learner.params) do
state.ŷs = learner.model(state.xs)

state.grads, = gradient(learner.model) do model
Copy link
Member

Choose a reason for hiding this comment

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

I think here you want to take the gradient w.r.t. learner.params when the optimizer is a Flux.Optimise.AbstractOptimiser. Conversely, if it is not, you take the gradient w.r.t. learner.model like you are now.

This is why update! below is erroring cause you need to Grads object for the old optimizers. And you can only get that with implicit params.

I think some dispatch for the gradient would be easiest. Another option is to have a utility that takes the model, the gradient w.r.t. it, and Params, then it produces a Grads to match.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I figured. Was hoping there may be a way to have the same Zygote.gradient call work but I guess not. I'll add a dispatch on the optimiser there.

Copy link
Member

Choose a reason for hiding this comment

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

Well what you're asking for might come eventually in a later version of Flux as part of the AD-agnostic push. So, the code might eventually get simpler.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's good to know. Definitely let me know then, so I can clean this up again.

state.ŷs = model(state.xs)
handle(LossBegin())
state.loss = learner.lossfn(state.ŷs, state.ys)
handle(BackwardBegin())
return state.loss
end
handle(BackwardEnd())
update!(learner.optimizer, learner.params, state.grads)
learner.params, learner.model = _update!(
learner.optimizer, learner.params, learner.model, state.grads)
end
end

# Handle both old Flux.jl and new Optimisers.jl optimisers
function _update!(optimizer::Flux.Optimise.AbstractOptimiser, params, model, grads)
update!(optimizer, model, grads)
Copy link
Member Author

Choose a reason for hiding this comment

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

This currently throws an error. For context params isa Params and grads is no longer a Grads. Is a Params even needed anymore?

Copy link
Member

Choose a reason for hiding this comment

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

Left a comment above about this.

return params, model
end
function _update!(_, st, model, grads)
st, model = Optimisers.update!(st, model, grads)
return st, model
end


function step!(learner, phase::ValidationPhase, batch)
xs, ys = batch
Expand Down
7 changes: 7 additions & 0 deletions test/training.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ end
fit!(learner, 5)
@test learner.model.coeff[1] ≈ 3 atol = 0.1
end


@testset "Optimisers.jl compatibility" begin
learner = testlearner(coeff = 3, opt=Optimisers.Descent(0.001))
Copy link
Member Author

Choose a reason for hiding this comment

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

This already passes 👍 but all the old optim tests are broken for the time being

fit!(learner, 5)
@test learner.model.coeff[1] ≈ 3 atol = 0.1
end