ngclearn.utils.optim package

Submodules

ngclearn.utils.optim.adam module

ngclearn.utils.optim.adam.adam_init(theta)[source]
ngclearn.utils.optim.adam.adam_step(opt_params, theta, updates, eta=0.001, beta1=0.9, beta2=0.999, eps=1e-08)[source]
Implements the adaptive moment estimation (Adam) algorithm as a decoupled

update rule given adjustments produced by a credit assignment algorithm/process.

Parameters:
  • opt_params – (ArrayLike) parameters of the optimization algorithm

  • theta – (ArrayLike) the weights of neural network

  • updates – (ArrayLike) the updates of neural network

  • eta – (float, optional) step size coefficient for Adam update (Default: 0.001)

  • beta1 – (float, optional) 1st moment control factor. (Default: 0.9)

  • beta2 – (float, optional) 2nd moment control factor. (Default: 0.999)

  • eps – (float, optional) numberical stability coefficient (for calculating final update). (Default: 1e-8)

Returns:

opt_params. New opt params, ArrayLike: theta. The updated weights

Return type:

ArrayLike

ngclearn.utils.optim.adam.step_update(param, update, g1, g2, eta, beta1, beta2, time_step, eps)[source]

Runs one step of Adam over a set of parameters given updates. The dynamics for any set of parameters is as follows:

g1 = beta1 * g1 + (1 - beta1) * update
g2 = beta2 * g2 + (1 - beta2) * (update)^2
g1_unbiased = g1 / (1 - beta1**time)
g2_unbiased = g2 / (1 - beta2**time)
param = param - lr * g1_unbiased / (sqrt(g2_unbiased) + epsilon)
Parameters:
  • param – parameter tensor to change/adjust

  • update – update tensor to be applied to parameter tensor (must be same shape as “param”)

  • g1 – first moment factor/correction factor to use in parameter update (must be same shape as “update”)

  • g2 – second moment factor/correction factor to use in parameter update (must be same shape as “update”)

  • eta – global step size value to be applied to updates to parameters

  • beta1 – 1st moment control factor

  • beta2 – 2nd moment control factor

  • time_step – current time t or iteration step/call to this Adam update

  • eps – numberical stability coefficient (for calculating final update)

Returns:

adjusted parameter tensor (same shape as “param”), adjusted g1, adjusted g2

ngclearn.utils.optim.nag module

ngclearn.utils.optim.nag.nag_init(theta)[source]
ngclearn.utils.optim.nag.nag_step(opt_params, theta, updates, eta=0.01, mu=0.9)[source]

Implements Nesterov’s accelerated gradient (NAG) algorithm as a decoupled update rule given adjustments produced by a credit assignment algorithm/process.

Parameters:
  • opt_params – (ArrayLike) parameters of the optimization algorithm

  • theta – (ArrayLike) the weights of neural network

  • updates – (ArrayLike) the updates of neural network

  • eta – (float, optional) step size coefficient for NAG update (Default: 0.001)

  • mu – (float, optional) friction/momentum control factor. (Default: 0.9)

Returns:

opt_params. New opt params, ArrayLike: theta. The updated weights

Return type:

ArrayLike

ngclearn.utils.optim.nag.step_update(param, update, phi_old, eta, mu, time_step)[source]

Runs one step of Nesterov’s accelerated gradient (NAG) over a set of parameters given updates. The dynamics for any set of parameters is as follows:

phi = param - update * lr
param = phi + (phi - phi_previous) * mu, where mu = 0 iff t <= 1 (first iteration)
Parameters:
  • param – parameter tensor to change/adjust

  • update – update tensor to be applied to parameter tensor (must be same shape as “param”)

  • phi_old – previous friction/momentum parameter

  • eta – global step size value to be applied to updates to parameters

  • mu – friction/momentum control factor

  • time_step – current time t or iteration step/call to this NAG update

Returns:

adjusted parameter tensor (same shape as “param”), adjusted momentum/friction variable

ngclearn.utils.optim.optim_utils module

ngclearn.utils.optim.optim_utils.get_opt_init_fn(opt='adam')[source]
ngclearn.utils.optim.optim_utils.get_opt_step_fn(opt='adam', **kwargs)[source]

ngclearn.utils.optim.sgd module

ngclearn.utils.optim.sgd.sgd_init(theta)[source]
ngclearn.utils.optim.sgd.sgd_step(opt_params, theta, updates, eta=0.001)[source]

Returns updated parameters in accordance to a stochastic gradient descent (SGD) recipe

Parameters:
  • opt_params – (ArrayLike) parameters of the optimization algorithm

  • theta – (ArrayLike) the weights of neural networks

  • updates – (ArrayLike) the updates of neural networks

  • eta – (float, optional) hyperparams. Defaults to 0.001.

Returns:

opt_params. New opt params, ArrayLike: theta. The updated weights

Return type:

ArrayLike

ngclearn.utils.optim.sgd.step_update(param, update, eta)[source]

Runs one step of SGD over a set of parameters given updates.

Parameters:

eta – global step size to apply when adjusting parameters

Returns:

adjusted parameter tensor (same shape as “param”)

Module contents