astype¶
- paddle.Tensor. astype ( self: Tensor, dtype: DTypeLike ) Tensor
-
Cast a Tensor to a specified data type if it differs from the current dtype; otherwise, return the original Tensor.
- Parameters
-
dtype – The target data type.
- Returns
-
a new Tensor with target dtype
- Return type
-
Tensor
Examples
>>> import paddle >>> import numpy as np >>> original_tensor = paddle.ones([2, 2]) >>> print("original tensor's dtype is: {}".format(original_tensor.dtype)) original tensor's dtype is: paddle.float32 >>> new_tensor = original_tensor.astype('float32') >>> print("new tensor's dtype is: {}".format(new_tensor.dtype)) new tensor's dtype is: paddle.float32