Rate this Page

torch.functional.norm#

torch.functional.norm(input,p='fro',dim=None,keepdim=False,out=None,dtype=None)[source]#

Returns the matrix norm or vector norm of a given tensor.

Warning

torch.norm is deprecated and may be removed in a future PyTorch release.Its documentation and behavior may be incorrect, and it is no longeractively maintained.

Usetorch.linalg.vector_norm() when computing vector norms andtorch.linalg.matrix_norm() when computing matrix norms.For a function with a similar behavior as this one seetorch.linalg.norm().Note, however, the signature for these functions is slightly different than thesignature fortorch.norm.

Parameters
  • input (Tensor) – The input tensor. Its data type must be either a floatingpoint or complex type. For complex inputs, the norm is calculated using theabsolute value of each element. If the input is complex and neitherdtype norout is specified, the result’s data type willbe the corresponding floating point type (e.g. float ifinput iscomplexfloat).

  • p (int,float,inf,-inf,'fro','nuc',optional) –

    the order of norm. Default:'fro'The following norms can be calculated:

    ord

    matrix norm

    vector norm

    ’fro’

    Frobenius norm

    ‘nuc’

    nuclear norm

    Number

    sum(abs(x)**ord)**(1./ord)

    The vector norm can be calculated across any number of dimensions.The corresponding dimensions ofinput are flattened intoone dimension, and the norm is calculated on the flatteneddimension.

    Frobenius norm produces the same result asp=2 in all casesexcept whendim is a list of three or more dims, in whichcase Frobenius norm throws an error.

    Nuclear norm can only be calculated across exactly two dimensions.

  • dim (int,tuple ofints,list ofints,optional) – Specifies which dimension or dimensions ofinput tocalculate the norm across. Ifdim isNone, the norm willbe calculated across all dimensions ofinput. If the normtype indicated byp does not support the specified number ofdimensions, an error will occur.

  • keepdim (bool,optional) – whether the output tensors havedimretained or not. Ignored ifdim =None andout =None. Default:False

  • out (Tensor,optional) – the output tensor. Ignored ifdim =None andout =None.

  • dtype (torch.dtype, optional) – the desired data type ofreturned tensor. If specified, the input tensor is casted todtype while performing the operation. Default: None.

Note

Even thoughp='fro' supports any number of dimensions, the truemathematical definition of Frobenius norm only applies to tensors withexactly two dimensions.torch.linalg.matrix_norm() withord='fro'aligns with the mathematical definition, since it can only be applied acrossexactly two dimensions.

Example:

>>>importtorch>>>a=torch.arange(9,dtype=torch.float)-4>>>b=a.reshape((3,3))>>>torch.norm(a)tensor(7.7460)>>>torch.norm(b)tensor(7.7460)>>>torch.norm(a,float('inf'))tensor(4.)>>>torch.norm(b,float('inf'))tensor(4.)>>>c=torch.tensor([[1,2,3],[-1,1,4]],dtype=torch.float)>>>torch.norm(c,dim=0)tensor([1.4142, 2.2361, 5.0000])>>>torch.norm(c,dim=1)tensor([3.7417, 4.2426])>>>torch.norm(c,p=1,dim=1)tensor([6., 6.])>>>d=torch.arange(8,dtype=torch.float).reshape(2,2,2)>>>torch.norm(d,dim=(1,2))tensor([ 3.7417, 11.2250])>>>torch.norm(d[0,:,:]),torch.norm(d[1,:,:])(tensor(3.7417), tensor(11.2250))