-
Notifications
You must be signed in to change notification settings - Fork 525
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 cosine restart learning rate #2953
base: devel
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -58,6 +58,8 @@ | |
) | ||
from deepmd.utils.learning_rate import ( | ||
LearningRateExp, | ||
LearningRateCos, | ||
LearningRateCosRestarts, | ||
) | ||
from deepmd.utils.sess import ( | ||
run_sess, | ||
|
@@ -113,13 +115,21 @@ | |
scale_lr_coef = np.sqrt(self.run_opt.world_size).real | ||
else: | ||
scale_lr_coef = 1.0 | ||
lr_type = lr_param.get("type", "exp") | ||
if lr_type == "exp": | ||
self.lr_type = lr_param.get("type", "exp") | ||
if self.lr_type == "exp": | ||
lr = LearningRateExp( | ||
lr_param["start_lr"], lr_param["stop_lr"], lr_param["decay_steps"] | ||
) | ||
elif self.lr_type == "cos": | ||
lr = LearningRateCos( | ||
lr_param["start_lr"], lr_param["stop_lr"], lr_param["decay_steps"] | ||
) | ||
elif self.lr_type == "cosrestart": | ||
lr = LearningRateCosRestarts( | ||
lr_param["start_lr"], lr_param["stop_lr"], lr_param["decay_steps"] | ||
) | ||
else: | ||
raise RuntimeError("unknown learning_rate type " + lr_type) | ||
raise RuntimeError("unknown learning_rate type " + self.lr_type) | ||
return lr, scale_lr_coef | ||
|
||
# learning rate | ||
|
@@ -553,29 +563,31 @@ | |
is_first_step = True | ||
self.cur_batch = cur_batch | ||
if not self.multi_task_mode: | ||
log.info( | ||
"start training at lr %.2e (== %.2e), decay_step %d, decay_rate %f, final lr will be %.2e" | ||
% ( | ||
run_sess(self.sess, self.learning_rate), | ||
self.lr.value(cur_batch), | ||
self.lr.decay_steps_, | ||
self.lr.decay_rate_, | ||
self.lr.value(stop_batch), | ||
) | ||
) | ||
else: | ||
for fitting_key in self.fitting: | ||
if self.lr_type == "exp": | ||
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. It's not a good behavior to switch the learning rate in the |
||
log.info( | ||
"%s: start training at lr %.2e (== %.2e), decay_step %d, decay_rate %f, final lr will be %.2e" | ||
"start training at lr %.2e (== %.2e), decay_step %d, decay_rate %f, final lr will be %.2e" | ||
% ( | ||
fitting_key, | ||
run_sess(self.sess, self.learning_rate_dict[fitting_key]), | ||
self.lr_dict[fitting_key].value(cur_batch), | ||
self.lr_dict[fitting_key].decay_steps_, | ||
self.lr_dict[fitting_key].decay_rate_, | ||
self.lr_dict[fitting_key].value(stop_batch), | ||
run_sess(self.sess, self.learning_rate), | ||
self.lr.value(cur_batch), | ||
self.lr.decay_steps_, | ||
self.lr.decay_rate_, | ||
self.lr.value(stop_batch), | ||
) | ||
) | ||
else: | ||
for fitting_key in self.fitting: | ||
if self.lr_type == "exp": | ||
log.info( | ||
"%s: start training at lr %.2e (== %.2e), decay_step %d, decay_rate %f, final lr will be %.2e" | ||
% ( | ||
fitting_key, | ||
run_sess(self.sess, self.learning_rate_dict[fitting_key]), | ||
self.lr_dict[fitting_key].value(cur_batch), | ||
self.lr_dict[fitting_key].decay_steps_, | ||
self.lr_dict[fitting_key].decay_rate_, | ||
self.lr_dict[fitting_key].value(stop_batch), | ||
) | ||
) | ||
|
||
prf_options = None | ||
prf_run_metadata = None | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1010,13 +1010,42 @@ def learning_rate_exp(): | |
] | ||
return args | ||
|
||
def learning_rate_cos(): | ||
doc_start_lr = "The learning rate the start of the training." | ||
doc_stop_lr = "The desired learning rate at the end of the training." | ||
doc_decay_steps = ( | ||
"Number of steps to decay over." | ||
) | ||
|
||
args = [ | ||
Argument("start_lr", float, optional=True, default=1e-3, doc=doc_start_lr), | ||
Argument("stop_lr", float, optional=True, default=1e-8, doc=doc_stop_lr), | ||
Argument("decay_steps", int, optional=True, default=100000, doc=doc_decay_steps), | ||
] | ||
return args | ||
|
||
def learning_rate_cosrestarts(): | ||
doc_start_lr = "The learning rate the start of the training." | ||
doc_stop_lr = "The desired learning rate at the end of the training." | ||
doc_decay_steps = ( | ||
"Number of steps to decay over of the first decay." | ||
) | ||
|
||
args = [ | ||
Argument("start_lr", float, optional=True, default=1e-3, doc=doc_start_lr), | ||
Argument("stop_lr", float, optional=True, default=1e-8, doc=doc_stop_lr), | ||
Argument("decay_steps", int, optional=True, default=10000, doc=doc_decay_steps), | ||
] | ||
return args | ||
|
||
def learning_rate_variant_type_args(): | ||
doc_lr = "The type of the learning rate." | ||
|
||
return Variant( | ||
"type", | ||
[Argument("exp", dict, learning_rate_exp())], | ||
[Argument("exp", dict, learning_rate_exp()), | ||
Argument("cos", dict, learning_rate_cos()), | ||
Argument("cosrestart", dict, learning_rate_cosrestarts())], | ||
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. You may need to add some documentation to variants ( |
||
optional=True, | ||
default_tag="exp", | ||
doc=doc_lr, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,3 +105,174 @@ | |
def value(self, step: int) -> float: | ||
"""Get the lr at a certain step.""" | ||
return self.start_lr_ * np.power(self.decay_rate_, (step // self.decay_steps_)) | ||
|
||
class LearningRateCos: | ||
r"""The cosine decaying learning rate. | ||
|
||
The function returns the decayed learning rate. It is computed as: | ||
```python | ||
global_step = min(global_step, decay_steps) | ||
cosine_decay = 0.5 * (1 + cos(pi * global_step / decay_steps)) | ||
decayed = (1 - alpha) * cosine_decay + alpha | ||
decayed_learning_rate = learning_rate * decayed | ||
``` | ||
Comment on lines
+113
to
+118
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. Please use this style: https://numpydoc.readthedocs.io/en/latest/format.html#other-points-to-keep-in-mind |
||
|
||
Parameters | ||
---------- | ||
start_lr | ||
Starting learning rate | ||
stop_lr | ||
Minimum learning rate value as a fraction of learning_rate. | ||
decay_steps | ||
Number of steps to decay over. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
start_lr: float, | ||
stop_lr: float = 5e-8, | ||
decay_steps: int = 100000, | ||
) -> None: | ||
"""Constructor.""" | ||
self.cd = {} | ||
self.cd["start_lr"] = start_lr | ||
self.cd["stop_lr"] = stop_lr | ||
self.cd["decay_steps"] = decay_steps | ||
self.start_lr_ = self.cd["start_lr"] | ||
self.alpha_ = self.cd["stop_lr"]/self.cd["start_lr"] | ||
|
||
def build( | ||
self, global_step: tf.Tensor, stop_step: Optional[int] = None | ||
) -> tf.Tensor: | ||
"""Build the learning rate. | ||
|
||
Parameters | ||
---------- | ||
global_step | ||
The tf Tensor prividing the global training step | ||
stop_step | ||
The stop step. | ||
|
||
Returns | ||
------- | ||
learning_rate | ||
The learning rate | ||
""" | ||
if stop_step is None: | ||
self.decay_steps_ = ( | ||
self.cd["decay_steps"] if self.cd["decay_steps"] is not None else 100000 | ||
) | ||
else: | ||
self.stop_lr_ = ( | ||
self.cd["stop_lr"] if self.cd["stop_lr"] is not None else 5e-8 | ||
) | ||
self.decay_steps_ = ( | ||
self.cd["decay_steps"] | ||
if self.cd["decay_steps"] is not None | ||
else stop_step | ||
) | ||
|
||
return tf.train.cosine_decay( | ||
self.start_lr_, | ||
global_step, | ||
self.decay_steps_, | ||
self.alpha_, | ||
name="cosine", | ||
) | ||
|
||
def start_lr(self) -> float: | ||
"""Get the start lr.""" | ||
return self.start_lr_ | ||
|
||
def value(self, step: int) -> float: | ||
"""Get the lr at a certain step.""" | ||
step = min(step, self.decay_steps_) | ||
cosine_decay = 0.5 * (1 + np.cos(np.pi * step / self.decay_steps_)) | ||
decayed = (1 - self.alpha_) * cosine_decay + self.alpha_ | ||
decayed_learning_rate = self.start_lr_ * decayed | ||
return decayed_learning_rate | ||
|
||
|
||
class LearningRateCosRestarts: | ||
r"""The cosine decaying restart learning rate. | ||
|
||
The function returns the cosine decayed learning rate while taking into account | ||
possible warm restarts. | ||
``` | ||
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. This line should be removed. |
||
|
||
Parameters | ||
---------- | ||
start_lr | ||
Starting learning rate | ||
stop_lr | ||
Minimum learning rate value as a fraction of learning_rate. | ||
decay_steps | ||
Number of steps to decay over. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
start_lr: float, | ||
stop_lr: float = 5e-8, | ||
decay_steps: int = 10000, | ||
) -> None: | ||
"""Constructor.""" | ||
self.cd = {} | ||
self.cd["start_lr"] = start_lr | ||
self.cd["stop_lr"] = stop_lr | ||
self.cd["decay_steps"] = decay_steps | ||
self.start_lr_ = self.cd["start_lr"] | ||
self.alpha_ = self.cd["stop_lr"]/self.cd["start_lr"] | ||
|
||
def build( | ||
self, global_step: tf.Tensor, stop_step: Optional[int] = None | ||
) -> tf.Tensor: | ||
"""Build the learning rate. | ||
|
||
Parameters | ||
---------- | ||
global_step | ||
The tf Tensor prividing the global training step | ||
stop_step | ||
The stop step. | ||
|
||
Returns | ||
------- | ||
learning_rate | ||
The learning rate | ||
""" | ||
if stop_step is None: | ||
self.decay_steps_ = ( | ||
self.cd["decay_steps"] if self.cd["decay_steps"] is not None else 10000 | ||
) | ||
else: | ||
self.stop_lr_ = ( | ||
self.cd["stop_lr"] if self.cd["stop_lr"] is not None else 5e-8 | ||
) | ||
self.decay_steps_ = ( | ||
self.cd["decay_steps"] | ||
if self.cd["decay_steps"] is not None | ||
else stop_step | ||
) | ||
|
||
|
||
|
||
return tf.train.cosine_decay_restarts( | ||
learning_rate=self.start_lr_, | ||
global_step=global_step, | ||
first_decay_steps=self.decay_steps_, | ||
alpha=self.alpha_, | ||
name="cosinerestart", | ||
) | ||
|
||
def start_lr(self) -> float: | ||
"""Get the start lr.""" | ||
return self.start_lr_ | ||
|
||
def value(self, step: int) -> float: | ||
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. you may not need to implement the value method if you do not print the information regarding the learning rate at the beginning of the training: |
||
"""Get the lr at a certain step. Need to revise later""" | ||
step = min(step, self.decay_steps_) | ||
cosine_decay = 0.5 * (1 + np.cos(np.pi * step / self.decay_steps_)) | ||
decayed = (1 - self.alpha_) * cosine_decay + self.alpha_ | ||
decayed_learning_rate = self.start_lr_ * decayed | ||
return decayed_learning_rate | ||
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.
It seems that it has been renamed to
silu
: tensorflow/tensorflow#41066