Skip to content

Commit 9db107d

Browse files
committed
Renamed and add comments
1 parent 2af9aac commit 9db107d

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

python/paddle/fluid/layers/nn.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
'softmax_with_cross_entropy',
7070
'smooth_l1',
7171
'one_hot',
72-
'global_step_counter',
72+
'autoincreased_step_counter',
7373
]
7474

7575

@@ -3253,23 +3253,32 @@ def one_hot(input, depth):
32533253
return one_hot_out
32543254

32553255

3256-
def global_step_counter():
3256+
def autoincreased_step_counter(counter_name=None, begin=1, step=1):
32573257
"""
3258+
NOTE: The counter will be automatically increased by 1 every mini-batch
32583259
Return the run counter of the main program, which is started with 1.
3260+
3261+
Args:
3262+
counter_name(str): The counter name, default is '@STEP_COUNTER@'.
3263+
begin(int): The first value of this counter.
3264+
step(int): The increment step between each execution.
3265+
32593266
Returns(Variable): The global run counter.
32603267
"""
32613268
helper = LayerHelper('global_step_counter')
3262-
counter_name = '@STEP_COUNTER@'
3269+
if counter_name is None:
3270+
counter_name = '@STEP_COUNTER@'
32633271
counter, is_new_var = helper.create_or_get_global_variable(
32643272
name=counter_name, dtype='int64', shape=[1], persistable=True)
32653273
if is_new_var:
32663274
helper.set_variable_initializer(
32673275
counter, initializer=Constant(
3268-
value=0, force_cpu=True))
3276+
value=begin - 1, force_cpu=True))
32693277
helper.main_program.global_block().prepend_op(
32703278
type='increment',
32713279
inputs={'X': [counter]},
3272-
outputs={'Out': [counter]})
3280+
outputs={'Out': [counter]},
3281+
attrs={'step': float(step)})
32733282
counter.stop_gradient = True
32743283

32753284
return counter

python/paddle/fluid/learning_rate_decay.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232

3333
def float_global_step():
3434
# the first global step is zero in learning rate decay
35-
global_step = layers.global_step_counter() - 1
35+
global_step = layers.autoincreased_step_counter(
36+
counter_name='@LR_DECAY_COUNTER@', begin=0, step=1)
3637
global_step = layers.cast(global_step, 'float32')
3738
return global_step
3839

python/paddle/fluid/tests/unittests/test_learning_rate_decay.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def check_decay(self, python_decay_fn, fluid_decay_fn, kwargs):
9393
step_val, lr_val = exe.run(
9494
fluid.default_main_program(),
9595
feed=[],
96-
fetch_list=[fluid.layers.global_step_counter(), decayed_lr])
96+
fetch_list=[
97+
fluid.layers.autoincreased_step_counter(), decayed_lr
98+
])
9799
python_decayed_lr = python_decay_fn(
98100
global_step=float(step), **kwargs)
99101
self.assertAlmostEqual(

0 commit comments

Comments
 (0)