Implements Adam algorithm with weight decay fix as introduced in [Decoupled Weight Decay Regularization](https://arxiv.org/abs/1711.05101). Parameters: params (`Iterable[nn.parameter.Parameter]`): Iterable of parameters to optimize or dictionaries defining parameter
| 556 | |
| 557 | |
| 558 | class AdamW(Optimizer): |
| 559 | """ |
| 560 | Implements Adam algorithm with weight decay fix as introduced in [Decoupled Weight Decay |
| 561 | Regularization](https://arxiv.org/abs/1711.05101). |
| 562 | |
| 563 | Parameters: |
| 564 | params (`Iterable[nn.parameter.Parameter]`): |
| 565 | Iterable of parameters to optimize or dictionaries defining parameter groups. |
| 566 | lr (`float`, *optional*, defaults to 0.001): |
| 567 | The learning rate to use. |
| 568 | betas (`Tuple[float,float]`, *optional*, defaults to `(0.9, 0.999)`): |
| 569 | Adam's betas parameters (b1, b2). |
| 570 | eps (`float`, *optional*, defaults to 1e-06): |
| 571 | Adam's epsilon for numerical stability. |
| 572 | weight_decay (`float`, *optional*, defaults to 0.0): |
| 573 | Decoupled weight decay to apply. |
| 574 | correct_bias (`bool`, *optional*, defaults to `True`): |
| 575 | Whether or not to correct bias in Adam (for instance, in Bert TF repository they use `False`). |
| 576 | no_deprecation_warning (`bool`, *optional*, defaults to `False`): |
| 577 | A flag used to disable the deprecation warning (set to `True` to disable the warning). |
| 578 | """ |
| 579 | |
| 580 | def __init__( |
| 581 | self, |
| 582 | params: Iterable[nn.parameter.Parameter], |
| 583 | lr: float = 1e-3, |
| 584 | betas: Tuple[float, float] = (0.9, 0.999), |
| 585 | eps: float = 1e-6, |
| 586 | weight_decay: float = 0.0, |
| 587 | correct_bias: bool = True, |
| 588 | no_deprecation_warning: bool = False, |
| 589 | ): |
| 590 | if not no_deprecation_warning: |
| 591 | warnings.warn( |
| 592 | "This implementation of AdamW is deprecated and will be removed in a future version. Use the PyTorch" |
| 593 | " implementation torch.optim.AdamW instead, or set `no_deprecation_warning=True` to disable this" |
| 594 | " warning", |
| 595 | FutureWarning, |
| 596 | ) |
| 597 | require_version("torch>=1.5.0") # add_ with alpha |
| 598 | if lr < 0.0: |
| 599 | raise ValueError(f"Invalid learning rate: {lr} - should be >= 0.0") |
| 600 | if not 0.0 <= betas[0] < 1.0: |
| 601 | raise ValueError(f"Invalid beta parameter: {betas[0]} - should be in [0.0, 1.0)") |
| 602 | if not 0.0 <= betas[1] < 1.0: |
| 603 | raise ValueError(f"Invalid beta parameter: {betas[1]} - should be in [0.0, 1.0)") |
| 604 | if not 0.0 <= eps: |
| 605 | raise ValueError(f"Invalid epsilon value: {eps} - should be >= 0.0") |
| 606 | defaults = {"lr": lr, "betas": betas, "eps": eps, "weight_decay": weight_decay, "correct_bias": correct_bias} |
| 607 | super().__init__(params, defaults) |
| 608 | |
| 609 | @torch.no_grad() |
| 610 | def step(self, closure: Callable = None): |
| 611 | """ |
| 612 | Performs a single optimization step. |
| 613 | |
| 614 | Arguments: |
| 615 | closure (`Callable`, *optional*): A closure that reevaluates the model and returns the loss. |
no outgoing calls