PyTorch torch.promote_types Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.promote_typesIt 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: First data type
  • type2: Second data type

Usage Example

Example

import torch

# Type promotion example
dtype1 = torch.float32
dtype2 = torch.float64

# Get common type
result_dtype = torch.promote_types(dtype1, dtype2)

print("Common type of float32 and float64:", result_dtype)

# Integer and float types
dtype3 = torch.int32
dtype4 = torch.float32
result_dtype2 = torch.promote_types(dtype3, dtype4)

print("Common type of int32 and float32:", result_dtype2)

The output result is:

float32 和 float64 的公共类型: torch.float64
int32 和 float32 的公共类型: torch.float32

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions