Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.result_type function

PyTorch torch.result_type function


PyTorch torch reference manual PyTorch torch reference manual

torch.result_type is a function in PyTorch used to determine the result type of an operation. It accepts tensors or scalars as input and returns the data type that should be used after performing the relevant operation.

Function definition

torch.result_type(tensor, scalar)

Parameter Description


Usage examples

Example

import

torch

Create tensors of different types

a

=

torch.

tensor

(

[

1

,

2

,

3

]

,

dtype

=

torch.

float32

)

b

=

torch.

tensor

(

[

4

,

5

,

6

]

,

dtype

=

torch.

float64

)

Get the result type

result_dtype

=

torch.

result_type

(

a

,

b

)

print

(

"Result type of float32 and float64:"

,

result_dtype

)

Use a scalar

c

=

torch.

tensor

(

[

1

,

2

,

3

]

,

dtype

=

torch.

int32

)

result_dtype2

=

torch.

result_type

(

c

,

1.5

)

print

(

"Result type of int32 and 1.5:"

,

result_dtype2

)

The output is:

float32 和 float64 的结果类型: torch.float64

int32 和 1.5 的结果类型: torch.float32


PyTorch torch reference manual PyTorch torch reference manual