mindspore.ops.cast

View Source On Gitee
mindspore.ops.cast(input, dtype)[source]

Returns a tensor with the new specified data type.

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
  • input (Union[Tensor, Number]) – The input tensor or number.

  • dtype (dtype.Number) – The dtype after conversion. Only constant value is allowed.

Returns

Tensor

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> input_np = np.random.randn(2, 3, 4, 5).astype(np.float32)
>>> input = mindspore.tensor(input_np)
>>> dtype = mindspore.int32
>>> output = mindspore.ops.cast(input, dtype)
>>> print(output.dtype)
Int32
>>> print(output.shape)
(2, 3, 4, 5)