13
13
# limitations under the License.
14
14
15
15
import layers
16
- from framework import Variable
17
16
from initializer import init_on_cpu
18
17
19
18
__all__ = [
30
29
"""
31
30
32
31
33
- def exponential_decay (learning_rate ,
34
- global_step ,
35
- decay_steps ,
36
- decay_rate ,
37
- staircase = False ):
32
+ def _decay_step_counter ():
33
+ # the first global step is zero in learning rate decay
34
+ global_step = layers .autoincreased_step_counter (
35
+ counter_name = '@LR_DECAY_COUNTER@' , begin = 0 , step = 1 )
36
+ global_step = layers .cast (global_step , 'float32' )
37
+ return global_step
38
+
39
+
40
+ def exponential_decay (learning_rate , decay_steps , decay_rate , staircase = False ):
38
41
"""Applies exponential decay to the learning rate.
39
42
40
43
```python
@@ -44,16 +47,14 @@ def exponential_decay(learning_rate,
44
47
Args:
45
48
learning_rate: A scalar float32 value or a Variable. This
46
49
will be the initial learning rate during training
47
- global_step: A Variable that record the training step.
48
50
decay_steps: A Python `int32` number.
49
51
decay_rate: A Python `float` number.
50
52
staircase: Boolean. If set true, decay the learning rate every decay_steps.
51
53
52
54
Returns:
53
55
The decayed learning rate
54
56
"""
55
- if not isinstance (global_step , Variable ):
56
- raise ValueError ("global_step is required for exponential_decay." )
57
+ global_step = _decay_step_counter ()
57
58
58
59
with init_on_cpu ():
59
60
# update learning_rate
@@ -65,32 +66,25 @@ def exponential_decay(learning_rate,
65
66
return decayed_lr
66
67
67
68
68
- def natural_exp_decay (learning_rate ,
69
- global_step ,
70
- decay_steps ,
71
- decay_rate ,
72
- staircase = False ):
69
+ def natural_exp_decay (learning_rate , decay_steps , decay_rate , staircase = False ):
73
70
"""Applies natural exponential decay to the initial learning rate.
74
71
75
- ```python
76
- if not staircase:
77
- decayed_learning_rate = learning_rate * exp(- decay_rate * (global_step / decay_steps))
78
- else:
79
- decayed_learning_rate = learning_rate * exp(- decay_rate * (global_step / decay_steps))
80
- ```
72
+ >>> if not staircase:
73
+ >>> decayed_learning_rate = learning_rate * exp(- decay_rate * (global_step / decay_steps))
74
+ >>> else:
75
+ >>> decayed_learning_rate = learning_rate * exp(- decay_rate * (global_step / decay_steps))
76
+
81
77
Args:
82
78
learning_rate: A scalar float32 value or a Variable. This
83
79
will be the initial learning rate during training
84
- global_step: A Variable that record the training step.
85
80
decay_steps: A Python `int32` number.
86
81
decay_rate: A Python `float` number.
87
82
staircase: Boolean. If set true, decay the learning rate every decay_steps.
88
83
89
84
Returns:
90
85
The decayed learning rate
91
86
"""
92
- if not isinstance (global_step , Variable ):
93
- raise ValueError ("global_step is required for natural_exp_decay." )
87
+ global_step = _decay_step_counter ()
94
88
95
89
with init_on_cpu ():
96
90
div_res = global_step / decay_steps
@@ -101,32 +95,25 @@ def natural_exp_decay(learning_rate,
101
95
return decayed_lr
102
96
103
97
104
- def inverse_time_decay (learning_rate ,
105
- global_step ,
106
- decay_steps ,
107
- decay_rate ,
108
- staircase = False ):
98
+ def inverse_time_decay (learning_rate , decay_steps , decay_rate , staircase = False ):
109
99
"""Applies inverse time decay to the initial learning rate.
110
100
111
- ```python
112
- if staircase:
113
- decayed_learning_rate = learning_rate / (1 + decay_rate * floor(global_step / decay_step))
114
- else:
115
- decayed_learning_rate = learning_rate / (1 + decay_rate * global_step / decay_step)
116
- ```
101
+ >>> if staircase:
102
+ >>> decayed_learning_rate = learning_rate / (1 + decay_rate * floor(global_step / decay_step))
103
+ >>> else:
104
+ >>> decayed_learning_rate = learning_rate / (1 + decay_rate * global_step / decay_step)
105
+
117
106
Args:
118
107
learning_rate: A scalar float32 value or a Variable. This
119
- will be the initial learning rate during training
120
- global_step: A Variable that record the training step.
108
+ will be the initial learning rate during training.
121
109
decay_steps: A Python `int32` number.
122
110
decay_rate: A Python `float` number.
123
111
staircase: Boolean. If set true, decay the learning rate every decay_steps.
124
112
125
113
Returns:
126
114
The decayed learning rate
127
115
"""
128
- if not isinstance (global_step , Variable ):
129
- raise ValueError ("global_step is required for inverse_time_decay." )
116
+ global_step = _decay_step_counter ()
130
117
131
118
with init_on_cpu ():
132
119
div_res = global_step / decay_steps
@@ -139,26 +126,22 @@ def inverse_time_decay(learning_rate,
139
126
140
127
141
128
def polynomial_decay (learning_rate ,
142
- global_step ,
143
129
decay_steps ,
144
130
end_learning_rate = 0.0001 ,
145
131
power = 1.0 ,
146
132
cycle = False ):
147
133
"""Applies polynomial decay to the initial learning rate.
148
134
149
- ```python
150
- if cycle:
151
- decay_steps = decay_steps * ceil(global_step / decay_steps)
152
- else:
153
- global_step = min(global_step, decay_steps)
154
- decayed_learning_rate = (learning_rate - end_learning_rate) *
155
- (1 - global_step / decay_steps) ^ power +
156
- end_learning_rate
157
- ```
135
+ >>> if cycle:
136
+ >>> decay_steps = decay_steps * ceil(global_step / decay_steps)
137
+ >>> else:
138
+ >>> global_step = min(global_step, decay_steps)
139
+ >>> decayed_learning_rate = (learning_rate - end_learning_rate) *
140
+ >>> (1 - global_step / decay_steps) ^ power +
141
+ >>> end_learning_rate
158
142
Args:
159
143
learning_rate: A scalar float32 value or a Variable. This
160
144
will be the initial learning rate during training
161
- global_step: A Variable that record the training step.
162
145
decay_steps: A Python `int32` number.
163
146
end_learning_rate: A Python `float` number.
164
147
power: A Python `float` number
@@ -167,8 +150,7 @@ def polynomial_decay(learning_rate,
167
150
Returns:
168
151
The decayed learning rate
169
152
"""
170
- if not isinstance (global_step , Variable ):
171
- raise ValueError ("global_step is required for inverse_time_decay." )
153
+ global_step = _decay_step_counter ()
172
154
173
155
with init_on_cpu ():
174
156
if cycle :
@@ -193,27 +175,24 @@ def polynomial_decay(learning_rate,
193
175
return decayed_lr
194
176
195
177
196
- def piecewise_decay (global_step , boundaries , values ):
178
+ def piecewise_decay (boundaries , values ):
197
179
"""Applies piecewise decay to the initial learning rate.
198
180
199
- ```python
200
- boundaries = [10000, 20000]
201
- values = [1.0, 0.5, 0.1]
202
-
203
- if step < 10000:
204
- learning_rate = 1.0
205
- elif step >= 10000 and step < 20000:
206
- learning_rate = 0.5
207
- else:
208
- learning_rate = 0.1
209
- ```
181
+ >>> boundaries = [10000, 20000]
182
+ >>> values = [1.0, 0.5, 0.1]
183
+ >>>
184
+ >>> if step < 10000:
185
+ >>> learning_rate = 1.0
186
+ >>> elif 10000 <= step < 20000:
187
+ >>> learning_rate = 0.5
188
+ >>> else:
189
+ >>> learning_rate = 0.1
210
190
"""
211
191
212
192
if len (values ) - len (boundaries ) != 1 :
213
193
raise ValueError ("len(values) - len(boundaries) should be 1" )
214
194
215
- if not isinstance (global_step , Variable ):
216
- raise ValueError ("global_step is required for piecewise_decay." )
195
+ global_step = _decay_step_counter ()
217
196
218
197
with init_on_cpu ():
219
198
lr = layers .create_global_var (
0 commit comments