PyTorch torch.dtype function
PyTorch torch reference manual
torch.dtype is a PyTorch object representing a tensor data type, not a function. It is used to get or specify a tensor's data type.
Data type description
torch.float32 - 32位浮点数
torch.float64 - 64位浮点数(双精度)
torch.float16 - 16位浮点数(半精度)
torch.bfloat16 - 16位浮点数(Brain Float)
torch.complex32 - 32位复数
torch.complex64 - 64位复数
torch.complex128 - 128位复数
torch.int8 - 8位整数
torch.int16 - 16位整数
torch.int32 - 32位整数
torch.int64 - 64位整数
torch.bool - 布尔类型
torch.uint8 - 无符号8位整数
Usage examples
Example
import
torch
Create tensors of different data types
x1
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
]
)
x2
=
torch.
tensor
(
[
1
,
2
,
3
]
,
dtype
=
torch.
int32
)
x3
=
torch.
tensor
(
[
True
,
False
,
True
]
,
dtype
=
torch.
bool
)
Get the data type of a tensor
(
"x1 dtype:"
,
x1.
dtype
)
(
"x2 dtype:"
,
x2.
dtype
)
(
"x3 dtype:"
,
x3.
dtype
)