0
0
PyTorchml~5 mins

Tensor data types in PyTorch

Choose your learning style9 modes available
Introduction
Tensors hold numbers in PyTorch. Choosing the right data type helps your model work well and fast.
When you want to save memory by using smaller number types.
When you need precise calculations using floating-point numbers.
When working with whole numbers like labels or counts.
When you want to convert data between types for compatibility.
When you want to speed up training by using faster data types.
Syntax
PyTorch
torch.tensor(data, dtype=torch.<data_type>)
Use like float32, int64, etc., to specify the tensor's number type.
If you don't set dtype, PyTorch picks a default based on your data.
Examples
Creates a tensor of floating-point numbers with 32-bit precision.
PyTorch
import torch
x = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32)
Creates a tensor of 64-bit integers, good for indexing or labels.
PyTorch
y = torch.tensor([1, 2, 3], dtype=torch.int64)
Creates a tensor of boolean values.
PyTorch
z = torch.tensor([True, False, True], dtype=torch.bool)
Sample Model
This program shows how to create tensors with float, int, and bool data types and prints their values and types.
PyTorch
import torch

# Create tensors with different data types
float_tensor = torch.tensor([1.5, 2.5, 3.5], dtype=torch.float32)
int_tensor = torch.tensor([1, 2, 3], dtype=torch.int64)
bool_tensor = torch.tensor([True, False, True], dtype=torch.bool)

# Print tensor details
print(f"Float tensor: {float_tensor}, dtype: {float_tensor.dtype}")
print(f"Int tensor: {int_tensor}, dtype: {int_tensor.dtype}")
print(f"Bool tensor: {bool_tensor}, dtype: {bool_tensor.dtype}")
OutputSuccess
Important Notes
Using float32 is common for neural networks because it balances speed and precision.
Integer tensors are often used for labels or counting, not for calculations.
Boolean tensors are useful for masks or conditions in your data.
Summary
Tensors store numbers with specific data types in PyTorch.
Choosing the right data type helps your model run efficiently.
You can create tensors with float, int, or bool types using the dtype argument.