Skip to content

Commit f45a82b

Browse files
authored
change learning_rate_decay to learning_rate_scheduler (#8583)
* change learning_rate_decay to learning_rate_scheduler * optimize code * change nn.cast to tensor.cast
1 parent bc36396 commit f45a82b

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

python/paddle/fluid/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import layers
2727
import nets
2828
import optimizer
29-
import learning_rate_decay
3029
import backward
3130
import regularizer
3231
from param_attr import ParamAttr, WeightNormParamAttr

python/paddle/fluid/layers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from math_op_patch import *
2929
import detection
3030
from detection import *
31+
from learning_rate_scheduler import *
3132

3233
__all__ = []
3334
__all__ += math_op_patch.__all__
@@ -38,3 +39,4 @@
3839
__all__ += ops.__all__
3940
__all__ += device.__all__
4041
__all__ += detection.__all__
42+
__all__ += learning_rate_scheduler.__all__

python/paddle/fluid/learning_rate_decay.py renamed to python/paddle/fluid/layers/learning_rate_scheduler.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import layers
16-
from initializer import init_on_cpu
15+
import control_flow
16+
import nn
17+
import ops
18+
import tensor
19+
from ..initializer import init_on_cpu
1720

1821
__all__ = [
1922
'exponential_decay', 'natural_exp_decay', 'inverse_time_decay',
@@ -31,9 +34,9 @@
3134

3235
def _decay_step_counter():
3336
# the first global step is zero in learning rate decay
34-
global_step = layers.autoincreased_step_counter(
37+
global_step = nn.autoincreased_step_counter(
3538
counter_name='@LR_DECAY_COUNTER@', begin=0, step=1)
36-
global_step = layers.cast(global_step, 'float32')
39+
global_step = tensor.cast(global_step, 'float32')
3740
return global_step
3841

3942

@@ -60,7 +63,7 @@ def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
6063
# update learning_rate
6164
div_res = global_step / decay_steps
6265
if staircase:
63-
div_res = layers.floor(x=div_res)
66+
div_res = ops.floor(div_res)
6467
decayed_lr = learning_rate * (decay_rate**div_res)
6568

6669
return decayed_lr
@@ -89,8 +92,8 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
8992
with init_on_cpu():
9093
div_res = global_step / decay_steps
9194
if staircase:
92-
div_res = layers.floor(x=div_res)
93-
decayed_lr = learning_rate * layers.exp(x=(-1 * decay_rate * div_res))
95+
div_res = ops.floor(div_res)
96+
decayed_lr = learning_rate * ops.exp(-1 * decay_rate * div_res)
9497

9598
return decayed_lr
9699

@@ -118,7 +121,7 @@ def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
118121
with init_on_cpu():
119122
div_res = global_step / decay_steps
120123
if staircase:
121-
div_res = layers.floor(x=div_res)
124+
div_res = ops.floor(div_res)
122125

123126
decayed_lr = learning_rate / (1 + decay_rate * div_res)
124127

@@ -154,21 +157,20 @@ def polynomial_decay(learning_rate,
154157

155158
with init_on_cpu():
156159
if cycle:
157-
div_res = layers.ceil(x=(global_step / decay_steps))
158-
zero_var = layers.fill_constant(
160+
div_res = ops.ceil(global_step / decay_steps)
161+
zero_var = tensor.fill_constant(
159162
shape=[1], dtype='float32', value=0.0)
160-
one_var = layers.fill_constant(
163+
one_var = tensor.fill_constant(
161164
shape=[1], dtype='float32', value=1.0)
162165

163-
with layers.Switch() as switch:
166+
with control_flow.Switch() as switch:
164167
with switch.case(global_step == zero_var):
165-
layers.assign(input=one_var, output=div_res)
168+
tensor.assign(input=one_var, output=div_res)
166169
decay_steps = decay_steps * div_res
167170
else:
168-
decay_steps_var = layers.fill_constant(
171+
decay_steps_var = tensor.fill_constant(
169172
shape=[1], dtype='float32', value=float(decay_steps))
170-
global_step = layers.elementwise_min(
171-
x=global_step, y=decay_steps_var)
173+
global_step = ops.elementwise_min(x=global_step, y=decay_steps_var)
172174

173175
decayed_lr = (learning_rate - end_learning_rate) * \
174176
((1 - global_step / decay_steps) ** power) + end_learning_rate
@@ -195,26 +197,26 @@ def piecewise_decay(boundaries, values):
195197
global_step = _decay_step_counter()
196198

197199
with init_on_cpu():
198-
lr = layers.create_global_var(
200+
lr = tensor.create_global_var(
199201
shape=[1],
200202
value=0.0,
201203
dtype='float32',
202204
persistable=True,
203205
name="learning_rate")
204206

205-
with layers.Switch() as switch:
207+
with control_flow.Switch() as switch:
206208
for i in range(len(boundaries)):
207-
boundary_val = layers.fill_constant(
209+
boundary_val = tensor.fill_constant(
208210
shape=[1], dtype='float32', value=float(boundaries[i]))
209-
value_var = layers.fill_constant(
211+
value_var = tensor.fill_constant(
210212
shape=[1], dtype='float32', value=float(values[i]))
211213
with switch.case(global_step < boundary_val):
212-
layers.assign(value_var, lr)
213-
last_value_var = layers.fill_constant(
214+
tensor.assign(value_var, lr)
215+
last_value_var = tensor.fill_constant(
214216
shape=[1],
215217
dtype='float32',
216218
value=float(values[len(values) - 1]))
217219
with switch.default():
218-
layers.assign(last_value_var, lr)
220+
tensor.assign(last_value_var, lr)
219221

220222
return lr

python/paddle/fluid/tests/book/test_label_semantic_roles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def train(use_cuda, save_dirname=None, is_local=True):
170170
# TODO(qiao)
171171
# check other optimizers and check why out will be NAN
172172
sgd_optimizer = fluid.optimizer.SGD(
173-
learning_rate=fluid.learning_rate_decay.exponential_decay(
173+
learning_rate=fluid.layers.exponential_decay(
174174
learning_rate=0.0001,
175175
decay_steps=100000,
176176
decay_rate=0.5,

python/paddle/fluid/tests/unittests/test_learning_rate_decay.py renamed to python/paddle/fluid/tests/unittests/test_learning_rate_scheduler.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import unittest
1818

1919
import paddle.fluid as fluid
20+
import paddle.fluid.layers as layers
2021
import paddle.fluid.framework as framework
21-
import paddle.fluid.learning_rate_decay as lr_decay
2222

2323

2424
def exponential_decay(learning_rate,
@@ -111,27 +111,24 @@ def test_decay(self):
111111
common_kwargs_false["staircase"] = False
112112

113113
decay_fns = [
114-
(exponential_decay, lr_decay.exponential_decay, common_kwargs_true),
115-
(exponential_decay, lr_decay.exponential_decay,
114+
(exponential_decay, layers.exponential_decay, common_kwargs_true),
115+
(exponential_decay, layers.exponential_decay, common_kwargs_false),
116+
(natural_exp_decay, layers.natural_exp_decay, common_kwargs_true),
117+
(natural_exp_decay, layers.natural_exp_decay, common_kwargs_false),
118+
(inverse_time_decay, layers.inverse_time_decay, common_kwargs_true),
119+
(inverse_time_decay, layers.inverse_time_decay,
116120
common_kwargs_false),
117-
(natural_exp_decay, lr_decay.natural_exp_decay, common_kwargs_true),
118-
(natural_exp_decay, lr_decay.natural_exp_decay,
119-
common_kwargs_false),
120-
(inverse_time_decay, lr_decay.inverse_time_decay,
121-
common_kwargs_true),
122-
(inverse_time_decay, lr_decay.inverse_time_decay,
123-
common_kwargs_false),
124-
(polynomial_decay, lr_decay.polynomial_decay, {
121+
(polynomial_decay, layers.polynomial_decay, {
125122
"learning_rate": 1.0,
126123
"decay_steps": 5,
127124
"cycle": True
128125
}),
129-
(polynomial_decay, lr_decay.polynomial_decay, {
126+
(polynomial_decay, layers.polynomial_decay, {
130127
"learning_rate": 1.0,
131128
"decay_steps": 5,
132129
"cycle": False
133130
}),
134-
(piecewise_decay, lr_decay.piecewise_decay, {
131+
(piecewise_decay, layers.piecewise_decay, {
135132
"boundaries": [3, 6, 9],
136133
"values": [0.1, 0.2, 0.3, 0.4]
137134
}),

0 commit comments

Comments
 (0)