r"""Implements AdamW algorithm. Solve the bug of torch 1.8 The original Adam algorithm was proposed in `Adam: A Method for Stochastic Optimization`_. The AdamW variant was proposed in `Decoupled Weight Decay Regularization`_. Args: params (iterable): iterable of parameters to o
| 9 | |
| 10 | @OPTIMIZERS.register_module() |
| 11 | class AdamW2(Optimizer): |
| 12 | r"""Implements AdamW algorithm. Solve the bug of torch 1.8 |
| 13 | |
| 14 | The original Adam algorithm was proposed in `Adam: A Method for Stochastic Optimization`_. |
| 15 | The AdamW variant was proposed in `Decoupled Weight Decay Regularization`_. |
| 16 | |
| 17 | Args: |
| 18 | params (iterable): iterable of parameters to optimize or dicts defining |
| 19 | parameter groups |
| 20 | lr (float, optional): learning rate (default: 1e-3) |
| 21 | betas (Tuple[float, float], optional): coefficients used for computing |
| 22 | running averages of gradient and its square (default: (0.9, 0.999)) |
| 23 | eps (float, optional): term added to the denominator to improve |
| 24 | numerical stability (default: 1e-8) |
| 25 | weight_decay (float, optional): weight decay coefficient (default: 1e-2) |
| 26 | amsgrad (boolean, optional): whether to use the AMSGrad variant of this |
| 27 | algorithm from the paper `On the Convergence of Adam and Beyond`_ |
| 28 | (default: False) |
| 29 | |
| 30 | .. _Adam\: A Method for Stochastic Optimization: |
| 31 | https://arxiv.org/abs/1412.6980 |
| 32 | .. _Decoupled Weight Decay Regularization: |
| 33 | https://arxiv.org/abs/1711.05101 |
| 34 | .. _On the Convergence of Adam and Beyond: |
| 35 | https://openreview.net/forum?id=ryQu7f-RZ |
| 36 | """ |
| 37 | |
| 38 | def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, |
| 39 | weight_decay=1e-2, amsgrad=False): |
| 40 | if not 0.0 <= lr: |
| 41 | raise ValueError("Invalid learning rate: {}".format(lr)) |
| 42 | if not 0.0 <= eps: |
| 43 | raise ValueError("Invalid epsilon value: {}".format(eps)) |
| 44 | if not 0.0 <= betas[0] < 1.0: |
| 45 | raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) |
| 46 | if not 0.0 <= betas[1] < 1.0: |
| 47 | raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) |
| 48 | if not 0.0 <= weight_decay: |
| 49 | raise ValueError("Invalid weight_decay value: {}".format(weight_decay)) |
| 50 | defaults = dict(lr=lr, betas=betas, eps=eps, |
| 51 | weight_decay=weight_decay, amsgrad=amsgrad) |
| 52 | super(AdamW2, self).__init__(params, defaults) |
| 53 | |
| 54 | def __setstate__(self, state): |
| 55 | super(AdamW2, self).__setstate__(state) |
| 56 | for group in self.param_groups: |
| 57 | group.setdefault('amsgrad', False) |
| 58 | |
| 59 | @torch.no_grad() |
| 60 | def step(self, closure=None): |
| 61 | """Performs a single optimization step. |
| 62 | |
| 63 | Args: |
| 64 | closure (callable, optional): A closure that reevaluates the model |
| 65 | and returns the loss. |
| 66 | """ |
| 67 | loss = None |
| 68 | if closure is not None: |
nothing calls this directly
no outgoing calls
no test coverage detected