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

Method update

numpy_ml/neural_nets/optimizers/optimizers.py:110–148  ·  view source on GitHub ↗

Compute the SGD update for a given parameter Parameters ---------- param : :py:class:`ndarray ` of shape (n, m) The value of the parameter to be updated. param_grad : :py:class:`ndarray ` of shape (n, m)

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

Source from the content-addressed store, hash-verified

108 )
109
110 def update(self, param, param_grad, param_name, cur_loss=None):
111 """
112 Compute the SGD update for a given parameter
113
114 Parameters
115 ----------
116 param : :py:class:`ndarray <numpy.ndarray>` of shape (n, m)
117 The value of the parameter to be updated.
118 param_grad : :py:class:`ndarray <numpy.ndarray>` of shape (n, m)
119 The gradient of the loss function with respect to `param_name`.
120 param_name : str
121 The name of the parameter.
122 cur_loss : float
123 The training or validation loss for the current minibatch. Used for
124 learning rate scheduling e.g., by
125 :class:`~numpy_ml.neural_nets.schedulers.KingScheduler`.
126 Default is None.
127
128 Returns
129 -------
130 updated_params : :py:class:`ndarray <numpy.ndarray>` of shape (n, m)
131 The value of `param` after applying the momentum update.
132 """
133 C = self.cache
134 H = self.hyperparameters
135 momentum, clip_norm = H["momentum"], H["clip_norm"]
136 lr = self.lr_scheduler(self.cur_step, cur_loss)
137
138 if param_name not in C:
139 C[param_name] = np.zeros_like(param_grad)
140
141 # scale gradient to avoid explosion
142 t = np.inf if clip_norm is None else clip_norm
143 if norm(param_grad) > t:
144 param_grad = param_grad * t / norm(param_grad)
145
146 update = momentum * C[param_name] + lr * param_grad
147 self.cache[param_name] = update
148 return param - update
149
150
151#######################################################################

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected