- class chainer.GradientMethod¶
Base class of all single gradient-based optimizers.
This is an extension of the
Optimizerclass. Typical gradient methods that just require the gradient at the current parameter vector on an update can be implemented as its child class.
This class also provides
hyperparam, which is the hyperparameter used as the default configuration of each update rule. All built-in gradient method implementations also provide proxy properties that act as aliases to the attributes of
hyperparam. It is recommended that you provide such an alias to each attribute. It can be done by only adding one line for each attribute using
~GradientMethod.hyperparam (Hyperparameter) – The hyperparameter of the gradient method. It is used as the default configuration of each update rule (i.e., the hyperparameter of each update rule refers this hyperparameter as its parent).
- 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.
Checks if there is NaN in grads when dynamic loss scaling used.
Creates a new update rule object.
This method creates an update rule object. It is called by
setup()to set up an update rule of each parameter. Each implementation of the gradient method should override this method to provide the default update rule implementation.
Update rule object.
- Return type
- loss_scaling(interval=1000, scale=None)¶
Configures the loss scaling algorithm.
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.
Reallocate gradients cleared by
This method allocates arrays for all gradients which have
None. This method is called before and after every optimizer hook. If an inheriting optimizer does not require this allocation, the optimizer can override this method with a blank function.
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 parameters based on a loss function or computed gradients.
This method runs in two ways.
lossfunis given, then it is used as a loss function to compute gradients.
Otherwise, this method assumes that the gradients are already computed.
In both cases, the computed gradients are used to update parameters. The actual update routines are defined by the update rule of each parameter.
Enables or disables use of
use (bool) – If
True, this function enables use of cleargrads. If
False, disables use of cleargrads (zerograds is used).
Enables use of parameter update in fp32.
- __eq__(value, /)¶
- __ne__(value, /)¶
- __lt__(value, /)¶
- __le__(value, /)¶
- __gt__(value, /)¶
- __ge__(value, /)¶
- epoch = 0¶
- t = 0¶
- target = None¶
- use_auto_new_epoch = False¶