PiecewiseDecay¶
- class paddle.optimizer.lr. PiecewiseDecay ( boundaries, values, last_epoch=- 1, verbose=False ) [源代码] ¶
该接口提供分段设置学习率的策略。boundaries 表示学习率变化的边界步数,对应 epoch 的值,values 表示学习率变化的值。
过程可以描述如下:
boundaries = [100, 200] # epoch 仅代表当前步数,无实义
values = [1.0, 0.5, 0.1] # 在第[0,100), [100,200), [200,+∞)分别对应 value 中学习率的值
learning_rate = 1.0 if epoch < 100
learning_rate = 0.5 if 100 <= epoch < 200
learning_rate = 0.1 if 200 <= epoch
...
参数¶
boundaries (list) - 指定学习率的边界值列表。列表的数据元素为 Python int 类型。
values (list) - 学习率列表。数据元素类型为 Python float 的列表。与边界值列表有对应的关系。
last_epoch (int,可选) - 上一轮的轮数,重启训练时设置为上一轮的 epoch 数。默认值为 -1,则为初始学习率。
verbose (bool,可选) - 如果是
True
,则在每一轮更新时在标准输出 stdout 输出一条信息。默认值为False
。
返回¶
用于调整学习率的 PiecewiseDecay
实例对象。
代码示例¶
import paddle
import numpy as np
# train on default dynamic graph mode
linear = paddle.nn.Linear(10, 10)
scheduler = paddle.optimizer.lr.PiecewiseDecay(boundaries=[3, 6, 9], values=[0.1, 0.2, 0.3, 0.4], verbose=True)
sgd = paddle.optimizer.SGD(learning_rate=scheduler, parameters=linear.parameters())
for epoch in range(20):
for batch_id in range(5):
x = paddle.uniform([10, 10])
out = linear(x)
loss = paddle.mean(out)
loss.backward()
sgd.step()
sgd.clear_gradients()
scheduler.step() # If you update learning rate each step
# scheduler.step() # If you update learning rate each epoch
# train on static graph mode
paddle.enable_static()
main_prog = paddle.static.Program()
start_prog = paddle.static.Program()
with paddle.static.program_guard(main_prog, start_prog):
x = paddle.static.data(name='x', shape=[None, 4, 5])
y = paddle.static.data(name='y', shape=[None, 4, 5])
z = paddle.static.nn.fc(x, 100)
loss = paddle.mean(z)
scheduler = paddle.optimizer.lr.PiecewiseDecay(boundaries=[3, 6, 9], values=[0.1, 0.2, 0.3, 0.4], verbose=True)
sgd = paddle.optimizer.SGD(learning_rate=scheduler)
sgd.minimize(loss)
exe = paddle.static.Executor()
exe.run(start_prog)
for epoch in range(20):
for batch_id in range(5):
out = exe.run(
main_prog,
feed={
'x': np.random.randn(3, 4, 5).astype('float32'),
'y': np.random.randn(3, 4, 5).astype('float32')
},
fetch_list=loss.name)
scheduler.step() # If you update learning rate each step
# scheduler.step() # If you update learning rate each epoch