PyTorch torch.linalg.matrix_rank Function
PyTorch torch Reference Manual
torch.linalg.matrix_rankIt is a function in PyTorch's linear algebra module used to compute the rank of a matrix. The rank of a matrix represents the number of linearly independent rows or columns in the matrix.
Function Definition
torch.linalg.matrix_rank(A, tol=None, hermitian=False, out=None)
Parameters:
A(Tensor): The input matrix.tol(float, optional): Singular value threshold.hermitian(bool, optional): If True, the matrix is assumed to be Hermitian (symmetric). Defaults to False.out(Tensor, optional): Output tensor.
Return Value:
torch.Tensor: Returns the rank of the matrix.
Usage Example
Example
import torch
# Create a full-rank matrix
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# Compute the rank of the matrix
rank = torch.linalg.matrix_rank(A)
print("Matrix A:")
print(A)
print("Rank of the matrix:", rank)
# Create a full-rank matrix
A = torch.tensor([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# Compute the rank of the matrix
rank = torch.linalg.matrix_rank(A)
print("Matrix A:")
print(A)
print("Rank of the matrix:", rank)
The output is:
矩阵 A:
tensor([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
矩阵的秩: tensor(2)
Example - Full-rank Matrix
import torch
# Create a full-rank matrix
B = torch.tensor([[1.0, 2.0],
[3.0, 4.0]])
rank_B = torch.linalg.matrix_rank(B)
print("Matrix B:")
print(B)
print("Rank of the matrix:", rank_B)
# Create a full-rank matrix
B = torch.tensor([[1.0, 2.0],
[3.0, 4.0]])
rank_B = torch.linalg.matrix_rank(B)
print("Matrix B:")
print(B)
print("Rank of the matrix:", rank_B)
Other Extensions