Base class of all numerical optimizers.
This class provides basic features for all optimization methods. It optimizes parameters of a target link. The target link is registered via the
setup()method, and then the
update()method updates its parameters based on a given loss function.
Each optimizer implementation must be defined as a child class of Optimizer. It must override
If the optimizer is based on single gradient computation (like most first-order methods), then it should inherit
GradientMethod, which adds some features dedicated for the first order methods, including the support of
Optimizer instance also supports hook functions. Hook function is registered by the
add_hook()method. Each hook function is called in registration order before of after the actual parameter update (configurable). If the hook function has an attribute
call_for_each_paramand its value is
True, the hook function is used as a hook function of all update rules (i.e., it is invoked for every parameter by passing the corresponding update rule and the parameter).
add_hook(hook, name=None, timing='auto')¶
Registers a hook function.
Hook function is typically called right after the gradient computation, though the timing depends on the optimization method, and the timing attribute.
hook (callable) – Hook function. If
hook.call_for_each_paramis true, this hook function is called for each parameter by passing the update rule and the parameter. Otherwise, this hook function is called only once each iteration by passing the optimizer.
name (str) – Name of the registration. If omitted,
hook.nameis used by default.
timing (str) – Specifies when the hook is called. If ‘auto’, the timimg property of the hook will decide the timing. If ‘pre’, the hook will be called before any updates. If ‘post’, the hook will be called after any updates.
Invokes hook functions in registration order.
Starts a new epoch.
This method increments the
epochcount. Note that if the optimizer depends on the epoch count, then user should call this method appropriately at the beginning of each epoch.
Removes a hook function.
name (str) – Registered name of the hook function to remove.
Serializes or deserializes the optimizer.
It only saves or loads the following things:
It does not saves nor loads the parameters of the target link. They should be separately saved or loaded.
serializer (AbstractSerializer) – Serializer or deserializer object.
Sets loss scaling factor.
Sets a target link and initializes the optimizer states.
Given link is set to the
targetattribute. It also prepares the optimizer state dictionaries corresponding to all parameters in the link hierarchy. The existing states are discarded.
link (Link) – Target link object.
The optimizer instance.
As of v4.0.0, this function returns the optimizer instance itself so that you can instantiate and setup the optimizer in one line, e.g.,
optimizer = SomeOptimizer().setup(link).
update(lossfun=None, *args, **kwds)¶
Updates the parameters.
This method updates the parameters of the target link. The behavior of this method is different for the cases either
lossfunis given or not.
lossfunis given, this method typically clears the gradients, calls the loss function with given extra arguments, and calls the
backward()method of its output to compute the gradients. The actual implementation might call
lossfunmore than once.
lossfunis not given, then this method assumes that the gradients of all parameters are already computed. An implementation that requires multiple gradient computations might raise an error on this case.
In both cases, this method invokes the update procedure for all parameters.
lossfun (callable) – Loss function. You can specify one of loss functions from built-in loss functions, or your own loss function. It should not be an loss functions with parameters (i.e.,
Linkinstance). The function must accept arbitrary arguments and return one
Variableobject that represents the loss (or objective) value. Returned value must be a Variable derived from the input Variable object.
lossfuncan be omitted for single gradient-based methods. In this case, this method assumes gradient arrays computed.
kwds (args,) – Arguments for the loss function.