How to access the metadata of a tensor in PyTorch?
Last Updated :
01 Nov, 2022
In this article, we are going to see how to access the metadata of a tensor in PyTorch using Python.
PyTorch in Python is a machine learning library. Also, it is free and open-source. It was firstly introduced by the Facebook AI research team. A tensor in PyTorch is similar to a NumPy array. But it doesn't have any knowledge of deep learning, graphs, etc. It is considered a normal n-dimensional array that can be used for mathematical computation. It differs from a Numpy in terms of its execution platform. Unlike Numpy array, a tensor is capable of running on CPU or GPU.
Example 1:
In this example, each of the statements used in the source code has been discussed below,
The first step is to import the torch library. We need to create a tensor. For example, we have created a tensor of dimension 5 X 3. Now to access metadata that is, the size and shape of the tensor we have used the .size() and .shape method. We have used the torch.numel() method. It gives us the total number of elements in the created tensor. Finally, we are printing these data to the console.
Python3
# Python Program demonstrate how
# to access meta-data of a Tensor
# Import necessary libraries
import torch
# Create a tensor of having dimensions 5 X 3
tensor = torch.Tensor([[5,1,7],[7,2,9],[4,7,9],
[8,12,14],[2,4,7]])
print("tensor:\n", tensor)
# Get the meta-data of tensor
# Get the size of tensor
tensor_size = tensor.size()
print("The size of tensor:\n", tensor_size)
# Applying .shape method to get the tensor size
print("The shape of tensor:\n", tensor.shape)
# Compute the number of elements in the tensor
size = torch.numel(tensor)
print("Total number of elements in tensor:\n", size)
Output:
tensor:
tensor([[ 5., 1., 7.],
[ 7., 2., 9.],
[ 4., 7., 9.],
[ 8., 12., 14.],
[ 2., 4., 7.]])
The size of tensor:
torch.Size([5, 3])
The shape of tensor:
torch.Size([5, 3])
Total number of elements in tensor:
15
Example 2:
In this example, each of the statements used in the source code has been discussed below,
The first step is to import the torch library. We need to create a tensor. For example, we have created a tensor of numbers from 1 to 9 (inclusive). Now to access metadata that is, the size and shape of the tensor we have used the .size() and .shape method. We have used the torch.numel() method. It gives us the total number of elements in the created tensor. Finally, we are printing these data to the console.
Python3
# Python Program demonstrate how to
# access meta-data of a Tensor
# Import the library
import torch
# Creating a tensor
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
tensor = torch.tensor(data)
# Printing tensor
print(tensor)
# Get the meta-data of tensor
# Get the size of tensor
tensor_size = tensor.size()
print("The size of tensor:\n", tensor_size)
# Applying .shape method to get the tensor size
print("The shape of tensor:\n", tensor.shape)
# Compute the number of elements in the tensor
size = torch.numel(tensor)
print("Total number of elements in tensor:\n", size)
Output:
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
The size of tensor:
torch.Size([9])
The shape of tensor:
torch.Size([9])
Total number of elements in tensor:
9
Similar Reads
How to access and modify the values of a Tensor in PyTorch?
In this article, we are going to see how to access and modify the value of a tensor in PyTorch using Python. We can access the value of a tensor by using indexing and slicing. Indexing is used to access a single value in the tensor. slicing is used to access the sequence of values in a tensor. we ca
2 min read
How to Get the Value of a Tensor in PyTorch
When working with PyTorch, a powerful and flexible deep learning framework, you often need to access and manipulate the values stored within tensors. Tensors are the core data structures in PyTorch, representing multi-dimensional arrays that can store various types of data, including scalars, vector
5 min read
How To Sort The Elements of a Tensor in PyTorch?
In this article, we are going to see how to sort the elements of a PyTorch Tensor in Python. To sort the elements of a PyTorch tensor, we use torch.sort() method. Â We can sort the elements along with columns or rows when the tensor is 2-dimensional. Syntax: torch.sort(input, dim=- 1, descending=Fals
3 min read
How to get the rank of a matrix in PyTorch
In this article, we are going to discuss how to get the rank of a matrix in PyTorch. we can get the rank of a matrix by using torch.linalg.matrix_rank() method.torch.linalg.matrix_rank() methodmatrix_rank() method accepts a matrix and a batch of matrices as the input. This method returns a new tenso
2 min read
How to Get the Shape of a Tensor as a List of int in Pytorch?
To get the shape of a tensor as a list in PyTorch, we can use two approaches. One using the size() method and another by using the shape attribute of a tensor in PyTorch. In this short article, we are going to see how to use both of the approaches. Using size() method: The size() method returns the
1 min read
How to Get the Data Type of a Pytorch Tensor?
In this article, we are going to create a tensor and get the data type. The Pytorch is used to process the tensors. Tensors are multidimensional arrays. PyTorch accelerates the scientific computation of tensors as it has various inbuilt functions. Vector: A vector is a one-dimensional tensor that ho
3 min read
How to find the transpose of a tensor in PyTorch?
In this article, we are going to discuss how to find the transpose of the tensor in PyTorch. The transpose is obtained by changing the rows to columns and columns to rows. we can transpose a tensor by using transpose() method. the below syntax is used to find the transpose of the tensor. Syntax: tor
2 min read
How to compute the inverse of a square matrix in PyTorch
In this article, we are going to cover how to compute the inverse of a square matrix in PyTorch. torch.linalg.inv() method we can compute the inverse of the matrix by using torch.linalg.inv() method. It accepts a square matrix and a batch of the square matrices as input. If the input is a batch of
2 min read
How to Convert a TensorFlow Model to PyTorch?
The landscape of deep learning is rapidly evolving. While TensorFlow and PyTorch stand as two of the most prominent frameworks, each boasts its unique advantages and ecosystems. However, transitioning between these frameworks can be daunting, often requiring tedious reimplementation and adaptation o
6 min read
How to Correctly Access Elements in a 3D Pytorch Tensor?
In this article, we will discuss how to access elements in a 3D Tensor in Pytorch. PyTorch is an optimized tensor library majorly used for Deep Learning applications using GPUs and CPUs. It is one of the widely used Machine learning libraries, others being TensorFlow and Keras. The python supports t
4 min read