r"""Implements Adam algorithm. It has been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3)
| 76 | |
| 77 | |
| 78 | class AdamW(Optimizer): |
| 79 | r"""Implements Adam algorithm. |
| 80 | |
| 81 | It has been proposed in `Adam: A Method for Stochastic Optimization`_. |
| 82 | |
| 83 | Arguments: |
| 84 | params (iterable): iterable of parameters to optimize or dicts defining |
| 85 | parameter groups |
| 86 | lr (float, optional): learning rate (default: 1e-3) |
| 87 | betas (Tuple[float, float], optional): coefficients used for computing |
| 88 | running averages of gradient and its square (default: (0.9, 0.999)) |
| 89 | eps (float, optional): term added to the denominator to improve |
| 90 | numerical stability (default: 1e-8) |
| 91 | weight_decay (float, optional): weight decay (L2 penalty) (default: 0) |
| 92 | amsgrad (boolean, optional): whether to use the AMSGrad variant of this |
| 93 | algorithm from the paper `On the Convergence of Adam and Beyond`_ |
| 94 | (default: False) |
| 95 | |
| 96 | .. _Adam\: A Method for Stochastic Optimization: |
| 97 | https://arxiv.org/abs/1412.6980 |
| 98 | .. _On the Convergence of Adam and Beyond: |
| 99 | https://openreview.net/forum?id=ryQu7f-RZ |
| 100 | """ |
| 101 | |
| 102 | def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, |
| 103 | weight_decay=0, amsgrad=False): |
| 104 | if not 0.0 <= lr: |
| 105 | raise ValueError("Invalid learning rate: {}".format(lr)) |
| 106 | if not 0.0 <= eps: |
| 107 | raise ValueError("Invalid epsilon value: {}".format(eps)) |
| 108 | if not 0.0 <= betas[0] < 1.0: |
| 109 | raise ValueError( |
| 110 | "Invalid beta parameter at index 0: {}".format(betas[0])) |
| 111 | if not 0.0 <= betas[1] < 1.0: |
| 112 | raise ValueError( |
| 113 | "Invalid beta parameter at index 1: {}".format(betas[1])) |
| 114 | if not 0.0 <= weight_decay: |
| 115 | raise ValueError( |
| 116 | "Invalid weight_decay value: {}".format(weight_decay)) |
| 117 | defaults = dict(lr=lr, betas=betas, eps=eps, |
| 118 | weight_decay=weight_decay, amsgrad=amsgrad) |
| 119 | super(AdamW, self).__init__(params, defaults) |
| 120 | |
| 121 | def __setstate__(self, state): |
| 122 | super(AdamW, self).__setstate__(state) |
| 123 | for group in self.param_groups: |
| 124 | group.setdefault('amsgrad', False) |
| 125 | |
| 126 | def get_second_moments(self): |
| 127 | return self.second_moments |
| 128 | |
| 129 | @torch.no_grad() |
| 130 | def step(self, closure=None): |
| 131 | """Performs a single optimization step. |
| 132 | |
| 133 | Arguments: |
| 134 | closure (callable, optional): A closure that reevaluates the model |
| 135 | and returns the loss. |
no outgoing calls
no test coverage detected