oneflow.linalg.norm¶
-
oneflow.linalg.norm(input, ord=None, dim=None, keepdim=False, *, dtype=None, out=None) → Tensor¶ Returns the matrix norm or vector norm of a given tensor.
This function can calculate one of eight different types of matrix norms, or one of an infinite number of vector norms, depending on both the number of reduction dimensions and the value of the ord parameter.
- Parameters
input (Tensor) – The input tensor. If dim is None, input must be 1-D or 2-D, unless
ordis None. If bothdimandordare None, the 2-norm of the input flattened to 1-D will be returned. Its data type must be either a floating point or complex type. For complex inputs, the norm is calculated on of the absolute values of each element. If the input is complex and neitherdtypenoroutis specified, the result’s data type will be the corresponding floating point type (e.g. float ifinputis complexfloat).ord (int, inf, -inf, 'fro', 'nuc', optional) –
order of norm. Default: ‘None’ The following norms can be calculated:
ordnorm for matrices
norm for vectors
None
Frobenius norm
2-norm
’fro’
Frobenius norm
– not supported –
‘nuc’
– not supported yet –
– not supported –
inf
max(sum(abs(x), dim=1))
max(abs(x))
-inf
min(sum(abs(x), dim=1))
min(abs(x))
0
– not supported –
sum(x != 0)
1
max(sum(abs(x), dim=0))
as below
-1
min(sum(abs(x), dim=0))
as below
2
– not supported yet –
as below
-2
– not supported yet –
as below
other
– not supported –
sum(abs(x)^{ord})^{(1 / ord)}
where inf refers to float(‘inf’), NumPy’s inf object, or any equivalent object.
dim (int, 2-tuple of ints, 2-list of ints, optional) – If
dimis an int, vector norm will be calculated over the specified dimension. Ifdimis a 2-tuple of ints, matrix norm will be calculated over the specified dimensions. Ifdimis None, matrix norm will be calculated when the input tensor has two dimensions, and vector norm will be calculated when the input tensor has one dimension. Default:Nonekeepdim (bool, optional) – If set to True, the reduced dimensions are retained in the result as dimensions with size one. Default:
Falseout (Tensor, optional) – The output tensor.
For example:
>>> import oneflow as flow >>> from oneflow import linalg as LA >>> import numpy as np >>> a = flow.tensor(np.arange(9, dtype=np.float32) - 4) >>> a tensor([-4., -3., -2., -1., 0., 1., 2., 3., 4.], dtype=oneflow.float32) >>> b = a.reshape(3, 3) >>> b tensor([[-4., -3., -2.], [-1., 0., 1.], [ 2., 3., 4.]], dtype=oneflow.float32) >>> LA.norm(a) tensor(7.7460, dtype=oneflow.float32) >>> LA.norm(b) tensor(7.7460, dtype=oneflow.float32) >>> LA.norm(b, 'fro') tensor(7.7460, dtype=oneflow.float32) >>> LA.norm(a, float('inf')) tensor(4., dtype=oneflow.float32) >>> LA.norm(b, float('inf')) tensor(9., dtype=oneflow.float32) >>> LA.norm(a, -float('inf')) tensor(0., dtype=oneflow.float32) >>> LA.norm(b, -float('inf')) tensor(2., dtype=oneflow.float32) >>> LA.norm(a, 1) tensor(20., dtype=oneflow.float32) >>> LA.norm(b, 1) tensor(7., dtype=oneflow.float32) >>> LA.norm(a, -1) tensor(0., dtype=oneflow.float32) >>> LA.norm(b, -1) tensor(6., dtype=oneflow.float32) >>> LA.norm(a, 2) tensor(7.7460, dtype=oneflow.float32) >>> LA.norm(a, -2) tensor(0., dtype=oneflow.float32) >>> LA.norm(a, 3) tensor(5.8480, dtype=oneflow.float32) >>> LA.norm(a, -3) tensor(0., dtype=oneflow.float32) >>> c = flow.tensor([[1., 2., 3.], ... [-1, 1, 4]]) >>> LA.norm(c, dim=0) tensor([1.4142, 2.2361, 5.0000], dtype=oneflow.float32) >>> LA.norm(c, dim=1, keepdim = True) tensor([[3.7417], [4.2426]], dtype=oneflow.float32) >>> LA.norm(c, ord=1, dim=1) tensor([6., 6.], dtype=oneflow.float32)