Skip to main content

How to Check the Data Type of a Tensor in PyTorch

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Every value in a PyTorch tensor has the same data type, and many operations refuse to mix two of them. When you make a tensor from a list, PyTorch picks that type for you. This guide reads it with .dtype for three kinds of values in PyTorch 2.15.0.

Syntax​

Syntax
tensor.dtype
PartWhat it means
tensorAny tensor
.dtypeThe data type of its values, such as torch.float32. It's an attribute: no brackets after it

The data types you'll meet first, from PyTorch's list of data types:

Data typeAlso calledHolds
torch.float32torch.floatDecimal numbers in 32 bits
torch.int64torch.longWhole numbers in 64 bits
torch.boolTrue or False

The Example​

A script makes three tensors from lists, one of whole numbers, one of decimals and one of True/False answers, and prints the data type PyTorch gave each.

Prerequisites​

  • A uv project with PyTorch and NumPy. uv add torch numpy adds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.

Step 1: Write the Script​

tensor_dtype.py
import torch

whole_numbers = torch.tensor([1, 2, 3])
decimals = torch.tensor([1.5, 2.5, 3.5])
answers = torch.tensor([True, False, True])

print(whole_numbers.dtype)
print(decimals.dtype)
print(answers.dtype)

The tensor_dtype.py script in a macOS-style code window: three tensors made from whole numbers, decimals and True/False values, then print of each one's dtype.

torch.tensor makes each tensor from a list, without being told a data type, so PyTorch chooses one from the values.

Step 2: Run It​

Terminal
uv run python tensor_dtype.py
Output
torch.int64
torch.float32
torch.bool

The terminal after uv run python tensor_dtype.py: torch.int64, torch.float32 and torch.bool.

Whole numbers became torch.int64, decimals torch.float32 and True/False values torch.bool. torch.float32 is PyTorch's default for decimals: torch.get_default_dtype() printed torch.float32 too. It matters, because neural networks compute in decimals. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
decimals.dtype == "float32"It printed False: a data type isn't textdecimals.dtype == torch.float32, which printed True
decimals.dtype() with bracketsTypeError: 'torch.dtype' object is not callabledecimals.dtype
Multiplying a matrix of whole numbers by one of decimals with @RuntimeError: expected m1 and m2 to have the same dtype, but got: long long != floatGive both the same data type first

FAQ​

How do I change a tensor's data type?​

With .to(): whole_numbers.to(torch.float32) printed tensor([1., 2., 3.]). It returns a new tensor, so keep the result.

Why did dividing whole numbers give decimals?​

Because / always divides into decimals: whole_numbers / 2 printed tensor([0.5000, 1.0000, 1.5000]).

Conclusion​

.dtype showed torch.int64 for whole numbers, torch.float32 for decimals and torch.bool for True/False values, the types PyTorch 2.15.0 picked from each list.

Additional Resources​