Create PyTorch Tensor with Data Types: An Introduction – PyTorch Tutorial

By | April 21, 2022

PyTorch supports to create tensors with different data types. In this tutorial, we will introduce you how to do.

Simple Guide to Create a Tensor in PyTorch – PyTorch Tutorial

4 Methods to Create a PyTorch Tensor – PyTorch Tutorial

PyTorch data types

Torch defines 10 tensor types with CPU and GPU variants, here we will list some common used:

Data type dtype CPU tensor GPU tensor
32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor
16-bit integer (signed) torch.int16 or torch.short torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.LongTensor torch.cuda.LongTensor
Boolean torch.bool torch.BoolTensor torch.cuda.BoolTensor

NumPy data types vs PyTorch data types

We also can create a tensor using numpy, it means we should know what numpy data can conver to which tensor type. Here is a list.

NumPy data type Tensor data type
numpy.uint8 torch.ByteTensor
numpy.int16 torch.ShortTensor
numpy.int32 torch.IntTensor
numpy.int torch.LongTensor
numpy.int64 torch.LongTensor
numpy.float32 torch.FloatTensor
numpy.float torch.DoubleTensor
numpy.float64 torch.DoubleTensor

How to create torch tensors with different data types?

In pytorch, we can set a data type when creating a tensor.  Here are some examples.

Example 1: create a float 32 tensor

import torch

p = torch.tensor([2, 3], dtype = torch.float32)

print(p)
print(p.dtype)

Run this code, we will see:

tensor([2., 3.])
torch.float32

Here we use dype = torch.float32 to set tensor p data type.

Of course, we also can use torch.FloatTensor to create a float32 data.

import torch

p = torch.FloatTensor([2, 3])

print(p)
print(p.dtype)

Run this code, we also can see:

tensor([2., 3.])
torch.float32

Example 2: create a long integer tensor

As to create a float32 tensor, we can create a long integer tensor as follows:

import torch

x = torch.LongTensor([2, 3, 4])
y = torch.tensor([2, 3, 4], dtype=torch.long)

print(x)
print(x.dtype)

print(y)
print(y.dtype)

Run code, we will see:

tensor([2, 3, 4])
torch.int64
tensor([2, 3, 4])
torch.int64

Leave a Reply