# torch.linalg.pinv¶

torch.linalg.pinv(A, rcond=1e-15, hermitian=False, *, out=None) → Tensor

Computes the pseudoinverse (Moore-Penrose inverse) of a matrix.

The pseudoinverse may be defined algebraically but it is more computationally convenient to understand it through the SVD

Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if A is a batch of matrices then the output has the same batch dimensions.

If hermitian= True, A is assumed to be Hermitian if complex or symmetric if real, but this is not checked internally. Instead, just the lower triangular part of the matrix is used in the computations.

The singular values (or the norm of the eigenvalues when hermitian= True) that are below the specified rcond threshold are treated as zero and discarded in the computation.

Note

This function uses torch.linalg.svd() if hermitian= False and torch.linalg.eigh() if hermitian= 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 by the the pseudoinverse, as:

torch.linalg.lstsq(A, B).solution == A.pinv() @ B


It is always prefered to use lstsq() when possible, as it is faster and more numerically stable than computing the pseudoinverse explicitly.

Warning

This function uses internally torch.linalg.svd() (or torch.linalg.eigh() when hermitian= True), so its derivative has the same problems as those of these functions. See the warnings in torch.linalg.svd() and torch.linalg.eigh() for more details.

torch.linalg.inv() computes the inverse of a square matrix.

torch.linalg.lstsq() computes A.pinv() @ B with a numerically stable algorithm.

Parameters
• A (Tensor) – tensor of shape (*, m, n) where * is zero or more batch dimensions.

• rcond (float or Tensor, optional) – the tolerance value to determine when is a singular value zero If it is a torch.Tensor, its shape must be broadcastable to that of the singular values of A as returned by torch.svd(). Default: 1e-15.

• hermitian (bool, optional) – indicates whether A is Hermitian if complex or symmetric if real. Default: False.

Keyword Arguments

out (Tensor, optional) – output tensor. Ignored if None. Default: None.

Examples:

>>> A = torch.randn(3, 5)
>>> A
tensor([[ 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]])

Batched linalg.pinv example
>>> A = torch.randn(2, 6, 3)
>>> B = torch.linalg.pinv(A)
>>> torch.matmul(B, A).round()
tensor([[[1., -0., 0.],
[0., 1., -0.],
[0., 0., 1.]],

[[1., -0., 0.],
[-0., 1., 0.],
[-0., -0., 1.]]])

Hermitian input example
>>> A = torch.randn(3, 3, dtype=torch.complex64)
>>> A = A + A.t().conj()  # creates a Hermitian matrix
>>> B = torch.linalg.pinv(A, hermitian=True)
>>> torch.matmul(B, A)
tensor([[ 1.0000e+00+0.0000e+00j, -1.1921e-07-2.3842e-07j,
5.9605e-08-2.3842e-07j],
[ 5.9605e-08+2.3842e-07j,  1.0000e+00+2.3842e-07j,
-4.7684e-07+1.1921e-07j],
[-1.1921e-07+0.0000e+00j, -2.3842e-07-2.9802e-07j,
1.0000e+00-1.7897e-07j]])

Non-default rcond example
>>> rcond = 0.5
>>> A = torch.randn(3, 3)
>>> torch.linalg.pinv(A)
tensor([[ 0.2971, -0.4280, -2.0111],
[-0.0090,  0.6426, -0.1116],
[-0.7832, -0.2465,  1.0994]])
>>> torch.linalg.pinv(A, rcond)
tensor([[-0.2672, -0.2351, -0.0539],
[-0.0211,  0.6467, -0.0698],
[-0.4400, -0.3638, -0.0910]])

Matrix-wise rcond example
>>> A = torch.randn(5, 6, 2, 3, 3)
>>> rcond = torch.rand(2)  # different rcond values for each matrix in a[:, :, 0] and a[:, :, 1]
>>> torch.linalg.pinv(A, rcond)
>>> rcond = torch.randn(5, 6, 2) # different rcond value for each matrix in 'a'
>>> torch.linalg.pinv(A, rcond)