PyTorch Get the shape of a tensor

The shape of a tensor signifies the number of elements (length) in each of the axes of a tensor. The length (number of elements) of each of the axes or shape of a tensor can be fetched like this:

shape_of_tensor = list(my_tensor.shape)

Complete code:

import torch

my_data = [[0.7213, 0.1766, 0.6062, 0.3769,  0.7579, 0.4698],
           [0.3332, 0.9581, 0.7579, 0.4698, 0.6268, 0.1420]]

my_tensor = torch.Tensor(my_data)
shape_of_tensor = list(my_tensor.shape)

print("Shape of tensor:", shape_of_tensor)


Shape of tensor: [2, 6]

Above, the tensor in the example is of shape [2, 6] i.e it has two items on the first axis and six on second axis.