Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.norm

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.

How it works

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.

Syntax

torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None)

Parameters

NameTypeDescription
inputTensorInput tensor; it must have a floating-point or complex dtype.
pint, float, inf, -inf, 'fro', or 'nuc'; optionalNorm order. It may be an int, float, positive or negative infinity, 'fro', or 'nuc'; default is 'fro'.
dimint, tuple of ints, or list of ints; optionalDimension or dimensions across which to calculate the norm; if None, all dimensions are reduced.
keepdimbool; optionalWhether reduced dimensions are retained in the output.
outTensor; optionalOptional destination tensor for the result.
dtypetorch.dtype; optionalDesired dtype of the returned tensor; the input is cast to it during the operation.

Returns

A tensor containing the requested matrix or vector norm, with shape determined by dim and keepdim.

Key points

  • The function supports both matrix-norm and vector-norm calculations.
  • The default norm order is 'fro', and the documentation lists Frobenius, nuclear, and numeric-order cases.
  • Vector norms may reduce across multiple dimensions, not only one axis.
  • Use dim to choose the axis or axes included in the reduction.
  • keepdim determines whether reduced dimensions are retained in the output shape.
  • Supplying dtype casts the input during computation and controls the returned tensor's desired dtype.

Common mistakes

  • This function is deprecated and may be removed in a future PyTorch release; prefer torch.linalg.vector_norm for vector norms or torch.linalg.matrix_norm for matrix norms.
  • The input tensor must use a floating-point or complex dtype.
  • Complex inputs contribute through the absolute value of each element.
  • Leaving dim unset reduces across every input dimension, rather than selecting a particular axis.

Example: Whole-tensor and row norms

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.

When to use it

Whole-tensor norm

Compute one magnitude for an entire tensor or matrix.

Per-dimension vector norms

Measure the magnitude of each row, column, or selected slice.

Matrix norms

Calculate Frobenius or nuclear norms for matrix-oriented operations.

Controlled-precision computation

Perform norm computation in a chosen numeric dtype.

Interview questions

QWhat does torch.norm compute, and what does p control?

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.

QHow does dim affect the result?

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.

QWhat do dtype and keepdim do?

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_norm
  • torch.linalg.matrix_norm
  • torch.linalg.norm
  • torch.norm_except_dim

Source: torch.norm — PyTorch 2.12 documentation. Checked 2026-10-16.