Rate this Page

torch.linalg.tensorinv#

torch.linalg.tensorinv(A,ind=2,*,out=None)Tensor#

Computes the multiplicative inverse oftorch.tensordot().

Ifm is the product of the firstind dimensions ofA andn is the product ofthe rest of the dimensions, this function expectsm andn to be equal.If this is the case, it computes a tensorX such thattensordot(A, X,ind) is the identity matrix in dimensionm.X will have the shape ofA but with the firstind dimensions pushed back to the end

X.shape == A.shape[ind:] + A.shape[:ind]

Supports input of float, double, cfloat and cdouble dtypes.

Note

WhenA is a2-dimensional tensor andind= 1,this function computes the (multiplicative) inverse ofA(seetorch.linalg.inv()).

Note

Consider usingtorch.linalg.tensorsolve() if possible for multiplying a tensor on the leftby the tensor inverse, as:

linalg.tensorsolve(A,B)==torch.tensordot(linalg.tensorinv(A),B)# When B is a tensor with shape A.shape[:B.ndim]

It is always preferred to usetensorsolve() when possible, as it is faster and morenumerically stable than computing the pseudoinverse explicitly.

See also

torch.linalg.tensorsolve() computestorch.tensordot(tensorinv(A),B).

Parameters
  • A (Tensor) – tensor to invert. Its shape must satisfyprod(A.shape[:ind]) ==prod(A.shape[ind:]).

  • ind (int) – index at which to compute the inverse oftorch.tensordot(). Default:2.

Keyword Arguments

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

Raises

RuntimeError – if the reshapedA is not invertible or the product of the firstind dimensions is not equal to the product of the rest.

Examples:

>>>A=torch.eye(4*6).reshape((4,6,8,3))>>>Ainv=torch.linalg.tensorinv(A,ind=2)>>>Ainv.shapetorch.Size([8, 3, 4, 6])>>>B=torch.randn(4,6)>>>torch.allclose(torch.tensordot(Ainv,B),torch.linalg.tensorsolve(A,B))True>>>A=torch.randn(4,4)>>>Atensorinv=torch.linalg.tensorinv(A,ind=1)>>>Ainv=torch.linalg.inv(A)>>>torch.allclose(Atensorinv,Ainv)True