How to Get the Data Type of a Pytorch Tensor?
Last Updated :
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.
Vector:
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
Creation of One-Dimensional Tensors:
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
import torch
a = torch.tensor([ 10 , 20 , 30 , 40 , 50 ])
print (a)
b = 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])
Supported Data Types:
The following data types are supported by vector:
Data Type |
Description |
int8 |
Integer type with 8 bytes |
uint8 |
Unsigned integer type with 8 bytes |
int16 |
Integer type with 16 bytes |
int32 |
Integer type with 32 bytes |
int64 |
Integer type with 64 bytes |
float |
Data with float type(decimal) |
double |
Data with float type (64 bit) decimal |
bool |
Boolean type: returns True if the value is greater than 0, otherwise False |
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 torch
a = torch.tensor([ 100 , 200 , 2 , 3 , 4 ], dtype = torch.uint8)
print (a)
print (a.dtype)
a = torch.tensor([ 1 , 2 , - 6 , - 8 , 0 ], dtype = torch.int8)
print (a)
print (a.dtype)
a = torch.tensor([ 1 , 2 , - 6 , - 8 , 0 ], dtype = torch.int16)
print (a)
print (a.dtype)
a = torch.tensor([ 1 , 2 , - 6 , - 8 , 0 ], dtype = torch.int32)
print (a)
print (a.dtype)
a = torch.tensor([ 1 , 2 , - 6 , - 8 , 0 ], dtype = torch.int64)
print (a)
print (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 torch
a = torch.tensor([ 100 , 200 , 2 , 3 , 4 ], dtype = torch. float )
print (a)
print (a.dtype)
a = torch.tensor([ 1 , 2 , - 6 , - 8 , 0 ], dtype = torch.double)
print (a)
print (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 torch
a = torch.tensor([ 100 , 200 , 2 , 3 , 4 ], dtype = torch. bool )
print (a)
print (a.dtype)
a = torch.tensor([ 0 , 0 , 0 , 1 , 2 ], dtype = torch. bool )
print (a)
print (a.dtype)
|
Output:
tensor([True, True, True, True, True])
torch.bool
tensor([False, False, False, True, True])
torch.bool
Share your thoughts in the comments
Please Login to comment...