Specifying Data Types
Anonymous contributor
Published Jan 13, 2025
Contribute to Docs
In PyTorch, specifying the data types for tensors
is crucial as they are the core data structures used to store and process data. Each tensor’s data type (dtype
) defines the kind of values it holds (e.g., integer
, float
, boolean
), ensuring precision, improving performance, and maintaining compatibility during computations.
Syntax
To specify a data type in a PyTorch tensor, use the dtype
parameter when creating a tensor or the .to()
method for converting an existing one.
For specifying dtype
when creating a tensor
torch.tensor(data, dtype=torch.<data_type>)
data
: The input data used to create the tensor. This can be a list, NumPy array, or another tensor.dtype
: Specifies the data type of the tensor. Common data types include:torch.float32
(default): 32-bit floating-pointtorch.float64
: 64-bit floating-pointtorch.int32
: 32-bit integertorch.int64
: 64-bit integertorch.bool
: Boolean
For converting an existing tensor to a different data type
tensor.to(torch.<data_type>)
Example
In the example below a tensor is created with a specified data type, another with a different type, and one tensor is converted to a new data type:
import torch# Creating a float32 tensorfloat_tensor = torch.tensor([1.0, 2.0, 3.0], dtype=torch.float32)print(float_tensor)# Creating an int64 tensorint_tensor = torch.tensor([1, 2, 3], dtype=torch.int64)print(int_tensor)# Converting a tensor to a different data typeconverted_tensor = float_tensor.to(torch.int64)print(converted_tensor)
The code above generates the output as:
tensor([1., 2., 3.])tensor([1, 2, 3])tensor([1, 2, 3])
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.