- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# PyTorch – How to compute the norm of a vector or matrix?

To compute the norm of a vector or a matrix, we could apply **torch.linalg.norm()** method. It returns a new tensor with computed norm. It accepts a vector, matrix, a batch of matrices and also batches of matrices.

A vector is a 1D torch Tensor where a matrix is a 2D torch Tensor. It supports input of **float, double, cfloat,** and **cdouble** data types. We can compute the norm of the matrix or batch/es of matrices along the different dimensions. For example, we could compute the norm of a matrix along dimension 0 or along dimension1.

## Syntax

torch.linalg.norm(A)

A is a vector, matrix or batch/s of matrices. A vector is a 1D torch tensor and a matrix is a 2D torch tensor.

## Steps

We could use the following steps to compute the norm of a vector or matrix −

Import the required library. In all the following examples, the required Python library is

**torch**. Make sure you have already installed it.

import torch

Define a vector or matrix. Here, we define matrix (2D tensor of size 3×3) of random numbers.

A = torch.randn(3,3)

Compute the norm of the vector or matrix using torch.linalg.norm(A). A is a vector or matrix or batch/s of matrices. Optionally assign this value to a new variable.

norm_A = torch.linalg.norm(A)

Print the computed norm

print("Norm:", norm_A)

## Example 1

In this program, we compute the norm of a vector.

# import required library import torch # create a vector/ 1D tensor v = torch.randn(3) # print the above created vector print("Vector:\n", v) # computet the norm of the vector n = torch.linalg.norm(v) print("Norm:\n", n)

## Output

Vector: tensor([-0.3792, -1.1512, 0.2590]) Norm: tensor(1.2394)

## Example 2

In this program, we compute the norm of a matrix.

# import required library import torch # create a 3x4 matrix mat = torch.randn(3,3) # print the above created matrix print("Matrix:\n", mat) # compute the norm of the matrix nor = torch.linalg.norm(mat) # print the computed determinants print("Norm:\n", nor)

## Output

Matrix: tensor([[ 0.2376, 0.5451, -0.2423], [-0.2320, -0.2493, 1.3164], [-0.0265, -0.9278, -0.8413]]) Norm: tensor(1.9572)

## Example 3

In this program, we compute the norm of a matrix along different dimensions.

# Python program to compute the norm of a matrix # import torch library import torch # create a 3x3 matrix mat = torch.tensor([[1.,2.,3.],[4.,5.,6.]]) # print the above created matrix print("Matrix:\n", mat) # compute the norm of the matrix in dim 0 nor0 = torch.linalg.norm(mat, dim = 0) # print the computed norm print("Norm in 0 dim:\n", nor0) # compute the norm of the matrix in dim 1 nor1 = torch.linalg.norm(mat, dim = 1) # print the computed norm print("Norm in 1 dim:\n", nor1)

## Output

Matrix: tensor([[1., 2., 3.], [4., 5., 6.]]) Norm in 0 dim: tensor([4.1231, 5.3852, 6.7082]) Norm in 1 dim: tensor([3.7417, 8.7750])

## Example 4

In this program, we compute the norm of a complex matrix.

# import required library import torch # create a 3x4 matrix mat = torch.randn(3,4, dtype = torch.cfloat) # print the above created matrix print("Matrix:\n", mat) # compute the norm of the matrix nor = torch.linalg.norm(mat) # print the computed norm print("Norm:\n", nor) # compute the norm of the matrix in dim 0 nor0 = torch.linalg.norm(mat, dim = 0) # print the computed norm print("Norm in 0 dim:\n", nor0) # compute the norm of the matrix in dim 1 nor1 = torch.linalg.norm(mat, dim = 1) # print the computed norm print("Norm in 1 dim:\n", nor1)

## Output

Matrix: tensor([[-0.2689+0.1693j, -1.5259-0.5821j, -0.2348-0.0016j, -0.9439+0.0868j], [-1.1065-0.5374j, 0.4492-0.0664j, 0.1469+1.0838j, -0.1163+0.2847j], [ 0.7928-1.0270j, 0.9414+1.0902j, 0.5717+0.9329j, -0.1108+0.2115j]]) Norm: tensor(3.4270) Norm in 0 dim: tensor([1.8159, 2.2244, 1.5648, 1.0247]) Norm in 1 dim: tensor([1.9292, 1.7350, 2.2388])

- Related Questions & Answers
- PyTorch – How to compute the pseudoinverse of a matrix?
- Return the Norm of the matrix or vector in Linear Algebra in Python
- PyTorch – How to compute the determinant of a square matrix?
- PyTorch – How to compute the inverse of a square matrix?
- PyTorch – How to compute QR decomposition of a matrix?
- PyTorch – How to compute the eigenvalues and eigenvectors of a square matrix?
- PyTorch – How to compute Singular Value Decomposition (SVD) of a matrix?
- Return the Norm of the matrix or vector in Linear Algebra and also set the order in Python
- Compute the condition number of a matrix in linear algebra using Infinity norm in Python
- Compute the condition number of a matrix in linear algebra using Frobenius norm in Python
- Compute the condition number of a matrix in linear algebra using 2 norm in Python
- How to compute the histogram of a tensor in PyTorch?
- How to shuffle columns or rows of matrix in PyTorch?
- Compute the condition number of a matrix in linear algebra using Negative Infinity norm in Python
- Compute the condition number of a matrix in linear algebra using negative 2 norm in Python