-
-
Notifications
You must be signed in to change notification settings - Fork 612
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 ParameterSchedulers.jl to docs #1511
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -137,6 +137,36 @@ In this manner it is possible to compose optimisers for some added flexibility. | |||||||||
Flux.Optimise.Optimiser | ||||||||||
``` | ||||||||||
|
||||||||||
## Scheduling Optimisers | ||||||||||
|
||||||||||
In practice, it is fairly common to schedule the learning rate of an optimiser to obtain faster convergence. There are a variety of popular scheduling policies, and you can find implementations of them in [ParameterSchedulers.jl](https://darsnack.github.io/ParameterSchedulers.jl/dev/README.html). The documentation for ParameterSchedulers.jl provides a more detailed overview of the different scheduling policies, and how to use them with Flux optimizers. Below, we provide a brief snippet illustrating a [cosine annealing](https://arxiv.org/pdf/1608.03983.pdf) schedule with a momentum optimiser. | ||||||||||
|
||||||||||
First, we import ParameterSchedulers.jl and initalize a cosine annealing schedule to varying the learning rate between `1e-4` and `1e-2` every 10 steps. We also create a new [`Momentum`](@ref) optimiser. | ||||||||||
```julia | ||||||||||
using ParameterSchedulers | ||||||||||
|
||||||||||
schedule = ScheduleIterator(Cos(λ0 = 1e-4, λ1 = 1e-2, period = 10)) | ||||||||||
opt = Momentum() | ||||||||||
``` | ||||||||||
|
||||||||||
Next, you can use your schedule directly in a `for`-loop: | ||||||||||
```julia | ||||||||||
for epoch in 1:100 | ||||||||||
opt.eta = next!(schedule) | ||||||||||
# your training code here | ||||||||||
end | ||||||||||
``` | ||||||||||
|
||||||||||
`schedule` can also be indexed (e.g. `schedule[100]`) or iterated like any iterator in Julia: | ||||||||||
```julia | ||||||||||
for (eta, epoch) in zip(schedule, 1:100) | ||||||||||
opt.eta = eta | ||||||||||
# your training code here | ||||||||||
Comment on lines
+163
to
+164
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.
Suggested change
|
||||||||||
end | ||||||||||
``` | ||||||||||
|
||||||||||
ParameterSchedulers.jl allows for many more scheduling policies including arbitrary functions, looping any function with a given period, or sequences of many schedules. See the ParameterSchedulers.jl documentation for more info. | ||||||||||
|
||||||||||
## Decays | ||||||||||
|
||||||||||
Similar to optimisers, Flux also defines some simple decays that can be used in conjunction with other optimisers, or standalone. | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
The original is actually the expected behavior. The first call to
next!(schedule)
will return the very first parameter value. I think that makes sense given that theopt.eta
value when you constructopt
can be out of sync with the schedule policy. This style ensures that the schedule policy sets is the authoritativeeta
on every iteration.