How to get the data type of a tensor in PyTorch?

Last updated Dec 31, 2021

In this article, we'll look at how to make tensors and get their data types. Tensors are processed using Pytorch. PyTorch is useful in GUIs because it contains a variety of built-in functions that enhance or improve the speed with which tensor computations are performed.

 

Getting data type of Tensor in a PyTorch

The PyTorch tensors are homogeneous in nature, with all members being of the same data type. We often utilize the tensor ".dtype" attribute to access the tensor data type. Normally, this property returns the tensor data type.

Simply follow the procedures below to get the PyTorch datatype:

·         Step 1: First, import all of the required libraries. Torch is one of the most crucial Python libraries required in this procedure, and you must guarantee that it is installed.

·         Step 2: Next, generate and show a tensor.

·         Step 3: Next, identify the dtype. Where d is the tensor type from which we want to derive the data type.

·         Step 4: Lastly, show the tensor data type.

 

Example

import torch

T = torch.randn(3,4)

print("Original Tensor T:\n", T)

data_type = T.dtype

print("Data type of tensor T:\n", data_type)

 

 

Output:

Tkinter Data Types

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

446 Views