PyTorch Tensor Data Types
A dtype names the kind of value in each cell of a tensor, like whole numbers or decimals. Every tensor has exactly one dtype.
Whole numbers become int64
A list of whole numbers gets the dtype int64.
import torch
print(torch.tensor([1, 2, 3]).dtype)Decimals become float32
A list of decimal numbers gets the dtype float32. Float is a common short name for a decimal number.
print(torch.tensor([1.0, 2.0, 3.0]).dtype)Choosing a dtype
The dtype argument picks the kind directly. Here the whole numbers become decimals.
x = torch.tensor([1, 2, 3], dtype=torch.float32)
print(x)True and false values
The bool dtype holds True or False in each cell. It suits answers to yes-or-no questions.
flags = torch.tensor([True, False, True])
print(flags)
print(flags.dtype)Example
import torch
data = [0, 1, 2]
ints = torch.tensor(data)
floats = torch.tensor(data, dtype=torch.float32)
flags = torch.tensor(data, dtype=torch.bool)
print(ints, ints.dtype)
print(floats, floats.dtype)
print(flags, flags.dtype)Key points
- Dtype: the kind of value stored in each cell of a tensor
- int64: the dtype a list of whole numbers gets by default
- Default float: a list with decimals gets float32 with no dtype argument
- Use the dtype argument to choose the kind at build time
- bool: True or False values, one per cell
In this example, one list of whole numbers feeds three tensors. The first tensor keeps the default int64 dtype. The second tensor uses the dtype argument to become float32. The third tensor becomes bool. In a bool tensor, zero becomes False. Each nonzero number becomes True. The print lines show each tensor beside its dtype.
Note
Writing 1.0 makes a decimal number, while a plain 1 stays a whole number.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Python Playground
Exercise
Which dtype holds True and False values?