MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / update

Method update

numpy_ml/neural_nets/optimizers/optimizers.py:215–259  ·  view source on GitHub ↗

Compute the AdaGrad update for a given parameter. Notes ----- Adjusts the learning rate of each weight based on the magnitudes of its gradients (big gradient -> small lr, small gradient -> big lr). Parameters ---------- param : :py:c

(self, param, param_grad, param_name, cur_loss=None)

Source from the content-addressed store, hash-verified

213 )
214
215 def update(self, param, param_grad, param_name, cur_loss=None):
216 """
217 Compute the AdaGrad update for a given parameter.
218
219 Notes
220 -----
221 Adjusts the learning rate of each weight based on the magnitudes of its
222 gradients (big gradient -> small lr, small gradient -> big lr).
223
224 Parameters
225 ----------
226 param : :py:class:`ndarray <numpy.ndarray>` of shape (n, m)
227 The value of the parameter to be updated
228 param_grad : :py:class:`ndarray <numpy.ndarray>` of shape (n, m)
229 The gradient of the loss function with respect to `param_name`
230 param_name : str
231 The name of the parameter
232 cur_loss : float or None
233 The training or validation loss for the current minibatch. Used for
234 learning rate scheduling e.g., by
235 :class:`~numpy_ml.neural_nets.schedulers.KingScheduler`.
236 Default is None.
237
238 Returns
239 -------
240 updated_params : :py:class:`ndarray <numpy.ndarray>` of shape (n, m)
241 The value of `param` after applying the AdaGrad update
242 """
243 C = self.cache
244 H = self.hyperparameters
245 eps, clip_norm = H["eps"], H["clip_norm"]
246 lr = self.lr_scheduler(self.cur_step, cur_loss)
247
248 if param_name not in C:
249 C[param_name] = np.zeros_like(param_grad)
250
251 # scale gradient to avoid explosion
252 t = np.inf if clip_norm is None else clip_norm
253 if norm(param_grad) > t:
254 param_grad = param_grad * t / norm(param_grad)
255
256 C[param_name] += param_grad ** 2
257 update = lr * param_grad / (np.sqrt(C[param_name]) + eps)
258 self.cache = C
259 return param - update
260
261
262class RMSProp(OptimizerBase):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected