learn2learn¶
clone_module(module, memo=None)
¶
Description
Creates a copy of a module, whose parameters/buffers/submodules are created using PyTorch's torch.clone().
This implies that the computational graph is kept, and you can compute the derivatives of the new modules' parameters w.r.t the original parameters.
Arguments
- module (Module) - Module to be cloned.
Return
- (Module) - The cloned module.
Example
1 2 3 4 |
|
detach_module(module, keep_requires_grad=False)
¶
Description
Detaches all parameters/buffers of a previously cloned module from its computational graph.
Note: detach works in-place, so it does not return a copy.
Arguments
- module (Module) - Module to be detached.
- keep_requires_grad (bool) - By default, all parameters of the detached module will have
requires_grad
set toFalse
. If this flag is set toTrue
, then therequires_grad
field will be the same as the pre-detached module.
Example
1 2 3 4 5 |
|
update_module(module, updates=None, memo=None)
¶
Description
Updates the parameters of a module in-place, in a way that preserves differentiability.
The parameters of the module are swapped with their update values, according to: where is the parameter, and is its corresponding update.
Arguments
- module (Module) - The module to update.
- updates (list, optional, default=None) - A list of gradients for each parameter of the model. If None, will use the tensors in .update attributes.
Example
1 2 3 4 5 6 7 8 |
|
magic_box(x)
¶
Description
The magic box operator, which evaluates to 1 but whose gradient is :
where is the stop-gradient (or detach) operator.
This operator is useful when computing higher-order derivatives of stochastic graphs. For more informations, please refer to the DiCE paper. (Reference 1)
References
- Foerster et al. 2018. "DiCE: The Infinitely Differentiable Monte-Carlo Estimator." arXiv.
Arguments
- x (Variable) - Variable to transform.
Return
- (Variable) - Tensor of 1, but it's gradient is the gradient of x.
Example
1 2 |
|