PyTorch Get the rank of a tensor

The rank of a tensor signifies the number of dimensions present within the tensor. We can use the following code to get the rank of a tensor:

torch.linalg.matrix_rank(my_tensor)

Complete code:

import torch

my_data = [[0.1051, 0.0710],
           [0.5582, 0.7901],
           [0.2262, 0.2459]]

my_tensor = torch.Tensor(my_data)

rank_tensor = torch.linalg.matrix_rank(my_tensor)

print("Rank of tensor: ", rank_tensor)

Output:

Rank of tensor:  tensor(2)

Above, the tensor in the example is of rank 2.