-
Notifications
You must be signed in to change notification settings - Fork 826
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 polynomial scheduler #7260
Merged
Merged
add polynomial scheduler #7260
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
93f9cd4
add polynomial scheduler
L1aoXingyu 5b26d0f
add polynomial eager unittest
L1aoXingyu cd499b2
add unittest for polynomial scheduler
L1aoXingyu 41dce03
Merge branch 'master' of github.com:Oneflow-Inc/oneflow into add_poly…
L1aoXingyu e9ec3c2
refine docstr
L1aoXingyu bd0137f
Merge branch 'master' into add_polynomial_scheduler
L1aoXingyu ed7f3eb
auto format by CI
oneflow-ci-bot 1abe5e4
refine arguments with type
L1aoXingyu f85e462
Merge branch 'add_polynomial_scheduler' of github.com:Oneflow-Inc/one…
L1aoXingyu 5ba986a
Merge branch 'master' into add_polynomial_scheduler
oneflow-ci-bot f7103b8
refine docstr
L1aoXingyu d6145eb
Merge branches 'add_polynomial_scheduler' and 'add_polynomial_schedul…
L1aoXingyu ed08841
Merge branch 'master' into add_polynomial_scheduler
L1aoXingyu c36995e
Merge branch 'master' into add_polynomial_scheduler
L1aoXingyu c0dcd48
Merge branch 'master' into add_polynomial_scheduler
oneflow-ci-bot 073c9da
Merge branch 'master' into add_polynomial_scheduler
oneflow-ci-bot 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ Optimizers | |
StepLR, | ||
MultiStepLR, | ||
ExponentialLR, | ||
ReduceLROnPlateau | ||
ReduceLROnPlateau, | ||
PolynomialLR |
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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
""" | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
|
||
import math | ||
|
||
from .lr_scheduler import LrScheduler | ||
|
||
|
||
class PolynomialLR(LrScheduler): | ||
r""" | ||
This operator creates a polynomial decayed learning rate scheduler. | ||
The learning rate will be updated as follows: | ||
|
||
If cycle is `True`, the equation is: | ||
|
||
.. math:: | ||
\begin{aligned} | ||
& decay\_batch = decay\_batch*ceil(\frac{current\_batch}{decay\_batch}) \\ | ||
& learning\_rate = (base\_lr-end\_lr)*(1-\frac{current\_batch}{decay\_batch})^{power}+end\_lr | ||
\end{aligned} | ||
|
||
If cycle is `False`, the equation is: | ||
|
||
.. math:: | ||
\begin{aligned} | ||
& decay\_batch = min(decay\_batch, current\_batch) \\ | ||
& learning\_rate = (base\_lr-end\_lr)*(1-\frac{current\_batch}{decay\_batch})^{power}+end\_lr | ||
\end{aligned} | ||
|
||
Args: | ||
optimizer (Optimizer): Wrapper optimizer. | ||
steps (int): The decayed steps. | ||
end_learning_rate (float, optional): The final learning rate. Defaults to 0.0001. | ||
power (float, optional): The power of polynomial. Defaults to 1.0. | ||
cycle (bool, optional): If cycle is True, the scheduler will decay the learning rate every decay steps. Defaults to False. | ||
|
||
For example: | ||
|
||
.. code-block:: python | ||
|
||
import oneflow as flow | ||
|
||
... | ||
polynomial_scheduler = flow.optim.lr_scheduler.PolynomialLR( | ||
optimizer, steps=5, end_learning_rate=0.00001, power=2 | ||
) | ||
|
||
for epoch in range(num_epoch): | ||
train(...) | ||
polynomial_scheduler.step() | ||
""" | ||
|
||
def __init__( | ||
self, | ||
optimizer, | ||
steps: int, | ||
end_learning_rate: float = 0.0001, | ||
power: float = 1.0, | ||
cycle: bool = False, | ||
last_step: int = -1, | ||
verbose: bool = False, | ||
): | ||
assert steps > 0, f"steps must greater than zero, but got {steps}" | ||
self.max_decay_steps = steps | ||
self.end_learning_rate = end_learning_rate | ||
self.power = power | ||
self.cycle = cycle | ||
super().__init__(optimizer, last_step, verbose) | ||
|
||
def get_lr(self): | ||
decay_batch = self.max_decay_steps | ||
cur_batch = self.last_step | ||
if self.cycle: | ||
if cur_batch == 0: | ||
cur_batch = 1 | ||
decay_batch = decay_batch * math.ceil(cur_batch / decay_batch) | ||
else: | ||
cur_batch = min(cur_batch, decay_batch) | ||
return [ | ||
(base_lr - self.end_learning_rate) | ||
* ((1 - cur_batch / decay_batch) ** (self.power)) | ||
+ self.end_learning_rate | ||
for base_lr in self.base_lrs | ||
] | ||
|
||
def _generate_conf_for_graph(self, opt_confs): | ||
for opt_conf in opt_confs: | ||
learning_rate_decay_conf = opt_conf.mutable_learning_rate_decay() | ||
learning_rate_decay_conf.mutable_polynomial_conf().set_decay_batches( | ||
self.max_decay_steps | ||
) | ||
learning_rate_decay_conf.mutable_polynomial_conf().set_end_learning_rate( | ||
self.end_learning_rate | ||
) | ||
learning_rate_decay_conf.mutable_polynomial_conf().set_power(self.power) | ||
learning_rate_decay_conf.mutable_polynomial_conf().set_cycle(self.cycle) |
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
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
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.
文档每一段可以加点空行,可以参考 https://github.com/Oneflow-Inc/OneTeam/issues/94 编译一下文档,看是否符合预期
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.
好的,我试试