mindspore.Tensor.astype

Tensor.astype(dtype, copy=True)[source]

Return a copy of the tensor, cast to a specified type.

Parameters
  • dtype (Union[mindspore.dtype, numpy.dtype, str]) – Designated tensor dtype, can be in format of mindspore.dtype.float32 or numpy.float32 or float32.

  • copy (bool, optional) – By default, astype always returns a newly allocated tensor. If this is set to false, the input tensor is returned instead of a copy. Default: True .

Returns

Tensor, with the designated dtype.

Raises

TypeError – If the specified dtype cannot be understood.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> x = Tensor(np.ones((1,2,2,1), dtype=np.float32))
>>> x = x.astype("int32")
>>> print(x.dtype)
Int32