余弦退火warmup_scheduler安装

 

pytorch-warmup · PyPI

 

pip install -i https://pypi.org/simple/ warmup-scheduler-pytorch
pip install -i https://pypi.org/simple/ warmup-scheduler

在paddle框架中实现下面的所有代码:class CosineAnnealingWarmbootingLR: # cawb learning rate scheduler: given the warm booting steps, calculate the learning rate automatically def __init__(self, optimizer, epochs=0, eta_min=0.05, steps=[], step_scale=0.8, lf=None, batchs=0, warmup_epoch=0, epoch_scale=1.0): self.warmup_iters = batchs * warmup_epoch self.optimizer = optimizer self.eta_min = eta_min self.iters = -1 self.iters_batch = -1 self.base_lr = [group['lr'] for group in optimizer.param_groups] self.step_scale = step_scale steps.sort() self.steps = [warmup_epoch] + [i for i in steps if (i < epochs and i > warmup_epoch)] + [epochs] self.gap = 0 self.last_epoch = 0 self.lf = lf self.epoch_scale = epoch_scale # Initialize epochs and base learning rates for group in optimizer.param_groups: group.setdefault('initial_lr', group['lr']) def step(self, external_iter = None): self.iters += 1 if external_iter is not None: self.iters = external_iter # cos warm boot policy iters = self.iters + self.last_epoch scale = 1.0 for i in range(len(self.steps)-1): if (iters <= self.steps[i+1]): self.gap = self.steps[i+1] - self.steps[i] iters = iters - self.steps[i] if i != len(self.steps)-2: self.gap += self.epoch_scale break scale *= self.step_scale if self.lf is None: for group, lr in zip(self.optimizer.param_groups, self.base_lr): group['lr'] = scale * lr * ((((1 + math.cos(iters * math.pi / self.gap)) / 2) ** 1.0) * (1.0 - self.eta_min) + self.eta_min) else: for group, lr in zip(self.optimizer.param_groups, self.base_lr): group['lr'] = scale * lr * self.lf(iters, self.gap) return self.optimizer.param_groups[0]['lr'] def step_batch(self): self.iters_batch += 1 if self.iters_batch < self.warmup_iters: rate = self.iters_batch / self.warmup_iters for group, lr in zip(self.optimizer.param_groups, self.base_lr): group['lr'] = lr * rate return self.optimizer.param_groups[0]['lr'] else: return None
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值