# 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

 `# importing torch module``import` `torch`` ` `# create one dimensional tensor with``# integer type elements``a ``=` `torch.tensor([``10``, ``20``, ``30``, ``40``, ``50``])``print``(a)`` ` `# create one dimensional tensor with ``# float type elements``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:

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``import` `torch`` ` ` ` `# create a tensor with unsigned integer type of 8 bytes size``a ``=` `torch.tensor([``100``, ``200``, ``2``, ``3``, ``4``], dtype``=``torch.uint8)``# display tensor``print``(a)``# display data type``print``(a.dtype)`` ` `# create a tensor with  integer type of 8 bytes size``a ``=` `torch.tensor([``1``, ``2``, ``-``6``, ``-``8``, ``0``], dtype``=``torch.int8)`` ` `# display tensor``print``(a)`` ` `# display data type``print``(a.dtype)`` ` `# create a tensor with  integer type of 16 bytes size``a ``=` `torch.tensor([``1``, ``2``, ``-``6``, ``-``8``, ``0``], dtype``=``torch.int16)`` ` `# display tensor``print``(a)`` ` `# display data type``print``(a.dtype)`` ` ` ` `# create a tensor with  integer type of 32 bytes size``a ``=` `torch.tensor([``1``, ``2``, ``-``6``, ``-``8``, ``0``], dtype``=``torch.int32)`` ` `# display tensor``print``(a)`` ` `# display data type``print``(a.dtype)`` ` `# create a tensor with  integer type of 64 bytes size``a ``=` `torch.tensor([``1``, ``2``, ``-``6``, ``-``8``, ``0``], dtype``=``torch.int64)`` ` `# display tensor``print``(a)`` ` `# display data type``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``import` `torch`` ` ` ` `# create a tensor with  float type``a ``=` `torch.tensor([``100``, ``200``, ``2``, ``3``, ``4``], dtype``=``torch.``float``)`` ` `# display tensor``print``(a)`` ` `# display data type``print``(a.dtype)`` ` `# create a tensor with  double type``a ``=` `torch.tensor([``1``, ``2``, ``-``6``, ``-``8``, ``0``], dtype``=``torch.double)`` ` `# display tensor``print``(a)`` ` `# display data type``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``import` `torch`` ` ` ` `# create a tensor with  bool type``a ``=` `torch.tensor([``100``, ``200``, ``2``, ``3``, ``4``], dtype``=``torch.``bool``)`` ` `# display tensor``print``(a)`` ` `# display data type``print``(a.dtype)`` ` `# create a tensor with  bool type``a ``=` `torch.tensor([``0``, ``0``, ``0``, ``1``, ``2``], dtype``=``torch.``bool``)`` ` `# display tensor``print``(a)`` ` `# display data type``print``(a.dtype)`

Output:

```tensor([True, True, True, True, True])
torch.bool
tensor([False, False, False,  True,  True])
torch.bool```

My Personal Notes arrow_drop_up