mindspore.Tensor.type_as
- mindspore.Tensor.type_as(other)[源代码]
将第一个输入的Tensor的数据类型转换为第二个输入的Tensor的数据类型。
说明
将复数转换为bool类型的时候,不考虑复数的虚部,只要实部不为零,返回True,否则返回False。
- 参数:
other (Tensor) - 数据类型为指定类型的Tensor,其shape为 \((x_0, x_1, ..., x_R)\) 。
- 返回:
Tensor,其shape与输入Tensor相同,即 \((x_0, x_1, ..., x_R)\) 。
- 异常:
TypeError - other 不是Tensor。
- 支持平台:
AscendGPUCPU
样例:
>>> import mindspore >>> import numpy as np >>> from mindspore import Tensor >>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32) >>> self = Tensor(input_np) >>> other_np = np.random.randn(2, 3, 4).astype(np.int32) >>> other = Tensor(other_np) >>> output = self.type_as(other) >>> print(output.dtype) Int32 >>> print(output.shape) (2, 3, 4, 5)