| 154 | |
| 155 | |
| 156 | class AdaGrad(OptimizerBase): |
| 157 | def __init__(self, lr=0.01, eps=1e-7, clip_norm=None, lr_scheduler=None, **kwargs): |
| 158 | """ |
| 159 | An AdaGrad optimizer. |
| 160 | |
| 161 | Notes |
| 162 | ----- |
| 163 | Weights that receive large gradients will have their effective learning |
| 164 | rate reduced, while weights that receive small or infrequent updates |
| 165 | will have their effective learning rate increased. |
| 166 | |
| 167 | Equations:: |
| 168 | |
| 169 | cache[t] = cache[t-1] + grad[t] ** 2 |
| 170 | update[t] = lr * grad[t] / (np.sqrt(cache[t]) + eps) |
| 171 | param[t+1] = param[t] - update[t] |
| 172 | |
| 173 | Note that the ``**`` and `/` operations are elementwise |
| 174 | |
| 175 | "A downside of Adagrad ... is that the monotonic learning rate usually |
| 176 | proves too aggressive and stops learning too early." [1] |
| 177 | |
| 178 | References |
| 179 | ---------- |
| 180 | .. [1] Karpathy, A. "CS231n: Convolutional neural networks for visual |
| 181 | recognition" https://cs231n.github.io/neural-networks-3/ |
| 182 | |
| 183 | Parameters |
| 184 | ---------- |
| 185 | lr : float |
| 186 | Global learning rate |
| 187 | eps : float |
| 188 | Smoothing term to avoid divide-by-zero errors in the update calc. |
| 189 | Default is 1e-7. |
| 190 | clip_norm : float or None |
| 191 | If not None, all param gradients are scaled to have maximum `L2` norm of |
| 192 | `clip_norm` before computing update. Default is None. |
| 193 | lr_scheduler : str or :doc:`Scheduler <numpy_ml.neural_nets.schedulers>` object or None |
| 194 | The learning rate scheduler. If None, use a constant learning |
| 195 | rate equal to `lr`. Default is None. |
| 196 | """ |
| 197 | super().__init__(lr, lr_scheduler) |
| 198 | |
| 199 | self.cache = {} |
| 200 | self.hyperparameters = { |
| 201 | "id": "AdaGrad", |
| 202 | "lr": lr, |
| 203 | "eps": eps, |
| 204 | "clip_norm": clip_norm, |
| 205 | "lr_scheduler": str(self.lr_scheduler), |
| 206 | } |
| 207 | |
| 208 | def __str__(self): |
| 209 | H = self.hyperparameters |
| 210 | lr, eps, cn, sc = H["lr"], H["eps"], H["clip_norm"], H["lr_scheduler"] |
| 211 | return "AdaGrad(lr={}, eps={}, clip_norm={}, lr_scheduler={})".format( |
| 212 | lr, eps, cn, sc |
| 213 | ) |
no outgoing calls
no test coverage detected