How to Get the Data Type of a Pytorch Tensor? - GeeksforGeeks

21 Jul, 2021
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.
A vector is a one-dimensional tensor that holds elements of multiple data types. We can create a vector using PyTorch. Pytorch is available in the Python torch module so, we need to import it
Syntax:
import pytorch
One dimensional vector is created using the torch.tensor() method.
Syntax:
torch.tensor([element1,element2,.,element n],dtype)
Parameters:
dtype: Specify the data type.
dtype=torch.datatype
Example: Python program to create tensor elements not specifying the data type.
Python3
# importing torch moduleimport torch  # create one dimensional tensor with# integer type elementsa = torch.tensor([10, 20, 30, 40, 50])print(a)  # create one dimensional tensor with # float type elementsb = torch.tensor([10.12, 20.56, 30.00, 40.3, 50.4])print(b)
Output:
tensor([10, 20, 30, 40, 50])
tensor([10.1200, 20.5600, 30.0000, 40.3000, 50.4000])
The following data types are supported by vector:
We can get the data type by using dtype command:
Syntax:
tensor_name.dtype
Example 1: Python program to create tensor with integer data types and display data type
Python3
# import torchimport torch    # create a tensor with unsigned integer type of 8 bytes sizea = torch.tensor([100, 200, 2, 3, 4], dtype=torch.uint8)# display tensorprint(a)# display data typeprint(a.dtype)  # create a tensor with  integer type of 8 bytes sizea = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int8)  # display tensorprint(a)  # display data typeprint(a.dtype)  # create a tensor with  integer type of 16 bytes sizea = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int16)  # display tensorprint(a)  # display data typeprint(a.dtype)    # create a tensor with  integer type of 32 bytes sizea = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int32)  # display tensorprint(a)  # display data typeprint(a.dtype)  # create a tensor with  integer type of 64 bytes sizea = torch.tensor([1, 2, -6, -8, 0], dtype=torch.int64)  # display tensorprint(a)  # display data typeprint(a.dtype)
Output:
tensor([100, 200,   2,   3,   4], dtype=torch.uint8)
torch.uint8
tensor([ 1,  2, -6, -8,  0], dtype=torch.int8)
torch.int8
tensor([ 1,  2, -6, -8,  0], dtype=torch.int16)
torch.int16
tensor([ 1,  2, -6, -8,  0], dtype=torch.int32)
torch.int32
tensor([ 1,  2, -6, -8,  0])
torch.int64
Example 2: Create float type and display data types.
Python3
# import torchimport torch    # create a tensor with  float typea = torch.tensor([100, 200, 2, 3, 4], dtype=torch.float)  # display tensorprint(a)  # display data typeprint(a.dtype)  # create a tensor with  double typea = torch.tensor([1, 2, -6, -8, 0], dtype=torch.double)  # display tensorprint(a)  # display data typeprint(a.dtype)
Output:
tensor([100., 200.,   2.,   3.,   4.])
torch.float32
tensor([ 1.,  2., -6., -8.,  0.], dtype=torch.float64)
torch.float64
Example 3: Create a tensor with boolean type
Python3
# import torchimport torch    # create a tensor with  bool typea = torch.tensor([100, 200, 2, 3, 4], dtype=torch.bool)  # display tensorprint(a)  # display data typeprint(a.dtype)  # create a tensor with  bool typea = torch.tensor([0, 0, 0, 1, 2], dtype=torch.bool)  # display tensorprint(a)  # display data typeprint(a.dtype)
Output:
tensor([True, True, True, True, True])
torch.bool
tensor([False, False, False,  True,  True])
torch.bool
Picked
Python-PyTorch
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Pandas dataframe.groupby()
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list