cast

paddle. cast ( x: Tensor, dtype: DTypeLike ) Tensor [source]

Take in the Tensor x with x.dtype and cast it to the output with dtype. It’s meaningless if the output dtype equals the input dtype, but it’s fine if you do so.

The following picture shows an example where a tensor of type float64 is cast to a tensor of type uint8.

legend of reshape API
Parameters
  • x (Tensor) – An input N-D Tensor with data type bool, float16, float32, float64, int32, int64, uint8.

  • dtype (paddle.dtype|np.dtype|str) – Data type of the output: bool, float16, float32, float64, int8, int32, int64, uint8.

Returns

Tensor, A Tensor with the same shape as input’s.

Examples

>>> import paddle

>>> x = paddle.to_tensor([2, 3, 4], 'float64')
>>> y = paddle.cast(x, 'uint8')