PyTorch torch.promote_types function
PyTorch torch reference manual
torch.promote_types is a function in PyTorch used for type promotion. It accepts multiple data types as input and returns the common type (the most suitable type) that can accommodate all input types.
Function definition
torch.promote_types(type1, type2)
Parameter Description
type1: The first data typetype2: The second data type
Usage examples
Example
import
torch
Type promotion example
dtype1
=
torch.
float32
dtype2
=
torch.
float64
Get the common type
result_dtype
=
torch.
promote_types
(
dtype1
,
dtype2
)
(
"Common type of float32 and float64:"
,
result_dtype
)
Integer types and floating-point types
dtype3
=
torch.
int32
dtype4
=
torch.
float32
result_dtype2
=
torch.
promote_types
(
dtype3
,
dtype4
)
(
"Common type of int32 and float32:"
,
result_dtype2
)
The output is:
float32 和 float64 的公共类型: torch.float64
int32 和 float32 的公共类型: torch.float32