Rate this Page

torch.linalg.matrix_norm#

torch.linalg.matrix_norm(A,ord='fro',dim=(-2,-1),keepdim=False,*,dtype=None,out=None)Tensor#

Computes a matrix norm.

IfA is complex valued, it computes the norm ofA.abs()

Support input of float, double, cfloat and cdouble dtypes.Also supports batches of matrices: the norm will be computed over thedimensions specified by the 2-tupledim and the other dimensions willbe treated as batch dimensions. The output will have the same batch dimensions.

ord defines the matrix norm that is computed. The following norms are supported:

ord

matrix norm

‘fro’ (default)

Frobenius norm

‘nuc’

nuclear norm

inf

max(sum(abs(x), dim=1))

-inf

min(sum(abs(x), dim=1))

1

max(sum(abs(x), dim=0))

-1

min(sum(abs(x), dim=0))

2

largest singular value

-2

smallest singular value

whereinf refers tofloat(‘inf’), NumPy’sinf object, or any equivalent object.

Parameters
  • A (Tensor) – tensor with two or more dimensions. By default itsshape is interpreted as(*, m, n) where* is zero or morebatch dimensions, but this behavior can be controlled usingdim.

  • ord (int,inf,-inf,'fro','nuc',optional) – order of norm. Default:‘fro’

  • dim (Tuple[int,int],optional) – dimensions over which to compute the norm. Default:(-2, -1)

  • keepdim (bool,optional) – If set toTrue, the reduced dimensions are retainedin the result as dimensions with size one. Default:False

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

  • dtype (torch.dtype, optional) – If specified, the input tensor is cast todtype before performing the operation, and the returned tensor’s typewill bedtype. Default:None

Returns

A real-valued tensor, even whenA is complex.

Examples:

>>>fromtorchimportlinalgasLA>>>A=torch.arange(9,dtype=torch.float).reshape(3,3)>>>Atensor([[0., 1., 2.],        [3., 4., 5.],        [6., 7., 8.]])>>>LA.matrix_norm(A)tensor(14.2829)>>>LA.matrix_norm(A,ord=-1)tensor(9.)>>>B=A.expand(2,-1,-1)>>>Btensor([[[0., 1., 2.],        [3., 4., 5.],        [6., 7., 8.]],        [[0., 1., 2.],        [3., 4., 5.],        [6., 7., 8.]]])>>>LA.matrix_norm(B)tensor([14.2829, 14.2829])>>>LA.matrix_norm(B,dim=(0,2))tensor([ 3.1623, 10.0000, 17.2627])