Whole-tensor norm
Compute one magnitude for an entire tensor or matrix.
PyTorch · Function
torch.norm computes a matrix or vector norm of a tensor, with configurable order, reduction dimensions, shape retention, output destination, and computation dtype. It is deprecated; new code should use the corresponding torch.linalg norm function.
torch.norm computes a matrix or vector norm for a tensor. It selects the norm order with p, reduces over dimensions chosen by dim, can preserve reduced dimensions with keepdim, optionally writes to out, and can cast during computation with dtype.
torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None)| Name | Type | Description |
|---|---|---|
input | Tensor | Input tensor; it must have a floating-point or complex dtype. |
p | int, float, inf, -inf, 'fro', or 'nuc'; optional | Norm order. It may be an int, float, positive or negative infinity, 'fro', or 'nuc'; default is 'fro'. |
dim | int, tuple of ints, or list of ints; optional | Dimension or dimensions across which to calculate the norm; if None, all dimensions are reduced. |
keepdim | bool; optional | Whether reduced dimensions are retained in the output. |
out | Tensor; optional | Optional destination tensor for the result. |
dtype | torch.dtype; optional | Desired dtype of the returned tensor; the input is cast to it during the operation. |
A tensor containing the requested matrix or vector norm, with shape determined by dim and keepdim.
import torch
x = torch.tensor([[3.0, 4.0], [5.0, 12.0]])
print(torch.norm(x))
print(torch.norm(x, p=2, dim=1))Computes the Frobenius norm of the whole matrix, then the Euclidean norm independently for each row.
Compute one magnitude for an entire tensor or matrix.
Measure the magnitude of each row, column, or selected slice.
Calculate Frobenius or nuclear norms for matrix-oriented operations.
Perform norm computation in a chosen numeric dtype.
torch.norm returns a matrix norm or vector norm. With the default p='fro', it computes the Frobenius norm for matrix inputs; a numeric p selects a vector p-norm when dimensions are reduced as specified.
Follow-up: How should new code express the operation? Use torch.linalg.vector_norm for vector norms and torch.linalg.matrix_norm for matrix norms, because torch.norm is deprecated.
dim identifies the dimension or dimensions over which the norm is calculated. If dim is None, all input dimensions are reduced.
Follow-up: What happens when multiple dimensions are supplied? The vector norm can be calculated across any number of dimensions.
dtype specifies the desired dtype of the returned tensor. When provided, the input is cast to that dtype while the operation runs; keepdim controls whether reduced dimensions remain in the output.
Follow-up: What is the default for dtype? It is None, so no dtype conversion is requested by this argument.
torch.linalg.vector_normtorch.linalg.matrix_normtorch.linalg.normtorch.norm_except_dim