Skip to content

Commit 977764f

Browse files
committed
Fix the other lr_decay
1 parent 381baca commit 977764f

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

python/paddle/fluid/layers/learning_rate_scheduler.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def noam_decay(d_model, warmup_steps):
6262
The decayed learning rate.
6363
"""
6464
global_step = _decay_step_counter(1)
65-
with init_on_cpu():
66-
a = global_step**-0.5
67-
b = (warmup_steps**-1.5) * global_step
68-
lr_value = (d_model**-0.5) * ops.elementwise_min(a, b)
65+
66+
a = global_step**-0.5
67+
b = (warmup_steps**-1.5) * global_step
68+
lr_value = (d_model**-0.5) * ops.elementwise_min(a, b)
6969

7070
return lr_value
7171

@@ -108,12 +108,10 @@ def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
108108
"""
109109
global_step = _decay_step_counter()
110110

111-
with init_on_cpu():
112-
# update learning_rate
113-
div_res = global_step / decay_steps
114-
if staircase:
115-
div_res = ops.floor(div_res)
116-
decayed_lr = learning_rate * (decay_rate**div_res)
111+
div_res = global_step / decay_steps
112+
if staircase:
113+
div_res = ops.floor(div_res)
114+
decayed_lr = learning_rate * (decay_rate**div_res)
117115

118116
return decayed_lr
119117

@@ -138,11 +136,10 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
138136
"""
139137
global_step = _decay_step_counter()
140138

141-
with init_on_cpu():
142-
div_res = global_step / decay_steps
143-
if staircase:
144-
div_res = ops.floor(div_res)
145-
decayed_lr = learning_rate * ops.exp(-1 * decay_rate * div_res)
139+
div_res = global_step / decay_steps
140+
if staircase:
141+
div_res = ops.floor(div_res)
142+
decayed_lr = learning_rate * ops.exp(-1 * decay_rate * div_res)
146143

147144
return decayed_lr
148145

@@ -184,12 +181,11 @@ def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
184181
"""
185182
global_step = _decay_step_counter()
186183

187-
with init_on_cpu():
188-
div_res = global_step / decay_steps
189-
if staircase:
190-
div_res = ops.floor(div_res)
184+
div_res = global_step / decay_steps
185+
if staircase:
186+
div_res = ops.floor(div_res)
191187

192-
decayed_lr = learning_rate / (1 + decay_rate * div_res)
188+
decayed_lr = learning_rate / (1 + decay_rate * div_res)
193189

194190
return decayed_lr
195191

@@ -224,25 +220,22 @@ def polynomial_decay(learning_rate,
224220
"""
225221
global_step = _decay_step_counter()
226222

227-
with init_on_cpu():
228-
if cycle:
229-
div_res = ops.ceil(global_step / decay_steps)
230-
zero_var = tensor.fill_constant(
231-
shape=[1], dtype='float32', value=0.0)
232-
one_var = tensor.fill_constant(
233-
shape=[1], dtype='float32', value=1.0)
234-
235-
with control_flow.Switch() as switch:
236-
with switch.case(global_step == zero_var):
237-
tensor.assign(input=one_var, output=div_res)
238-
decay_steps = decay_steps * div_res
239-
else:
240-
decay_steps_var = tensor.fill_constant(
241-
shape=[1], dtype='float32', value=float(decay_steps))
242-
global_step = ops.elementwise_min(x=global_step, y=decay_steps_var)
243-
244-
decayed_lr = (learning_rate - end_learning_rate) * \
245-
((1 - global_step / decay_steps) ** power) + end_learning_rate
223+
if cycle:
224+
div_res = ops.ceil(global_step / decay_steps)
225+
zero_var = tensor.fill_constant(shape=[1], dtype='float32', value=0.0)
226+
one_var = tensor.fill_constant(shape=[1], dtype='float32', value=1.0)
227+
228+
with control_flow.Switch() as switch:
229+
with switch.case(global_step == zero_var):
230+
tensor.assign(input=one_var, output=div_res)
231+
decay_steps = decay_steps * div_res
232+
else:
233+
decay_steps_var = tensor.fill_constant(
234+
shape=[1], dtype='float32', value=float(decay_steps))
235+
global_step = ops.elementwise_min(x=global_step, y=decay_steps_var)
236+
237+
decayed_lr = (learning_rate - end_learning_rate) * \
238+
((1 - global_step / decay_steps) ** power) + end_learning_rate
246239
return decayed_lr
247240

248241

0 commit comments

Comments
 (0)