Register a parametrization to a tensor in a module.
Assume that tensor_name="weight" for simplicity. When accessing ,
the module will return the parametrized version .
If the original tensor requires a gradient, the backward pass will differentiate
through , and the optimizer will update the tensor accordingly.
module.weight
parametrization(module.weight)
parametrization
The first time that a module registers a parametrization, this function will add an attribute
parametrizations to the module of type ParametrizationList.
The list of parametrizations on the tensor weight will be accessible under
module.parametrizations.weight.
The original tensor will be accessible under
module.parametrizations.weight.original.
Parametrizations may be concatenated by registering several parametrizations
on the same attribute.
The training mode of a registered parametrization is updated on registration
to match the training mode of the host module
Parametrized parameters and buffers have an inbuilt caching system that can be activated
using the context manager cached().
A parametrization may optionally implement a method with signature
This method is called on the unparametrized tensor when the first parametrization
is registered to compute the initial value of the original tensor.
If this method is not implemented, the original tensor will be just the unparametrized tensor.
If all the parametrizations registered on a tensor implement right_inverse it is possible
to initialize a parametrized tensor by assigning to it, as shown in the example below.
It is possible for the first parametrization to depend on several inputs.
This may be implemented returning a tuple of tensors from right_inverse
(see the example implementation of a RankOne parametrization below).
In this case, the unconstrained tensors are also located under module.parametrizations.weight
with names original0, original1,…
Parameters:
module (nn.Module) – module on which to register the parametrization
tensor_name (str) – name of the parameter or buffer on which to register
the parametrization
parametrization (nn.Module) – the parametrization to register
Keyword Arguments:
unsafe (bool) – a boolean flag that denotes whether the parametrization
may change the dtype and shape of the tensor. Default: False
Warning: the parametrization is not checked for consistency upon registration.
Enable this flag at your own risk.
Raises:
ValueError – if the module does not have a parameter or a buffer named tensor_name
>>> importtorch>>> importtorch.nnasnn>>> importtorch.nn.utils.parametrizeasP>>>>>> classSymmetric(nn.Module):>>> defforward(self,X):>>> returnX.triu()+X.triu(1).T# Return a symmetric matrix>>>>>> defright_inverse(self,A):>>> returnA.triu()>>>>>> m=nn.Linear(5,5)>>> P.register_parametrization(m,"weight",Symmetric())>>> print(torch.allclose(m.weight,m.weight.T))# m.weight is now symmetricTrue>>> A=torch.rand(5,5)>>> A=A+A.T# A is now symmetric>>> m.weight=A# Initialize the weight to be the symmetric matrix A>>> print(torch.allclose(m.weight,A))True
>>> classRankOne(nn.Module):>>> defforward(self,x,y):>>> # Form a rank 1 matrix multiplying two vectors>>> returnx.unsqueeze(-1)@y.unsqueeze(-2)>>>>>> defright_inverse(self,Z):>>> # Project Z onto the rank 1 matrices>>> U,S,Vh=torch.linalg.svd(Z,full_matrices=False)>>> # Return rescaled singular vectors>>> s0_sqrt=S[0].sqrt().unsqueeze(-1)>>> returnU[...,:,0]*s0_sqrt,Vh[...,0,:]*s0_sqrt>>>>>> linear_rank_one=P.register_parametrization(... nn.Linear(4,4),"weight",RankOne()... )>>> print(torch.linalg.matrix_rank(linear_rank_one.weight).item())1