torch.linalg.pinv#
- torch.linalg.pinv(A,*,atol=None,rtol=None,hermitian=False,out=None)→Tensor#
Computes the pseudoinverse (Moore-Penrose inverse) of a matrix.
The pseudoinverse may bedefined algebraicallybut it is more computationally convenient to understand itthrough the SVD
Supports input of float, double, cfloat and cdouble dtypes.Also supports batches of matrices, and if
Ais a batch of matrices thenthe output has the same batch dimensions.If
hermitian= True,Ais assumed to be Hermitian if complex orsymmetric if real, but this is not checked internally. Instead, just the lowertriangular part of the matrix is used in the computations.The singular values (or the norm of the eigenvalues when
hermitian= True)that are below threshold aretreated as zero and discarded in the computation,where is the largest singular value (or eigenvalue).If
rtolis not specified andAis a matrix of dimensions(m, n),the relative tolerance is set to beand is the epsilon value for the dtype ofA(seefinfo).Ifrtolis not specified andatolis specified to be larger than zero thenrtolis set to zero.If
atolorrtolis atorch.Tensor, its shape must be broadcastable to thatof the singular values ofAas returned bytorch.linalg.svd().Note
This function uses
torch.linalg.svd()ifhermitian= False andtorch.linalg.eigh()ifhermitian= True.For CUDA inputs, this function synchronizes that device with the CPU.Note
Consider using
torch.linalg.lstsq()if possible for multiplying a matrix on the left bythe pseudoinverse, as:torch.linalg.lstsq(A,B).solution==A.pinv()@B
It is always preferred to use
lstsq()when possible, as it is faster and morenumerically stable than computing the pseudoinverse explicitly.Note
This function has NumPy compatible variantlinalg.pinv(A, rcond, hermitian=False).However, use of the positional argument
rcondis deprecated in favor ofrtol.Warning
This function uses internally
torch.linalg.svd()(ortorch.linalg.eigh()whenhermitian= True), so its derivative has the same problems as those of thesefunctions. See the warnings intorch.linalg.svd()andtorch.linalg.eigh()formore details.See also
torch.linalg.inv()computes the inverse of a square matrix.torch.linalg.lstsq()computesA.pinv() @Bwith anumerically stable algorithm.- Parameters
- Keyword Arguments
atol (float,Tensor,optional) – the absolute tolerance value. WhenNone it’s considered to be zero.Default:None.
rtol (float,Tensor,optional) – the relative tolerance value. See above for the value it takes whenNone.Default:None.
hermitian (bool,optional) – indicates whether
Ais Hermitian if complexor symmetric if real. Default:False.out (Tensor,optional) – output tensor. Ignored ifNone. Default:None.
Examples:
>>>A=torch.randn(3,5)>>>Atensor([[ 0.5495, 0.0979, -1.4092, -0.1128, 0.4132], [-1.1143, -0.3662, 0.3042, 1.6374, -0.9294], [-0.3269, -0.5745, -0.0382, -0.5922, -0.6759]])>>>torch.linalg.pinv(A)tensor([[ 0.0600, -0.1933, -0.2090], [-0.0903, -0.0817, -0.4752], [-0.7124, -0.1631, -0.2272], [ 0.1356, 0.3933, -0.5023], [-0.0308, -0.1725, -0.5216]])>>>A=torch.randn(2,6,3)>>>Apinv=torch.linalg.pinv(A)>>>torch.dist(Apinv@A,torch.eye(3))tensor(8.5633e-07)>>>A=torch.randn(3,3,dtype=torch.complex64)>>>A=A+A.T.conj()# creates a Hermitian matrix>>>Apinv=torch.linalg.pinv(A,hermitian=True)>>>torch.dist(Apinv@A,torch.eye(3))tensor(1.0830e-06)