提交 b3d3f7ce 编写于 作者: Z Zhang Ting 提交者: wangguanzhong

remove init_on_cpu because it does not take effect, test=develop (#18)

上级 5447bcf7
...@@ -27,7 +27,6 @@ import paddle.fluid.layers.ops as ops ...@@ -27,7 +27,6 @@ import paddle.fluid.layers.ops as ops
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter
import math import math
from paddle.fluid.initializer import init_on_cpu
from models import inception from models import inception
from absl import flags from absl import flags
...@@ -66,8 +65,7 @@ class CIFARModel(object): ...@@ -66,8 +65,7 @@ class CIFARModel(object):
Applies cosine decay to the learning rate. Applies cosine decay to the learning rate.
""" """
global_step = _decay_step_counter() global_step = _decay_step_counter()
with init_on_cpu(): frac = (1 + ops.cos(global_step / max_step * math.pi)) / 2
frac = (1 + ops.cos(global_step / max_step * math.pi)) / 2
return FLAGS.lr_min + (FLAGS.lr_max - FLAGS.lr_min) * frac return FLAGS.lr_min + (FLAGS.lr_max - FLAGS.lr_min) * frac
self.lr_strategy = cosine_decay self.lr_strategy = cosine_decay
......
...@@ -27,7 +27,6 @@ import paddle.fluid as fluid ...@@ -27,7 +27,6 @@ import paddle.fluid as fluid
import paddle.fluid.layers.ops as ops import paddle.fluid.layers.ops as ops
from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter
import math import math
from paddle.fluid.initializer import init_on_cpu
def cosine_decay(learning_rate, num_epoch, steps_one_epoch): def cosine_decay(learning_rate, num_epoch, steps_one_epoch):
...@@ -36,10 +35,9 @@ def cosine_decay(learning_rate, num_epoch, steps_one_epoch): ...@@ -36,10 +35,9 @@ def cosine_decay(learning_rate, num_epoch, steps_one_epoch):
""" """
global_step = _decay_step_counter() global_step = _decay_step_counter()
with init_on_cpu(): decayed_lr = learning_rate * \
decayed_lr = learning_rate * \ (ops.cos(fluid.layers.floor(global_step / steps_one_epoch) \
(ops.cos(fluid.layers.floor(global_step / steps_one_epoch) \ * math.pi / num_epoch) + 1)/2
* math.pi / num_epoch) + 1)/2
return decayed_lr return decayed_lr
......
...@@ -69,7 +69,6 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120): ...@@ -69,7 +69,6 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120):
decrease lr for every mini-batch and start with warmup. decrease lr for every mini-batch and start with warmup.
""" """
from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter
from paddle.fluid.initializer import init_on_cpu
global_step = _decay_step_counter() global_step = _decay_step_counter()
lr = fluid.layers.tensor.create_global_var( lr = fluid.layers.tensor.create_global_var(
shape=[1], shape=[1],
...@@ -81,17 +80,16 @@ def cosine_decay_with_warmup(learning_rate, step_each_epoch, epochs=120): ...@@ -81,17 +80,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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册