Python PyTorch linalg.svd() method
Last Updated :
28 Feb, 2022
PyTorch linalg.svd() method computes the singular value decomposition (SVD) of a matrix. 2D tensors are matrices in PyTorch. This method supports both real and complex-valued matrices (float, double, cfloat, and cdouble dtypes). It takes input a matrix or a batch of matrices and returns decomposition as a named tuple (U, S, VT). I have listed some properties of U, S, and VT as below-
- If input matrix A is a real-valued matrix, both U and VT are orthogonal.
- If input matrix A is a complex-valued matrix, U and VT are unitary.
- S is always real-valued for both real or complex-valued input matrix A. The singular values (elements of S) are arranged in descending order.
Following is the syntax-
Syntax: torch.linalg.svd(A, full_matrices=True, *, out=None)
Parameters:
- A: the input matrix (tensor or batch of tensors).
- full_matrices: an optional boolean value. If True full SVD is computed. If False, reduced SVD is computed.
- out: the output tuple of three tensors.
Return: It returns three tensors as a named tuple (U, S, VT).
Let’s understand the torch.linalg.svd() method with the help of some Python program examples.
Example 1:
In this example, we compute the full singular value decomposition of a real-valued matrix using torch.linalg.svd(). Here, the U and VT matrices are square matrices, and the size of S is min(2,3). Matrices U and VT are orthogonal. You can check the orthogonality of these matrices using [email protected]() and [email protected]().
Python3
import torch
Mat = torch.tensor([[ 1. , 2. , 3. ],
[ 3. , - 2 , - 7 ]])
print ( "Matrix:\n" , Mat)
U, S, VT = torch.linalg.svd(Mat)
print ( "U:\n" ,U)
print ( "Singular Values (S):\n" ,S)
print ( "VT:\n" ,VT)
print ( "Shape of U, S and VT:\n" , U.shape, S.shape, VT.shape)
|
Output:
Matrix:
tensor([[ 1., 2., 3.],
[ 3., -2., -7.]])
U:
tensor([[-0.3625, -0.9320],
[ 0.9320, -0.3625]])
Singular Values (S):
tensor([8.3999, 2.3329])
VT:
tensor([[ 0.2897, -0.3082, -0.9061],
[-0.8657, -0.4882, -0.1107],
[ 0.4082, -0.8165, 0.4082]])
Shape of U, S and VT:
torch.Size([2, 2]) torch.Size([2]) torch.Size([3, 3])
Example 2:
In the example below we compute the reduced singular value decomposition of a real-valued matrix using torch.linalg.svd(). Here, the reduced SVD is computed. Notice that U is square and VT is not, and the size of S is min(3,4). As the input matrix is real-valued matrices U and VT should be orthogonal. You can check the orthogonality of these matrices using [email protected]() and [email protected](). What if the input matrix Mat in the above code is a 4×3 matrix? Yes!!!. VT is square and U is not.
Python3
import torch
Mat = torch.rand( 3 , 4 )
print ( "Matrix:\n" , Mat)
U, S, VT = torch.linalg.svd(Mat, full_matrices = False )
print ( "U:\n" , U)
print ( "Singular Values (S):\n" , S)
print ( "VT:\n" , VT)
print ( "Shape of U, S and VT:\n" , U.shape, S.shape, VT.shape)
|
Output:
Please note that as we are creating an input matrix using a random number generator, you may get a different matrix from the output-
Matrix:
tensor([[0.5280, 0.3108, 0.1215, 0.8151],
[0.9640, 0.4199, 0.3913, 0.2239],
[0.7886, 0.0702, 0.7123, 0.6120]])
U:
tensor([[-0.4984, -0.7911, -0.3546],
[-0.5810, 0.6083, -0.5407],
[-0.6435, 0.0634, 0.7628]])
Singular Values (S):
tensor([1.8411, 0.5512, 0.4225])
VT:
tensor([[-0.7227, -0.2412, -0.4053, -0.5052],
[ 0.3969, 0.0254, 0.3395, -0.8524],
[-0.2531, -0.6715, 0.6834, 0.1343]])
Shape of U, S and VT:
torch.Size([3, 3]) torch.Size([3]) torch.Size([3, 4])
Example 3:
In the example below we compute the full SVD of a complex-valued matrix. Notice that the S is real-valued and U and VT are complex-valued.
Python3
import torch
Mat = torch.randn( 3 , 2 , dtype = torch.cfloat)
print ( "Matrix:\n" , Mat)
U, S, VT = torch.linalg.svd(Mat)
print ( "U:\n" , U)
print ( "Singular Values (S):\n" , S)
print ( "VT:\n" , VT)
print ( "Shape of U, S and VT:\n" , U.shape, S.shape, VT.shape)
|
Output:
Please note that as we are creating an input matrix using a random number generator, you may get a different matrix from the output below
Matrix:
tensor([[-0.4095-0.8878j, 0.3983+0.5446j],
[-1.3408+1.1268j, 0.3193+0.9775j],
[ 0.8876+0.6970j, -0.9217-0.5416j]])
U:
tensor([[-0.2687-0.3263j, -0.3146+0.2721j, 0.7890+0.1605j],
[-0.6525+0.3324j, -0.1834-0.6499j, 0.0518+0.0713j],
[ 0.5008+0.1853j, 0.3777-0.4778j, 0.5800-0.0863j]])
Singular Values (S):
tensor([2.4929, 1.3183])
VT:
tensor([[ 0.8916+0.0000j, -0.2928-0.3453j],
[-0.4527+0.0000j, -0.5767-0.6800j]])
Shape of U, S and VT:
torch.Size([3, 3]) torch.Size([2]) torch.Size([2, 2])
Similar Reads
Python PyTorch - linalg.norm() method
PyTorch linalg.norm() method computes a vector or matrix norm. Norm is always a non-negative real number which is a measure of the magnitude of the matrix. It accepts a vector or matrix or batch of matrices as the input. It supports inputs of only float, double, cfloat, and cdouble dtypes. We will b
5 min read
Python Pytorch linspace() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.linspace() returns a one-dimensional tensor of steps equally spaced points between start and end. The output tensor is 1-D of size
2 min read
Python - PyTorch log() method
PyTorch torch.log() method gives a new tensor having the natural logarithm of the elements of input tensor. Syntax: torch.log(input, out=None) Arguments input: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see this concept with the help of few examples: Example 1:
1 min read
Python PyTorch log2() method
PyTorch log2() method computes the logarithm to the base 2 of the elements of an input tensor. It computes the logarithm values element-wise. It takes a tensor as an input and returns a new tensor with computed logarithm values. The elements of the input tensor must be between zero and the positive
4 min read
Python | PyTorch asin() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.The function torch.asin() provides support for the inverse sine function in PyTorch. It expects the input to be in the range [-1, 1] and gives the out
2 min read
Python - PyTorch ceil() method
PyTorch torch.ceil() method returns a new tensor having the ceil value of the elements of input, Which is the smallest integer larger than or equal to each element. Syntax: torch.ceil(inp, out=None) Arguments inp: This is input tensor. out: The output tensor. Return: It returns a Tensor. Let's see t
1 min read
Python - PyTorch div() method
PyTorch torch.div() method divides every element of the input with a constant and returns a new modified tensor. Syntax: torch.div(inp, other, out=None) Arguments inp: This is input tensor. other: This is a number to be divided to each element of input inp. out: The output tensor. Return: It returns
1 min read
Python | PyTorch sin() method
PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes. The function torch.sin() provides support for the sine function in PyTorch. It expects the input in radian form and the output is in the range [-1, 1
2 min read
Python - PyTorch abs() method
PyTorch torch.abs() method computes the element-wise absolute value of the given input tensor. Syntax: torch.abs(inp, out=None) ? Tensor Arguments inp: This is input tensor. out: This is optional parameter and is the output tensor. Return: It returns a Tensor having absolute value of input inp. Let'
1 min read
Python - PyTorch add() method
PyTorch torch.add() method adds a constant value to each element of the input tensor and returns a new modified tensor. Syntax: torch.add(inp, c, out=None) Arguments inp: This is input tensor. c: The value that is to be added to every element of tensor. out: This is optional parameter and it is the
1 min read