mindspore.Tensor.type_as

View Source On AtomGit
Tensor.type_as(other)[source]

Returns self tensor cast to the type of the input other tensor.

Note

When converting complex numbers to boolean type, the imaginary part of the complex number is not taken into account. As long as the real part is non-zero, it returns True; otherwise, it returns False.

Parameters

other (Tensor) – The tensor whose data type is specified. The shape of tensor is \((x_0, x_1, ..., x_R)\).

Returns

Tensor, the shape of tensor is the same as self, \((x_0, x_1, ..., x_R)\).

Raises

TypeError – If other is not a Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> 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)