|
| 1 | +import torch |
| 2 | +from torch.optim.optimizer import Optimizer |
| 3 | + |
| 4 | +from pytorch_optimizer.base.exception import NoSparseGradientError |
| 5 | +from pytorch_optimizer.base.optimizer import BaseOptimizer |
| 6 | +from pytorch_optimizer.base.types import BETAS, CLOSURE, DEFAULTS, LOSS, PARAMETERS |
| 7 | + |
| 8 | + |
| 9 | +class Lion(Optimizer, BaseOptimizer): |
| 10 | + r"""Symbolic Discovery of Optimization Algorithms. |
| 11 | +
|
| 12 | + :param params: PARAMETERS. iterable of parameters to optimize or dicts defining parameter groups. |
| 13 | + :param lr: float. learning rate. |
| 14 | + :param betas: BETAS. coefficients used for computing running averages of gradient and the squared hessian trace. |
| 15 | + :param weight_decay: float. weight decay (L2 penalty). |
| 16 | + :param weight_decouple: bool. the optimizer uses decoupled weight decay as in AdamW. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + params: PARAMETERS, |
| 22 | + lr: float = 1e-4, |
| 23 | + betas: BETAS = (0.9, 0.99), |
| 24 | + weight_decay: float = 0.0, |
| 25 | + weight_decouple: bool = True, |
| 26 | + ): |
| 27 | + self.lr = lr |
| 28 | + self.betas = betas |
| 29 | + self.weight_decay = weight_decay |
| 30 | + self.weight_decouple = weight_decouple |
| 31 | + |
| 32 | + self.validate_parameters() |
| 33 | + |
| 34 | + defaults: DEFAULTS = { |
| 35 | + 'lr': lr, |
| 36 | + 'betas': betas, |
| 37 | + 'weight_decay': weight_decay, |
| 38 | + } |
| 39 | + super().__init__(params, defaults) |
| 40 | + |
| 41 | + def validate_parameters(self): |
| 42 | + self.validate_learning_rate(self.lr) |
| 43 | + self.validate_betas(self.betas) |
| 44 | + self.validate_weight_decay(self.weight_decay) |
| 45 | + |
| 46 | + @property |
| 47 | + def __str__(self) -> str: |
| 48 | + return 'Lion' |
| 49 | + |
| 50 | + @torch.no_grad() |
| 51 | + def reset(self): |
| 52 | + for group in self.param_groups: |
| 53 | + for p in group['params']: |
| 54 | + state = self.state[p] |
| 55 | + |
| 56 | + state['exp_avg'] = torch.zeros_like(p) |
| 57 | + |
| 58 | + @torch.no_grad() |
| 59 | + def step(self, closure: CLOSURE = None) -> LOSS: |
| 60 | + loss: LOSS = None |
| 61 | + if closure is not None: |
| 62 | + with torch.enable_grad(): |
| 63 | + loss = closure() |
| 64 | + |
| 65 | + for group in self.param_groups: |
| 66 | + beta1, beta2 = group['betas'] |
| 67 | + weight_decay = group['weight_decay'] |
| 68 | + for p in group['params']: |
| 69 | + if p.grad is None: |
| 70 | + continue |
| 71 | + |
| 72 | + grad = p.grad |
| 73 | + if grad.is_sparse: |
| 74 | + raise NoSparseGradientError(self.__str__) |
| 75 | + |
| 76 | + state = self.state[p] |
| 77 | + |
| 78 | + if len(state) == 0: |
| 79 | + state['exp_avg'] = torch.zeros_like(p) |
| 80 | + |
| 81 | + update = exp_avg = state['exp_avg'] |
| 82 | + |
| 83 | + if weight_decay > 0.0: |
| 84 | + if self.weight_decouple: |
| 85 | + p.mul_(1.0 - group['lr'] * weight_decay) |
| 86 | + else: |
| 87 | + grad.add_(p, alpha=weight_decay) |
| 88 | + |
| 89 | + update.mul_(beta1).add_(grad, alpha=1.0 - beta1) |
| 90 | + exp_avg.mul_(beta2).add_(grad, alpha=1.0 - beta2) |
| 91 | + |
| 92 | + p.add_(update.sign(), alpha=-group['lr']) |
| 93 | + |
| 94 | + return loss |
0 commit comments