Mighty maml runner
mighty.mighty_runners.mighty_maml_runner
#
MightyTRPOMAMLRunner
#
Bases: MightyRunner
Source code in mighty/mighty_runners/mighty_maml_runner.py
conjugate_gradient
#
conjugate_gradient(
Ax,
b,
num_iterations: int = 10,
tol: float = 1e-10,
eps: float = 1e-08,
) -> Tensor
Description#
Computes (x = A^{-1}b) using the conjugate gradient algorithm.
Credit#
Adapted from Kai Arulkumaran's implementation, with additions inspired from John Schulman's implementation.
References#
- Nocedal and Wright. 2006. "Numerical Optimization, 2nd edition". Springer.
- Shewchuk et al. 1994. “An Introduction to the Conjugate Gradient Method without the Agonizing Pain.” CMU.
Arguments#
Ax
(callable) - Given a vector x, computes A@x.b
(tensor or list) - The reference vector.num_iterations
(int, optional, default=10) - Number of conjugate gradient iterations.tol
(float, optional, default=1e-10) - Tolerance for proposed solution.eps
(float, optional, default=1e-8) - Numerical stability constant.
Returns#
x
(tensor or list) - The solution to Ax = b, as a list if b is a list else a tensor.
Source code in mighty/mighty_runners/mighty_maml_runner.py
hessian_vector_product
#
hessian_vector_product(
loss, parameters, damping=1e-05
) -> Callable
Description#
Returns a callable that computes the product of the Hessian of loss (w.r.t. parameters) with another vector, using Pearlmutter's trick.
Note that parameters and the argument of the callable can be tensors or list of tensors.
References#
- Pearlmutter, B. A. 1994. “Fast Exact Multiplication by the Hessian.” Neural Computation.
Arguments#
loss
(tensor) - The loss of which to compute the Hessian.parameters
(tensor or list) - The tensors to take the gradient with respect to.damping
(float, optional, default=1e-5) - Damping of the Hessian-vector product.
Returns#
hvp(other)
(callable) - A function to compute the Hessian-vector product, given a vector or listother
.
Source code in mighty/mighty_runners/mighty_maml_runner.py
maml_update
#
Description
Performs a MAML update on model using grads and lr. The function re-routes the Python object, thus avoiding in-place operations.
The model itself is updated in-place (no deepcopy), but the
parameters' tensors are not.
Arguments
- model (Module) - The model to update.
- lr (float) - The learning rate used to update the model.
- grads (list, optional, default=None) - A list of gradients for each parameter of the model. If None, will use the gradients in .grad attributes.
Example
maml = l2l.algorithms.MAML(Model(), lr=0.1)
model = maml.clone() # The next two lines essentially implement model.adapt(loss)
grads = autograd.grad(loss, model.parameters(), create_graph=True)
maml_update(model, lr=0.1, grads)
Source code in mighty/mighty_runners/mighty_maml_runner.py
update_module
#
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: [ p \gets p + u, ] where (p) is the parameter, and (u) 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
error = loss(model(X), y)
grads = torch.autograd.grad(
error,
model.parameters(),
create_graph=True,
)
updates = [-lr * g for g in grads]
l2l.update_module(model, updates=updates)
Source code in mighty/mighty_runners/mighty_maml_runner.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|