-
-
Notifications
You must be signed in to change notification settings - Fork 608
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 step! #1833
base: master
Are you sure you want to change the base?
add step! #1833
Changes from all commits
2fa1c7c
c8f147b
1f34fd7
0765010
5b4fc17
12a8284
68a0590
c321e4f
bb7b5c5
cc31acb
5220fe9
fa68993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
using Juno | ||
import Zygote: Params, gradient | ||
import Zygote: Params, withgradient | ||
|
||
""" | ||
update!(x, x̄) | ||
|
@@ -80,6 +80,35 @@ end | |
batchmemaybe(x) = tuple(x) | ||
batchmemaybe(x::Tuple) = x | ||
|
||
""" | ||
optimstep!(loss, params, opt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One vote for something evoking If the longer-term plan is to use Optimisers.jl, this may not fit with Maybe it should just be 3-arg train!(loss, ::Params, data, ::AbstractOptimiser) # calls loss(d...) for d in data
train!(loss, ::Params, ::AbstractOptimiser) # calls loss() since there is no data |
||
|
||
`optimstep!` uses a `loss` function (with no inputs) to improve the [Model parameters](@ref) (`params`) | ||
based on a pluggable [Optimisers](@ref) (`opt`). It represents a single step in | ||
the training loop `train!`. | ||
|
||
The default implementation for `optimstep!` is takes the gradient of `loss` | ||
and calls `Flux.Optimise.update!` to adjust the parameters, but you can overload | ||
`optimstep!` for specific types of `opt`. This can be useful if your optimization routine | ||
has does not follow the standard gradient descent procedure (e.g. gradient-free optimizers). | ||
|
||
Unlike `train!`, the loss function of `optimstep!` accepts no input. | ||
Instead, `train!` cycles through the data in a loop and calls `optimstep!`: | ||
```julia | ||
for d in data | ||
optimstep!(ps, opt) do | ||
loss(d) | ||
end | ||
end | ||
``` | ||
If you are writing [Custom Training loops](@ref), then you should follow this pattern. | ||
""" | ||
function optimstep!(loss, params, opt) | ||
val, gs = withgradient(loss, params) | ||
darsnack marked this conversation as resolved.
Show resolved
Hide resolved
|
||
update!(opt, params, gs) | ||
return val, gs | ||
end | ||
|
||
""" | ||
train!(loss, params, data, opt; cb) | ||
|
||
|
@@ -106,10 +135,9 @@ function train!(loss, ps, data, opt; cb = () -> ()) | |
cb = runall(cb) | ||
@progress for d in data | ||
try | ||
gs = gradient(ps) do | ||
optimstep!(ps, opt) do | ||
loss(batchmemaybe(d)...) | ||
end | ||
update!(opt, ps, gs) | ||
cb() | ||
catch ex | ||
if ex isa StopException | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is right at the beginning instead of in the
Custom Training Loop Section
. It seems to me like the custom training loop section might either be redundant or demonstrate how to have a custom gradient calculation now.