cast

paddle.fluid.layers.tensor. cast ( x, dtype ) [source]

This OP takes in the Tensor x with x.dtype and casts 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.

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

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

Returns

A Tensor with the same shape as input’s.

Return type

Tensor

Examples

import paddle

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