我想实现自定义学习率衰减。细节是:0.01作为initial_learning_rate,并且在50次迭代之后,lr(学习率)衰减到0.001并且在70之后衰减到0.0001。所以在我的代码中, self.lr_decay_iters 是一个列表[50,70]。我通过tf.py_func( def _custom_lr())实现了它。然后添加到优化器。这是我的相对片段:
def _custom_lr(self, lr):
if (len(self.lr_decay_iters) > 0 and
self.sess.run(self.global_step) >= self.lr_decay_iters[0]):
lr *= self.decay_factor
self.lr_decay_iters.pop(0)
import ipdb; ipdb.set_trace()
return (lr).astype(np.float32)
def get_opt(self):
name = self.OPT['name']
args = self.OPT['args'] if 'args' in self.OPT else {}
# custom learning rate strategy
lr = tf.Variable(self.initial_learning_rate, trainable=False,
name='learning_rate', dtype=tf.float32)
lr = tf.py_func(self._custom_lr, [lr], tf.float32)
tf.scalar_summary('learning_rate', lr)
self.learning_rate = lr
if not hasattr(tf.train, '%sOptimizer' % name):
raise ValueError('%s optimizer not support', name)
optimizer = getattr(tf.train, '%sOptimizer' % name)
return optimizer(lr, **args)
但结果是:
我的lr是否应用于优化器是否会衰减?
否则, self.global_step 是一个tf.Variable。