1 回答
TA贡献1776条经验 获得超12个赞
您需要排除 numpy 调用并用张量流运算符替换 python 条件(“if”、“min”):
def make_cosine_anneal_lr(learning_rate, alpha, decay_steps):
def gen_lr(global_step):
#global_step = min(global_step, decay_steps)
global_step = tf.minimum(global_step, decay_steps)
cosine_decay = 0.5 * (1 + tf.cos(3.1415926 * global_step / decay_steps)) # changed np.pi to 3.14
decayed = (1 - alpha) * cosine_decay + alpha
decayed_learning_rate = learning_rate * decayed
return decayed_learning_rate
return gen_lr
def make_cosine_annealing_with_warmup(min_lr, max_lr, number_of_steps, alpha, decay_steps):
gen_lr_1 = make_linear_lr(min_lr, max_lr, number_of_steps)
gen_lr_2 = make_cosine_anneal_lr(max_lr, alpha, decay_steps)
def gen_lr(global_step):
#if global_step < number_of_steps:
# return gen_lr_1(global_step)
#else:
# return gen_lr_2(global_step - number_of_steps)
a = global_step < number_of_steps
a = tf.cast(a, tf.float32)
b = 1. - a
return a * gen_lr_1(global_step) + b * gen_lr_2(global_step - number_of_steps)
return gen_lr
这样的时间表适用于 Keras。
添加回答
举报