|
|
|
|
@ -27,7 +27,7 @@ from . import nn
|
|
|
|
|
from . import ops
|
|
|
|
|
from . import tensor
|
|
|
|
|
from ..initializer import init_on_cpu
|
|
|
|
|
from ..framework import default_main_program, Parameter
|
|
|
|
|
from ..framework import default_main_program, Parameter, unique_name
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
|
'exponential_decay', 'natural_exp_decay', 'inverse_time_decay',
|
|
|
|
|
@ -63,6 +63,7 @@ def noam_decay(d_model, warmup_steps):
|
|
|
|
|
Returns:
|
|
|
|
|
The decayed learning rate.
|
|
|
|
|
"""
|
|
|
|
|
with default_main_program()._lr_schedule_guard():
|
|
|
|
|
global_step = _decay_step_counter(1)
|
|
|
|
|
|
|
|
|
|
a = global_step**-0.5
|
|
|
|
|
@ -108,6 +109,7 @@ def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
|
|
|
|
|
sgd_optimizer.minimize(avg_cost)
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
with default_main_program()._lr_schedule_guard():
|
|
|
|
|
global_step = _decay_step_counter()
|
|
|
|
|
|
|
|
|
|
div_res = global_step / decay_steps
|
|
|
|
|
@ -136,6 +138,7 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
|
|
|
|
|
Returns:
|
|
|
|
|
The decayed learning rate
|
|
|
|
|
"""
|
|
|
|
|
with default_main_program()._lr_schedule_guard():
|
|
|
|
|
global_step = _decay_step_counter()
|
|
|
|
|
|
|
|
|
|
div_res = global_step / decay_steps
|
|
|
|
|
@ -181,6 +184,7 @@ def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
|
|
|
|
|
staircase=True))
|
|
|
|
|
sgd_optimizer.minimize(avg_cost)
|
|
|
|
|
"""
|
|
|
|
|
with default_main_program()._lr_schedule_guard():
|
|
|
|
|
global_step = _decay_step_counter()
|
|
|
|
|
|
|
|
|
|
div_res = global_step / decay_steps
|
|
|
|
|
@ -220,12 +224,15 @@ def polynomial_decay(learning_rate,
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: The decayed learning rate
|
|
|
|
|
"""
|
|
|
|
|
with default_main_program()._lr_schedule_guard():
|
|
|
|
|
global_step = _decay_step_counter()
|
|
|
|
|
|
|
|
|
|
if cycle:
|
|
|
|
|
div_res = ops.ceil(global_step / decay_steps)
|
|
|
|
|
zero_var = tensor.fill_constant(shape=[1], dtype='float32', value=0.0)
|
|
|
|
|
one_var = tensor.fill_constant(shape=[1], dtype='float32', value=1.0)
|
|
|
|
|
zero_var = tensor.fill_constant(
|
|
|
|
|
shape=[1], dtype='float32', value=0.0)
|
|
|
|
|
one_var = tensor.fill_constant(
|
|
|
|
|
shape=[1], dtype='float32', value=1.0)
|
|
|
|
|
|
|
|
|
|
with control_flow.Switch() as switch:
|
|
|
|
|
with switch.case(global_step == zero_var):
|
|
|
|
|
@ -266,7 +273,7 @@ def piecewise_decay(boundaries, values):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
with default_main_program()._lr_schedule_guard():
|
|
|
|
|
if len(values) - len(boundaries) != 1:
|
|
|
|
|
raise ValueError("len(values) - len(boundaries) should be 1")
|
|
|
|
|
|
|
|
|
|
@ -291,7 +298,9 @@ def piecewise_decay(boundaries, values):
|
|
|
|
|
with switch.case(global_step < boundary_val):
|
|
|
|
|
tensor.assign(value_var, lr)
|
|
|
|
|
last_value_var = tensor.fill_constant(
|
|
|
|
|
shape=[1], dtype='float32', value=float(values[len(values) - 1]))
|
|
|
|
|
shape=[1],
|
|
|
|
|
dtype='float32',
|
|
|
|
|
value=float(values[len(values) - 1]))
|
|
|
|
|
with switch.default():
|
|
|
|
|
tensor.assign(last_value_var, lr)
|
|
|
|
|
|
|
|
|
|
|