pytorch实现设置不定长的epoch点集降低学习率

本文介绍了一种使用Python队列结构实现自定义学习率衰减策略的方法,该策略允许在预设的epoch点手动调整学习率,适用于深度学习模型训练过程中的精细化调参。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前一直想过在调参时实现一下,比如训练300个epoch,在100个epoch降低学习率,然后在150个epoch又去降低学习率,现在实现的代码大多数是设定epoch的every数,就是说每隔多少个epoch,就将学习率降低0.1倍,我现在想借助于python中的队列结构实现,可以设定任意个降低学习率的epoch点数,对学习率进行decay操作。

from collections import deque
queue = deque([1,4,6])
lr=1e-1
for epoch in range(10):
    if len(queue)>=1:
        if epoch==queue[0]:
            lr*=0.1
            queue.popleft()
    '''training one epoch'''
    print(epoch,lr)
'''
0 0.1
1 0.010000000000000002
2 0.010000000000000002
3 0.010000000000000002
4 0.0010000000000000002
5 0.0010000000000000002
6 0.00010000000000000003
7 0.00010000000000000003
8 0.00010000000000000003
9 0.00010000000000000003
'''
    

for epoch in range(args.start_epoch, args.max_epochs + 1):
    # setting to train mode
    fasterRCNN.train()
    loss_temp = 0
    start = time.time()
    
    if len(decay_point)>=1:
      if epoch==decay_point[0]:
          adjust_learning_rate(optimizer, args.lr_decay_gamma)
          lr *= args.lr_decay_gamma  
          decay_point.popleft()

    #if epoch % (args.lr_decay_step + 1) == 0:   

    data_iter = iter(dataloader)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值