未验证 提交 04b036cd 编写于 作者: Z Zhang Ting 提交者: GitHub

remove init_on_cpu, test=develop (#4444)

上级 50cf1d81
...@@ -20,7 +20,6 @@ import math ...@@ -20,7 +20,6 @@ import math
import paddle.fluid as fluid import paddle.fluid as fluid
import paddle.fluid.layers.ops as ops import paddle.fluid.layers.ops as ops
from paddle.fluid.initializer import init_on_cpu
from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter
...@@ -30,10 +29,9 @@ def cosine_decay(learning_rate, step_each_epoch, epochs=120): ...@@ -30,10 +29,9 @@ def cosine_decay(learning_rate, step_each_epoch, epochs=120):
""" """
global_step = _decay_step_counter() global_step = _decay_step_counter()
with init_on_cpu(): epoch = ops.floor(global_step / step_each_epoch)
epoch = ops.floor(global_step / step_each_epoch) decayed_lr = learning_rate * \
decayed_lr = learning_rate * \ (ops.cos(epoch * (math.pi / epochs)) + 1)/2
(ops.cos(epoch * (math.pi / epochs)) + 1)/2
return decayed_lr return decayed_lr
...@@ -53,17 +51,16 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120): ...@@ -53,17 +51,16 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120):
warmup_epoch = fluid.layers.fill_constant( warmup_epoch = fluid.layers.fill_constant(
shape=[1], dtype='float32', value=float(5), force_cpu=True) shape=[1], dtype='float32', value=float(5), force_cpu=True)
with init_on_cpu(): epoch = ops.floor(global_step / step_each_epoch)
epoch = ops.floor(global_step / step_each_epoch) with fluid.layers.control_flow.Switch() as switch:
with fluid.layers.control_flow.Switch() as switch: with switch.case(epoch < warmup_epoch):
with switch.case(epoch < warmup_epoch): decayed_lr = learning_rate * (global_step /
decayed_lr = learning_rate * (global_step / (step_each_epoch * warmup_epoch))
(step_each_epoch * warmup_epoch)) fluid.layers.tensor.assign(input=decayed_lr, output=lr)
fluid.layers.tensor.assign(input=decayed_lr, output=lr) with switch.default():
with switch.default(): decayed_lr = learning_rate * \
decayed_lr = learning_rate * \ (ops.cos((global_step - warmup_epoch * step_each_epoch) * (math.pi / (epochs * step_each_epoch))) + 1)/2
(ops.cos((global_step - warmup_epoch * step_each_epoch) * (math.pi / (epochs * step_each_epoch))) + 1)/2 fluid.layers.tensor.assign(input=decayed_lr, output=lr)
fluid.layers.tensor.assign(input=decayed_lr, output=lr)
return lr return lr
...@@ -85,19 +82,18 @@ def exponential_decay_with_warmup(learning_rate, ...@@ -85,19 +82,18 @@ def exponential_decay_with_warmup(learning_rate,
warmup_epoch = fluid.layers.fill_constant( warmup_epoch = fluid.layers.fill_constant(
shape=[1], dtype='float32', value=float(warm_up_epoch), force_cpu=True) shape=[1], dtype='float32', value=float(warm_up_epoch), force_cpu=True)
with init_on_cpu(): epoch = ops.floor(global_step / step_each_epoch)
epoch = ops.floor(global_step / step_each_epoch) with fluid.layers.control_flow.Switch() as switch:
with fluid.layers.control_flow.Switch() as switch: with switch.case(epoch < warmup_epoch):
with switch.case(epoch < warmup_epoch): decayed_lr = learning_rate * (global_step /
decayed_lr = learning_rate * (global_step / (step_each_epoch * warmup_epoch))
(step_each_epoch * warmup_epoch)) fluid.layers.assign(input=decayed_lr, output=lr)
fluid.layers.assign(input=decayed_lr, output=lr) with switch.default():
with switch.default(): div_res = (
div_res = ( global_step - warmup_epoch * step_each_epoch) / decay_epochs
global_step - warmup_epoch * step_each_epoch) / decay_epochs div_res = ops.floor(div_res)
div_res = ops.floor(div_res) decayed_lr = learning_rate * (decay_rate**div_res)
decayed_lr = learning_rate * (decay_rate**div_res) fluid.layers.assign(input=decayed_lr, output=lr)
fluid.layers.assign(input=decayed_lr, output=lr)
return lr return lr
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册