12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
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
17
20
18
21
__all__ = [
19
22
'exponential_decay' , 'natural_exp_decay' , 'inverse_time_decay' ,
31
34
32
35
def _decay_step_counter ():
33
36
# the first global step is zero in learning rate decay
34
- global_step = layers .autoincreased_step_counter (
37
+ global_step = nn .autoincreased_step_counter (
35
38
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' )
37
40
return global_step
38
41
39
42
@@ -60,7 +63,7 @@ def exponential_decay(learning_rate, decay_steps, decay_rate, staircase=False):
60
63
# update learning_rate
61
64
div_res = global_step / decay_steps
62
65
if staircase :
63
- div_res = layers .floor (x = div_res )
66
+ div_res = ops .floor (div_res )
64
67
decayed_lr = learning_rate * (decay_rate ** div_res )
65
68
66
69
return decayed_lr
@@ -89,8 +92,8 @@ def natural_exp_decay(learning_rate, decay_steps, decay_rate, staircase=False):
89
92
with init_on_cpu ():
90
93
div_res = global_step / decay_steps
91
94
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 )
94
97
95
98
return decayed_lr
96
99
@@ -118,7 +121,7 @@ def inverse_time_decay(learning_rate, decay_steps, decay_rate, staircase=False):
118
121
with init_on_cpu ():
119
122
div_res = global_step / decay_steps
120
123
if staircase :
121
- div_res = layers .floor (x = div_res )
124
+ div_res = ops .floor (div_res )
122
125
123
126
decayed_lr = learning_rate / (1 + decay_rate * div_res )
124
127
@@ -154,21 +157,20 @@ def polynomial_decay(learning_rate,
154
157
155
158
with init_on_cpu ():
156
159
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 (
159
162
shape = [1 ], dtype = 'float32' , value = 0.0 )
160
- one_var = layers .fill_constant (
163
+ one_var = tensor .fill_constant (
161
164
shape = [1 ], dtype = 'float32' , value = 1.0 )
162
165
163
- with layers .Switch () as switch :
166
+ with control_flow .Switch () as switch :
164
167
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 )
166
169
decay_steps = decay_steps * div_res
167
170
else :
168
- decay_steps_var = layers .fill_constant (
171
+ decay_steps_var = tensor .fill_constant (
169
172
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 )
172
174
173
175
decayed_lr = (learning_rate - end_learning_rate ) * \
174
176
((1 - global_step / decay_steps ) ** power ) + end_learning_rate
@@ -195,26 +197,26 @@ def piecewise_decay(boundaries, values):
195
197
global_step = _decay_step_counter ()
196
198
197
199
with init_on_cpu ():
198
- lr = layers .create_global_var (
200
+ lr = tensor .create_global_var (
199
201
shape = [1 ],
200
202
value = 0.0 ,
201
203
dtype = 'float32' ,
202
204
persistable = True ,
203
205
name = "learning_rate" )
204
206
205
- with layers .Switch () as switch :
207
+ with control_flow .Switch () as switch :
206
208
for i in range (len (boundaries )):
207
- boundary_val = layers .fill_constant (
209
+ boundary_val = tensor .fill_constant (
208
210
shape = [1 ], dtype = 'float32' , value = float (boundaries [i ]))
209
- value_var = layers .fill_constant (
211
+ value_var = tensor .fill_constant (
210
212
shape = [1 ], dtype = 'float32' , value = float (values [i ]))
211
213
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 (
214
216
shape = [1 ],
215
217
dtype = 'float32' ,
216
218
value = float (values [len (values ) - 1 ]))
217
219
with switch .default ():
218
- layers .assign (last_value_var , lr )
220
+ tensor .assign (last_value_var , lr )
219
221
220
222
return lr
0 commit comments