oneflow.linalg.vector_norm(input, ord=2, dim=None, keepdim=False, *, dtype=None, out=None)Tensor

Computes a vector norm.

Supports input of float, double dtypes.

This function does not necessarily treat multidimensonal attr:input as a batch of vectors, instead:

  • If dim= None, input will be flattened before the norm is computed.

  • If dim is an int or a tuple, the norm will be computed over these dimensions and the other dimensions will be treated as batch dimensions.

This behavior is for consistency with flow.linalg.norm().

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


vector norm

2 (default)

2-norm (see below)






sum(x != 0)

other int or float

sum(abs(x)^{ord})^{(1 / ord)}

where inf refers to float(‘inf’), NumPy’s inf object, or any equivalent object.

  • input (Tensor) – tensor, flattened by default, but this behavior can be controlled using dim.

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

  • dim (int, Tuple[int], optional) – dimensions over which to compute the norm. See above for the behavior when dim= None. Default: None

  • keepdim (bool, optional) – If set to True, the reduced dimensions are retained in the result as dimensions with size one. Default: False


A real-valued tensor.


>>> 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.vector_norm(a, ord=3.5)
tensor(5.4345, dtype=oneflow.float32)
>>> LA.vector_norm(b, ord=3.5)
tensor(5.4345, dtype=oneflow.float32)