How to Check the Data Type of a Tensor in PyTorch
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
tensor.dtype
| Part | What it means |
|---|---|
tensor | Any tensor |
.dtype | The 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 type | Also called | Holds |
|---|---|---|
torch.float32 | torch.float | Decimal numbers in 32 bits |
torch.int64 | torch.long | Whole numbers in 64 bits |
torch.bool | True 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 numpyadds 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
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)

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
uv run python tensor_dtype.py
torch.int64
torch.float32
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
| Mistake | What happens | Do this instead |
|---|---|---|
decimals.dtype == "float32" | It printed False: a data type isn't text | decimals.dtype == torch.float32, which printed True |
decimals.dtype() with brackets | TypeError: 'torch.dtype' object is not callable | decimals.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 != float | Give 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
- Tensor attributes — every data type PyTorch has, and how they combine.
- torch.tensor — making a tensor, with its
dtypeoption. - Tensors tutorial — PyTorch's introduction to tensors.
